Question

I have a class as such;

[DataContract]
public class Request
{
   [DataMember]        
   public int Id {get;set;}

   [DataMember]
   public UserType Type { get; set; }
}

I have a Wcf webservice that when the method is called, loads the object into Request object and I don't want the incoming Request to modify the Type property. I tried setting the class property as

[DataContract]
public class Request
{
    [DataMember]   
    public int Id {get;set;}

    [NonSerialized]
    [DataMember]
    public UserType Type { get; set; }
}

and I also tried

[DataContract]
public class Request
{
    [NonSerialized]
    private UserType _type;

    [DataMember]
    public UserType Type 
    {
        get { return _type; }
        set { _type = value; }
    }
}

But in both cases I'm getting the error 'Attribute 'NonSerialized' is not valid on this declaration type. It is only valid on 'field' declarations.

The Request object in the Wcf service is loaded like;

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/XML/GetTypes", 
 BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml)]
XElement XMLGetTypes(Request oRequest) 
{ 
  ...do something with the object
}

or this, depending on the Endpoint

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/JSON/GetTypes", 
BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
string JSONGetTypes(Request oRequest) 
{ 
  ...do something with the object
 }

How can I overcome this problem ?

Was it helpful?

Solution

Because WCF includes a new serialization engine the DataContractSerializer

To be more explicit

  • if class is marked as [DataContract], then only the members marked [DataMember] are considered

  • if class is not marked as [DataContract], then it defaults to everything(to support POCO ), but you can subtract members using [IgnoreDataMember]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top