WCF deserialization, does not call the constructor, how to solve the class initialization problem? Encountered this problem, found some information to finally solve the problem. The key is two features:
[OnDeserializing]
[OnDeserialized]
Check the code:
[DataContract] public class Mydatatype {[DataMember] private list<int> nval = new List<in T> (); Public Mydatatype () {nval.add (12); } [ondeserializing] void BeginInit (StreamingContext sc) {Console.WriteLine ("Same to begin Constructor "); } [ondeserialized] void EndInit (StreamingContext sc) {Console.WriteLine ("Same to end Cons Tructor "); } [DataMember] public int Initmemberdata {get {return 0;} set {Object obj = this; Console.WriteLine ("Initmemberdata:set."); }} private int intv; [DataMember] public int int {get {return intv; } set {Console.WriteLine ("Int:set."); if (nval = = null) Intv = value; }} string _str; [DataMember] public string String {get {return _str;} set {Console.WriteLine ("String:set."); _str = value; }} long _long; [DataMember] public long Long {get {return _long;} set {Console.WriteLine ("Long:set."); _long = value; } } }
Output when deserializing:
WCF deserialization, does not call constructors, how to resolve class initialization problems