Ben McCallum's Blog

Dissecting ASP.NET MVC3, CSS3, HTML5, jQuery and a whole lot of other things ending with numbers…

WCF Web Service Wrapper: Closing, Disposing and Aborting best practices

When calling methods on a WCF web service it’s best practise to do the following:
(To avoid the using keyword with WCF clients:

But all this garbage is exhausting and repetitive when you are calling the web service in many places.

One solution is to inherit IDisposable and implement Dispose() for the web service but you’ll need to do that for every web service you consume meaning more duplicate code and using partial classes in combination with the auto-generated code from your service reference in Visual Studio.

Instead, I’ve come up with a hybrid of many ideas on the web that essentially wraps the WCF web service, performs any method calls and closes/disposes/aborts the connection gracefully:

WCF Service Wrapper class


An example of usage:

It’s a very elegant, and highly re-usable piece of code that I’ll be using in all apps that consume WCF web services. Using generics the same Service class can make calls to different services meaning you can plug-n-play this code in all your client apps and never write a try catch finally block around WCF service calls again. And if you ever need to customise the way service calls are handled, errors logged, etc. it’s all in one spot! Enjoy!


Single Post Navigation

7 thoughts on “WCF Web Service Wrapper: Closing, Disposing and Aborting best practices

  1. eric on said:

    Why are you aborting the channel and the factory twice…once in the catch and then again in the finally?

    • benmccallum on said:

      Hey Eric,

      You are right, I realised this a few weeks after I posted and went to use it again except I haven’t revised the code. I’ll revise it now.


      • hi,

        How do you deal with returning a complex type fro mthe service.


      • benmccallum on said:

        Hi Raj,

        Thanks for your comment. Although I’ve used int type in my example usage that could be any generic type as the Action parameter is for you to pass a function to perform. You simply pass it a call to a method on the type T (client object) which does assignment to a variable outside that call.

        Let me know if you have anymore issues. You should find that int can be substituted for any complex type.


  2. Hi, Ben.

    I like your code but I am having a problem getting a behavior added to my service’s endpoint.

    My existing code looks something like:
    var myServiceProxy = new MyServiceClientProxy(“MyBinding”);
    var myBehavior = new BehaviorToBeAdded(“SomeValue”);

    … and then I’d invoke the method that I needed.

    I tried fitting this into your code by adding the behavior after creating factory and before calling “CreateChannel”, but when “action(client)” gets invoked the behavior is not there.

    Any thoughts?


    • benmccallum on said:

      Hi Mike,

      Sorry, my experience with Behaviours is limited. Do they need to be added to the T client rather than the ChannelFactory?

      If that behavior is cross-cutting across all your methods on that client, you’ll want to update the Service class to add that behavior in the right spot (as you mentioned) though you’d probably be better off making it strictly to T of your client type.

      If it’s a one off behavior that needs to be added just for a particular method call on that client T instance, then I’d try something like this if possible:

      Service.Use(client =>
      client.Endpoint.Behaviors.Add(new BehaviorToBeAdded(“SomeValue”));

      • Hi, Ben.

        I just figured it out. I implemented my behavior incorrectly.

        After making the corrections everything works just fine.



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: