Handcrafting the WCF client

Two years ago I wrote an article Building a Windows Communication Foundation client, describing three ways of creating WCF clients. I did not mention the fourth one, as I did not know at the time. Now I am older and wiser ;-)

The four ways of creating a WCF client are:

  1. Visual Studio’s “Add Service” reference
  2. Service Model Metadata Utility Tool (SvcUtil.exe) command line tool
  3. Dynamic proxy with ChannelFactory
  4. Handcrafting the WCF client

The last two options are only viable for WCF to WCF implementations where you either have control of both service and client or the service provider supplies a .Net assembly. Because both of them require the service contract aka the .Net interface marked with ServiceContractAttribute and OperationContractAttribute.

Anybody that has tried the “Add Service” reference in Visual Studio knows it is broken. First of all it generates an enormous amount of files, even for the simplest service contract.

Secondly it sometimes corrupts the state of the generated files, so you have to remove the reference and then add it again.. Just try to google it and you will find many frustrated developers. Do not use it!

The SvcUtil.exe is better and has a vast number of options. This is the preferred option for non .Net services or where the service contract interface is not available.

The dynamic proxy with ChannelFactory is useful, but be aware that the interface returned does not implement IDisposable, but the implementation does. See below:

var factory = new ChannelFactory<ihelloworldservice>("myEndPoint");

IHelloWorldService proxy = factory.CreateChannel();

using (proxy as IDisposable)
{
   MessageBox.Show(proxy.HelloWorld());
}

Finally the handcrafted version – the one I want to talk about. Writing the WCF client by hand is fairly easy – it requires the implementation to inherit from ClienBase and implementing the service contract interface. Below is first a simple service contract IHelloWorldService:

[ServiceContract(Namespace = "www.lybecker.com/blog/HelloWorldService")]
public interface IHelloWorldService
{
   [OperationContract]
   string HelloWorld();
}

Next the handcrafted WCF client with constructors and service contract interface implementation:

public class HelloWorldClient : ClientBase<ihelloworldservice>, IHelloWorldService
{
   public HelloWorldClient()
   { }

   public HelloWorldClient(string configurationName) : base(configurationName)
   { }

   public HelloWorldClient(Binding binding, EndpointAddress address) : base(binding, address)
   { }

   public string HelloWorld()
   {
      return Channel.HelloWorld();
   }
}

That’s it. :-)

Now you have full control of the WCF client implementation.

Download the sample application Hello World WCF clients including a handcrafted version


 Anders Lybecker is an chief architect at Kring Development A/S, a consultancy firm in Copenhagen, Denmark. He holds a degree in software engineering specializing in software development. His primary expertises are the Microsoft .Net framework and SQL Server which he has been working with since the start of this century! He enjoys discussing technical topics, teaching and speaking at conferences.


Related posts:

  1. Building a Windows Communication Foundation client
  2. A simple Windows Communication Foundation Web Service
  3. WCF Sessions and Reliable Messaging
  4. Configuring the DTC for WCF with OleTx
  5. How to view default values for a WCF binding

Tags: ,

6 Responses to “Handcrafting the WCF client”

  1. Hmm… any ideas on http://stackoverflow.com/questions/903977/inheriting-from-a-generic-contract-in-wcf ? I’ve been wasting almost 3 days on this issue. I’m kinda desperate, hence me bugging random people on the intarweb :P

  2. Hi Marcel,

    You must use the standard way of defining generic types in configuration file if you want to use a service contract like IBasicContract

    It is written in the configuration file like this: IBasicContract`2[TRequest,TResponse]

    I also posted this reply on stackoverflow in case you do not see this.
    :-)
    Anders Lybecker

  3. Thank you :) The problem with that would have been the fact that I reference several distinct workflows from the same program… they can’t all use the same contract. Anyway, I managed to solve the issue. Thanks again for taking the time to answer!

  4. RajmundNo Gravatar says:

    How can I invoke wcf services asynchronously?

  5. Hi Rajmund,

    I can’t think of a good way of implementing an asynchronous handcrafted client proxy.

    Add Service Reference and the SvcUtil creates a modified service contract with BeginXXX and EndXXX methods. For the developer this is nice, as the asynchronous method invocation functionality is explicated state and follows the Asynchronous Design Pattern.

    I would just use delegates with the handcraftet WCF client, like so:

    var proxy = new HelloWorldClient();

    Func<string> del = proxy.HelloWorld;

    del.BeginInvoke(callback =>
    Console.WriteLine(del.EndInvoke(callback)), null);

    Can you thing of a better option that explicit states the possibility of the asynchronous method invocation?

Leave a Reply