Frequent Cause of WCF Exception "The connection was closed unexpectedly"

If you're communicating with WCF via a ClientBase proxy class, you may, at one time or another, have encountered the cryptic exception "The underlying connection was closed: The connection was closed unexpectedly."

If your WCF method is returning something other than void when you encounter this exception, there's a good chance that the problem has to do with an inability to serialize the return value, rather than anything to do with the "underlying connection."  This is almost certainly the case if other calls to void-returning-WCF-methods are working just fine.  To verify and fix, run the following code example, replacing MyObject with the class that you're returning from the WCF method, to see if the result type will in fact serialize successfully.

MyObject myObject = new MyObject();
System.Xml.Serialization.XmlSerializer serializer =
   new System.Xml.Serialization.XmlSerializer(myObject.GetType());
serializer.Serialize(new MemoryStream(), myObject);

The only limitation to this troubleshooting approach is that if you're using DataContract/DataMember attributes on your properties, then WCF may end up serializing the object successfully while this test code will fail if you're not decorating the culprit property with the DataMember attribute.  Accordingly, if you're using DataContract/DataMember attributes, you'll want to decorate unincluded properties with [XmlIgnore] to make the XML serialization test mimic what WCF will be serializing.  (Please let me know if you know of a cleaner way to do this without dirtying up the object.)

If you're developing TDD-style (of course you are!), just make a test with this code called CanWcfReturnTypeBeSerialied() or something along those lines.  On a related note, for a good resource concerning serializing and deserializing objects in C#, along with a couple of nice helper methods, check out http://www.dotnetjohn.com/articles.aspx?articleid=173.

I've run into this problem enough times (including an hour or so on it today) to blog about it...mostly so that I'll find my own post via Google the next time I run into it!

Billy McCafferty
http://www.itsamuraischool.com


Posted 07-29-2009 2:24 PM by Billy McCafferty
Filed under:

[Advertisement]

Comments

Dennis Leng wrote re: Frequent Cause of WCF Exception "The connection was closed unexpectedly"
on 07-30-2009 6:02 PM

Another easy/efficient way i found is to use Svctraceviewer, but you have to enable it in the web.config.

HTH

Billy McCafferty wrote re: Frequent Cause of WCF Exception "The connection was closed unexpectedly"
on 07-30-2009 7:41 PM

Good call HTH...an important tip that I should have included.

justin davies wrote re: Frequent Cause of WCF Exception "The connection was closed unexpectedly"
on 08-03-2009 3:10 AM

Thanks Billy - this made me realise that this is something I always leave out in TDD... that the types remain serializable throughout development.

I put something together that uses the native WCF DataContractSerializer to faciliate assertions: www.beingnew.net/.../datacontract-serialization-and.html

justin davies wrote re: Frequent Cause of WCF Exception "The connection was closed unexpectedly"
on 08-03-2009 3:12 AM

Thanks Billy - this made me realise that this is something I always leave out in TDD... that the types remain serializable throughout development.

I put something together that uses the native WCF DataContractSerializer to faciliate assertions: www.beingnew.net/.../datacontract-serialization-and.html

Sidar Ok wrote re: Frequent Cause of WCF Exception "The connection was closed unexpectedly"
on 08-07-2009 5:46 AM

Nice one Billy. But in my experience the main problem here is usually having "any exception" not necessarily a serialization one. Since this cryptic error can come up by any exception a betteradvice would be to go and trace down what the error is, and one of the ways is what Dennis mentioned, add includeExceptionDetailInFaults to your config.

Another way is to add this as an attribute to your service ( all sets System.ServiceModel.Description.ServiceDebugBehavior.IncludeExceptionDetailInFaults  to true).

Billy McCafferty wrote re: Frequent Cause of WCF Exception "The connection was closed unexpectedly"
on 09-22-2009 5:46 PM

@Sidar, I like that IncludeExceptionDetailInFaults...very helpful!

Silvia wrote re: Frequent Cause of WCF Exception "The connection was closed unexpectedly"
on 02-17-2010 12:04 PM

yUlvPN I am always excited to visit this blog in the evenings.Please churning hold the contents. It is very entertaining.

Ruzive Mazhandu wrote re: Frequent Cause of WCF Exception "The connection was closed unexpectedly"
on 04-04-2010 5:20 AM

I was about to loose all my hair from the thinking and then it hit me when i was using classes that i had created myself everything was working ok,but when i turned to LINQ i started having this error only when using functions that return object of which the class were generated automatically by VS so the problem had to be with the classes themselves and i was right all i had to do was add [DataContract] to all the classes created by LINQ the classes have [Table(Name="tablename")] on top.

hope this will save all you time  

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)