cost
cost

Reputation: 4480

Setting a default value of a parent variable within a datacontact?

I have a base class

[DataContract]
public class BaseRequest
{
    [DataMember]
    public Guid Key;

    [DataMember]
    public RequestType RequestType;


}

There are lots of classes that will inherit this and I'd like them to set their inherited RequestType Enum automatically

[DataContract]
public class LoginRequest : BaseRequest
{
    [DataMember]
    public void LoginRequest()
    {
        this.RequestType = StationHouseWebServices.RequestType.Login;
    }

    [DataMember]
    public string Username;

    [DataMember]
    public string Password;
}

But since this is a WCF DataContact my service reference will only carry over the DataMembers, not the constructor. Is there a way I can still do this, have a default value for a parent's variable assigned automatically? Something like

this.RequestType = StationHouseWebServices.RequestType.Login;

Upvotes: 0

Views: 110

Answers (1)

tom redfern
tom redfern

Reputation: 31770

Because the constructors won't be called WCF has provided a handy way for you to run any setup code which would normally be executed in the constructor. This is called the OnDeserializedAttribute.

[OnDeserializedAttribute]
private void RunThisCode(StreamingContext context)
{ 
    ...
}

See here: http://msdn.microsoft.com/en-us/library/system.runtime.serialization.ondeserializedattribute.aspx

Upvotes: 1

Related Questions