Tutorial :Issue with deserialization of a static property in .Net


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.


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.


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.


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.)


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.


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.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »