Welcome to MSDN Blogs Sign in | Join | Help

MSDN Utopia

Salvador Patuel 's architectural challenges. Application Development Consultants Team
Why "using" may play tricks on your WCF service host

Many of the examples that have been published on the web about hosting the WCF use the "using" block to initialize the service. This blog entry shows you why this can catch you.

The problem arises when you try to open your host and there is a problem with the configuration file, if you were coding with “using” and you try to catch an exception you will find the familiar meaningless message:

“The communication object, System.ServiceModel.ServiceHost, cannot be used for communication because it is in the Faulted state.”

The next step is to go to the configuration file and play around with the parameters until you find which one is causing the hassle. You fix it and life goes on... but what does this message mean?

Well, during this process we were missing an important piece of information, if you check the Output window you will find that actually are two exceptions:

“System.InvalidOperationExceptoin”
 and
“System.ServiceModel.CommunicationObjectFaultedException”

Indeed, if you remove your error trapping and run the code you will get the first exception, and guess what? It contains the useful information regarding the problem with the configuration file. But why you don’t catch it with your try/catch block? The problem is on the “using” block.

The using block is a try/finally implementation, this means that if an exception occurs it will execute the finally and then it will be thrown to the next exception block. Reading this you should think “aha! But why is not appearing on my exception control?”

The answer to this is the nature of the second exception, this one is not thrown because of your configuration file, it is thrown because the finally statement of the “using” block is failing. If we write the same code as the “using” you will see:

ServiceHost Host = null;

 

try

{

       Host = new ServiceHost(MySingletonService);

       Host.Open();

       Console.ReadKey();

       Host.Close();

}

finally

{

       if (Host != null)

              ((IDisposable)Host).Dispose();

}

The configuration exception is thrown by the “Host.Open()” line, the code jumps into the finally block and tries to dispose the host. Here the host is not null but it is in a faulty state, this means that it cannot be disposed and this raises the second exception that you usually see on your application.

Posted: Wednesday, April 25, 2007 2:55 PM by Salva Patuel

Comments

MichaelGiagnocavo said:

Yes, this silly design led to quite a few lost hours. Also, having a try/finally everywhere in your code is just a mess. Fortunately C# makes encapsulating patterns easy.

We define a method like this:

public static void UseServiceClient<T>(Action<T> action)

Which internally does the try/finally. Then you can make all your service calls like this:

UseServiceClient<ISomeService>(delegate(ISomeService client)

{

 client.Foo();

});

In C# 3, the syntax will be even nicer. Anyways, more details here:

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1116280&SiteID=1

# April 25, 2007 10:29 AM

Jochen Zeischka said:

Great post!

# September 7, 2007 5:13 AM

Steve said:

Great post! Spent ages trying to figure out why I wsn't getting any exceptions even though the service was failing. Thank you!

# December 30, 2008 12:02 AM

Cordell Lawrence said:

Fantastic post. I didn't think that the exception would be lost like this... they couldn't even put it in the inner exception? ... again, thanks for the post.

# July 31, 2009 9:26 AM

Jader Dias said:

I wrote a variation of Michael code:

   public static class CommunicationObjectExtensions

   {

       public static void Use<TCommunicationObject>(this TCommunicationObject communicationObject, Action<TCommunicationObject> action) where TCommunicationObject : ICommunicationObject

       {

           try

           {

               action(communicationObject);

               communicationObject.Close();

           }

           finally

           {

               communicationObject.Abort();

           }

       }

   }

# August 7, 2009 3:47 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker