2

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?

Ammar Raja
  • 1,284
  • 5
  • 14
  • 23
muhammad kashif
  • 2,566
  • 3
  • 26
  • 49

1 Answers1

1

You will most likely find that objUser is not serializable. For more info read this: MSDN: Using Data Contracts

To quote a short snippet of that:

All .NET Framework primitive types, such as integers and strings, as well as certain types treated as primitives, such as DateTime and XmlElement, can be serialized with no other preparation and are considered as having default data contracts.... New complex types that you create must have a data contract defined for them to be serializable.

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.

Community
  • 1
  • 1
slugster
  • 49,403
  • 14
  • 95
  • 145
  • thankyou slugster for you reply, let me have a look at links you mentioned . then i will get back to you. – muhammad kashif May 11 '11 at 07:39
  • ok , well **objUser** is not serializable but should i make it serializable or should i use another approach? as objUser is an object of a class which exists in another project and working fine. if i change my **User** class then there might be problems. give me suggestions how to solve this issue? – muhammad kashif May 11 '11 at 08:02
  • @user, personally i would make it serializable. If it introduces too much risk because it is used in other projects or it isn't suitable as a DTO then you should just create a new User object that meets your needs. – slugster May 11 '11 at 08:17
  • but why this class is not serializable? my User class works fine if i comment the IPAddress property.i mean User Class has a propert like this public IPAddress IP{ get;set;} if i comment this code it works fine. it means my IPAddress class is not serializable, right? but why it is not serializable as all other classes are? am using many other class in my User class but all works fine, only IPAddress class creates problem. why? – muhammad kashif May 11 '11 at 08:46
  • @user, see my edit. I've given you a whole bunch of reference links which should keep you busy for a while ;) P.S. it seems like this has answered your question, so don't forget to mark it as the answer if you agree. – slugster May 11 '11 at 11:19
  • @user, there should be a tick (check) mark underneath the votes up/down buttons, you can just click on it and it should turn green (only you as the question asker can see it until it has been set, once it is selected everyone else can then see that it was the correct answer). Did you decide how to fix the issue? – slugster May 12 '11 at 09:09
  • Yes in my IPAddress class i created a parameterless constructor and that works for me :-) – muhammad kashif May 12 '11 at 11:53