The underlying connection was closed: The connection was closed unexpectedly

"The underlying connection was closed: The connection was closed unexpectedly."

Generally, this Communication Exception is thrown due to serialization problem and message is displayed as-

underlyingconnectionwasclosed

If you are expecting an return from a called method and using WCF service, then one possible cause behind this error could be your object serialization/de-serialization problem. If your service method returns something that is not serialize-able then your proxy channel makes this error.

To avoid this, just check-

1. The return type and make sure it is serializable. You may have to put [Serializable] attribute with your class as below:

 [DataContract(Namespace = "http://codePattern.net/DataTypes/2013/03", Name ="StudentDetail")]

[Serializable]

 public class StudentDetail

   {

     [DataMember(IsRequired=true)]

        public string StudentName { getset; }

   }

IsRequired=true; tells that the value will be present while serializing ( its says underline property is a Non-Nullable value type).

 

2.  Make sure your Enum values are matched with the values stored inside tables. This is the very critical situation because you don't expect it.

 

Happy Coding :)