Tuesday, August 11, 2009

using statement causes undesired result in WCF client.

I have a WCF service client which is a WinForms desktop application.  I have a method that is responsible for uploading certain information by consuming the WCF service.  I was initializing the proxy as follows:

using ( proxy = new WCFServiceClient. WCFServiceClient() ) {
proxy = new WCFServiceClient. WCFServiceClient() ;
proxy.UploadInfo();
}

It was working just fine.  Then I had to make some changes (added a new method to the business logic layer called by the service) on the server side, it compiled without any error and passed the unit test.  So I published it to my test site and ran a unit test against the test site.  To my surprise, I started getting the following error message: 
The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it is in the Faulted state.

After bit of searching, I found this article.

According to this artical, one "should not use the C# "using" statement to automatically clean up resources when using a typed client".  The reason being, "The C# "using" statement results in a call to Dispose(). This is the same as Close(), which may throw exceptions when a network error occurs. Because the call to Dispose() happens implicitly at the closing brace of the "using" block, this source of exceptions is likely to go unnoticed both by people writing the code and reading the code. This represents a potential source of application errors."  That was exactly what was going on with my code.  The "using" statement was masking the actual error that was occurring on the server.  After removing the "using" statement and running the test again, I got the actual error which was the service couldn't find the new method that I had just added.  It turned out that the publishing tool didn't update the business logic DLLs.  The problem went away after forcing a replacement of the BLL DLLs.



No comments:

Post a Comment

About Cullen

My photo
Christian, Father, Software Developer/Architect who enjoys technology and using it to make people's lives easier!

Followers