开发者

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

So I have some code like this.

[DataContract]
public class Example
{
    SomeClass _someVar;

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

Here is the funny thing, OnDeserializing() gets called if I use the test debugging client from Visual Studio 2010. But if I try and host my WCF service and then call it from my own client it doesn't get called (or probably do开发者_StackOverflowesn't), because _someVar is always null.

Argh!

What else do I need to do?

Kind regards, Fugu


WCF does not use standard .net serialization, so I'm not certain it will invoke your OnDeserializing method. However you can ask WCF to use an XmlSerializer, which should give you the behavior you want. Have a look at "Controlling the Serialization Process" here.


Further to PaulF's answer, your class is not a singleton - 2 calls to the service will by default instantiate Example twice and call the method once.

Because of this, there's really very little point in having any variables declared at a class level.

If you want to change this behaviour, have a look here for more information

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜