I created a WCF service.in my data contract I have two attributes lets say:
[DataMember]
Private User objUser;
and
[DataMember]
Private tempClass ObjTemp;
I have get and set property for both the attributes. In my implementation class, I have an object of datacontract class… lets say. objData.
When I assign
objData.ObjTemp=(function which return objTemp);
the service work fine. But, when I assign.
objData.objUser=(function which return objUser)
it throws following error:
The underlying connection was closed: The connection was closed
unexpectedly.
When I comment
objData.objUser=(function which return objUser)
it works fine again.
When I inspect the code inside my User class, I found one property creating problem. When I change the property that property it works fine too; but i do not know why that property is creating problem.
The property is like this:
public IPAddress IP { get; set; }
Now this IPAdress class contains a constructor, and get and set for ip variable. In the get, it simply returns ip variable.
in set it checks some condition and then assign value to ip variable. If condition fails, it throws an exception. Can anybody explain what the problem could be?
I just checked another thing.
If i remove [DataMember] attribute from private User objUser; it works fine; but if i put [DataMember] back it generates the same error.
any suggestions?
You will most likely find that
objUseris not serializable. For more info read this: MSDN: Using Data ContractsTo quote a short snippet of that:
You could also consider using the Service Trace Viewer tool from Microsoft to help track down the exact problem you are having.
Edit:
if it is the System.Net.IPAddress object you are talking about, then it is marked as
[Serializable], but it doesn’t have a default parameterless constructor, which is one of the requirements necessary when using the DataContractSerializer (which is what is used to serialize your data objects over the WCF boundary). You may also want to ensure that if any of your custom objects used in your WCF calls contain any further custom objects (in properties, like your IPAddress) then they are decorated with the KnownType attribute.So, as a solution you can either write your own IPAddress class that plays nice with the DataContractSerializer, or switch to using the XmlSerializer in your WCF calls.