While working on a WCF Service call to return an e-commerce order object to a web client , I sometimes got this puzzling error inside client code:
“The underlying connection was closed: The connection was closed unexpectedly.”
At the beginning, I thought this must be something to do with Entity connection or some SQL to Entity operations inside the data tier that was built on ASP.Net Entity Framework; but there was nothing to be found there.
Then I searched and visited many web postings, and most of them pointed to configuration problem with WCF service model’s endpoint settings. I even followed some postings and made the corresponding changes; but this dreaded error still lingered. I was about to give up before I realized that the WCF service call was working fine before I added couple of enumerated class members to the DataContract class that is to return. Here was the class that was built into my WCF services layer and to be returned to client when the service method “GetOrder” was called:
[DataContract] public class EcommOrder : SalesOrder
{
[DataMember] public int EcommOrderID { get; set; }
[DataMember] public int EcommOrderTypeID { get; set; }
// … more data members
}
//web service method call
public EcommOrder GetOrder(int orerid)
{
EcommOrder returned=new EcommOrder();
//retrieve order from data tier (EF) and populate the object
return returned;
}
On the client side where the service was invoked, the serializable object EcommOrder was fetched good and sound. Then I added this enum member to the EcommOrder class inside the Service tier:
[DataContract] public enum OrderTypes
{
[EnumMember]
StudentOrder=1,
[EnumMember]
CorporateOrder=2,
//and so on
} and EcommOrder now looks like :
[DataContract] public class EcommOrder : SalesOrder
{
[DataMember] public int EcommOrderID { get; set; }
[DataMember] public int EcommOrderTypeID { get; set; }
[DataMember] public OrderTypes EcommOrderType;
// … more data members
}
Then I went back to the client code and call the same service call to return the modified EcommOrder object: Oops, now I got that dreaded and misleading error:
“The underlying connection was closed: The connection was closed unexpectedly.”
To confirm the Enum was the cause, I went back and took out the EcommOrderType enumerated member, then re-called the service, yes, the error again went away.
How wacky was that?
Appreciation pro this article. Present are categorically tips participating in at this juncture to I choice benefit.
Canon Camera Reviews
Great post however I was wondering if you could write a litte more on this topic? I’d be very grateful if you could elaborate a little bit further. Cheers!
My website is Anxiety remedies.
Please see my comments to the post. Thanks for reading.
As it turned out, I needed a default enum member set to value 0. for example, [EnumMember] DefaultOrderType=0; I remembered after I added that, the error went away.