Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 142009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:52:06+00:00 2026-05-11T07:52:06+00:00

I like instantiating my WCF service clients within a using block as it’s pretty

  • 0

I like instantiating my WCF service clients within a using block as it’s pretty much the standard way to use resources that implement IDisposable:

using (var client = new SomeWCFServiceClient())  {     //Do something with the client  } 

But, as noted in this MSDN article, wrapping a WCF client in a using block could mask any errors that result in the client being left in a faulted state (like a timeout or communication problem). Long story short, when Dispose() is called, the client’s Close() method fires, but throws an error because it’s in a faulted state. The original exception is then masked by the second exception. Not good.

The suggested workaround in the MSDN article is to completely avoid using a using block, and to instead instantiate your clients and use them something like this:

try {     ...     client.Close(); } catch (CommunicationException e) {     ...     client.Abort(); } catch (TimeoutException e) {     ...     client.Abort(); } catch (Exception e) {     ...     client.Abort();     throw; } 

Compared to the using block, I think that’s ugly. And a lot of code to write each time you need a client.

Luckily, I found a few other workarounds, such as this one on the (now defunct) IServiceOriented blog. You start with:

public delegate void UseServiceDelegate<T>(T proxy);   public static class Service<T>  {      public static ChannelFactory<T> _channelFactory = new ChannelFactory<T>("");           public static void Use(UseServiceDelegate<T> codeBlock)      {          IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();          bool success = false;          try          {              codeBlock((T)proxy);              proxy.Close();              success = true;          }          finally          {              if (!success)              {                  proxy.Abort();              }          }       }  }  

Which then allows:

Service<IOrderService>.Use(orderService =>  {      orderService.PlaceOrder(request);  });  

That’s not bad, but I don’t think it’s as expressive and easily understandable as the using block.

The workaround I’m currently trying to use I first read about on blog.davidbarret.net. Basically, you override the client’s Dispose() method wherever you use it. Something like:

public partial class SomeWCFServiceClient : IDisposable {     void IDisposable.Dispose()      {         if (this.State == CommunicationState.Faulted)          {             this.Abort();         }          else          {             this.Close();         }     } } 

This appears to be able to allow the using block again without the danger of masking a faulted state exception.

So, are there any other gotchas I have to look out for using these workarounds? Has anybody come up with anything better?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T07:52:06+00:00Added an answer on May 11, 2026 at 7:52 am

    Actually, although I blogged (see Luke’s answer), I think this is better than my IDisposable wrapper. Typical code:

    Service<IOrderService>.Use(orderService=> {   orderService.PlaceOrder(request); });  

    (edit per comments)

    Since Use returns void, the easiest way to handle return values is via a captured variable:

    int newOrderId = 0; // need a value for definite assignment Service<IOrderService>.Use(orderService=>   {     newOrderId = orderService.PlaceOrder(request);   }); Console.WriteLine(newOrderId); // should be updated 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am hosting a WCF service inside of a WPF app. I would like
An idiom commonly used in OO languages like Python and Ruby is instantiating an
I'm instantiating an autocomplete input for Google Maps API (level 3), like so: var
I would like to write a hql query using a dynamic instantiation with a
Like the Delicious submission bookmark-let, I'd like to have some standard JavaScript I can
Like many of you, I use ReSharper to speed up the development process. When
I want to define a protocol and create an easy, standard way to grab
Is there a good way to test the performance of instantiating my controls, specifically
I'd like to use define a generic class in java, that can only be
I am instantiating a .NET COM object and would like to update the ConfigurationManager.AppSettings

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.