views:

139

answers:

4

I have a class called Test which has four public properties and one of them is static. the problem is after deserialization the static property contains null value. i have debugged the code and found that at server side it contains the value which is a collection , but at client side it becomes null after deserialization. i know static members doesn't serialize and deserialize so obviously it should contain the value.

+2  A: 

Static variables are global and stateful - thus they exist solely in the context of the application, or in other words, memory.

You could pass the value of the static property in another non-static property, but you can't send your application's memory down to the client.

MunkiPhD
+1  A: 

I'm not sure I understand...as you say in your question:

i know static members doesn't serialize and deserialize...

Given that, why would you expect the value from the server to propagate to the client? You will need to find an alternative means of transferring this property (make an instance property, send your own message, etc.)

Adam Robinson
+2  A: 

If the static value is initialised when the type is loaded (via a field initialiser or via the type initialiser/static constructor) then it should contain the value.

If however, the server side static value is initialised as a side effect of some method call, then you would have to reproduce this method call on the client as well.

Drew Noakes
hi i m still facing the problem. let me tell in more detailed. i call my server side using web service. i found in debugging that after OnDesrialize method collection becomes null. While debugging I found that some text"transition from client to server" and after that deserialization calls and the collection becomes null. it htorws soapexception. pls can somebody tell me more abt ths.
Hi Punit. I think you will have to post some more information in the original question. If you could extract some of the code from your class (including the `[OnDeserialised]` or `[OnDeserialising]` methods) it would be helpful.
Drew Noakes
A: 

Remember that static member values exist within a particular .NET application domain, and application domains exist within a particular operating system process.

Given that the server and client are different operating system processes and possibly even different machines, as Adam mentioned, there is no way for the value you had on the server to automatically transfer to the client without you writing some code.

I think maybe there is a misconception - Serialization is not packaging up the instance and its static members on the server, and sending it down to the client. It is extracting the values of the members it regards as serializable (e.g. members annotated with [DataMember], or instance members, but not static members), and sending down only those values to the client.

Therefore the value on the client will be the same as the value on the server was before you set it to the value you're now expecting to see on the client.

However: I notice you also mention you see that a collection of yours has a null value.

If you are using DataContractSerializer in a PartialTrust environment, be aware that it may not call the constructor of your class.

Quote:

When instantiating the target object during deserialization, the DataContractSerializer does not call the constructor of the target object.

If that collection was created by your constructor, this may explain why you see null.

Leon Breedt