WCF, how do I create all objects in a DataContract? [OnDeserializing] does not work

So, I have such code.

[DataContract] public class Example { SomeClass _someVar; [OnDeserializing] public void OnDeserializing(StremingContext c) { _someVar = new SomeClass(); } } 

Here's the funny thing, OnDeserializing () is called if I use a test debugging client from Visual Studio 2010. But if I try to host my WCF service and then call it on my own client, it wonโ€™t be called (or probably not), because _someVar is always null.

Argh!

What else do I need to do?

Regards, Fugue

+4
source share
2 answers

WCF does not use standard .net serialization, so I'm not sure if it will call your OnDeserializing method. However, you can ask WCF to use the XmlSerializer, which should give you the behavior you want. Take a look at "Managing the Serialization Process" here .

+1
source

In response to PaulF's answer, your class is not single - 2 service calls will, by default, repeat Example twice and call the method once.

Because of this, it makes little sense to specify any variables at the class level.

If you want to change this behavior, look here for more information.

0
source

Source: https://habr.com/ru/post/1339799/


All Articles