我有一个类,为什么我不能在WCF对象中指定NonSerialized
[DataContract]
public class Request
{
[DataMember]
public int Id {get;set;}
[DataMember]
public UserType Type { get; set; }
}
我有一个WCF web服务,当方法被调用,加载对象到Request对象,我不希望传入的请求修改类型属性。我试图将类属性为
[DataContract]
public class Request
{
[DataMember]
public int Id {get;set;}
[NonSerialized]
[DataMember]
public UserType Type { get; set; }
}
,我也试过
[DataContract]
public class Request
{
[NonSerialized]
private UserType _type;
[DataMember]
public UserType Type
{
get { return _type; }
set { _type = value; }
}
}
但在这两种情况下,我发现了错误“属性‘非序列化’是不会在这个声明类型有效。它只对'field'声明有效。
Wcf服务中的Request对象被加载为;
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/XML/GetTypes",
BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml)]
XElement XMLGetTypes(Request oRequest)
{
...do something with the object
}
或这取决于端点
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/JSON/GetTypes",
BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
string JSONGetTypes(Request oRequest)
{
...do something with the object
}
我怎样才能解决这个问题?
请求类DataContract?你在哪里创建Request对象并为其赋值? – danish
我更新了我的问题以显示我如何分配/创建对象 – Tommassiov