Dynamic WCF Client Endpoint

WCF is fantastic for easy communication between applications. WCF is normally implemented using binding configurations embedded in each application. You usually have configuration for your server, and also your endpoint for your client. I found myself in a situation in which I had no readily available configuration file to use, as well, the endpoint address was dynamic itself based on the application that is consuming this library. Thus, I began searching on how to programmatically setup your client endpoint. Turns out, it is not that hard. Check it out:

  1. // dynamically configure based on passed service url
  2. var binding = new BasicHttpBinding();
  3. var endpoint = new EndpointAddress(“http://SPS02:4762/StatService.svc”);
  4. var channelFactory = new ChannelFactory(binding, endpoint);
  6. // create and make channel call
  7. IStatService service = channelFactory.CreateChannel();
  8. StatResults results =  service.PostStats(“hello world”);

In my example, I have created a custom statistics posting service. So, IStatService and StatResults is just the service contract created for must WCF service (these would change with your application). Notice the string passed in indicating the location of the service. This obviously creates a BasicHttpBinding configuration only, but you should be able to alter any of your standard WCF Configuration settings programmatically here too (make sure they match the service though J). With this above implementation I have NO config file (and the endpoint address is passed to this code – not hardcoded as shown above).


  1. Sumit says:

    var channelFactory = new ChannelFactory(binding, endpoint);

    Does not seem to work. Says that cannot create an instance of the abstract class.

  2. Leo says:

    Thanks for the post -it’s helpful. Note that new ChannelFactory is a generic and requires a type reference, e.g., ChannelFactory(…)

  3. Serhat says:

    Thanks for the post and it works with :

    var chnannelFactory = new ChanneloıFactory(binding,endPoint)

Leave a Reply