WCF DataContract属性 [英] WCF DataContract properties
问题描述
我使用SoapUI注意到,我的OperationContract的DataContract(请求)参数被视为可选". (我也对其进行了测试,实际上,我可以避免在wcf方法中传递请求,或者将其作为null传递)
< soapenv:信封xmlns:soapenv ="http://schemas.xmlsoap.org/soap/envelope/" xmlns:con ="......" xmlns:data ="...">
< soapenv:Header/>
< soapenv:Body>
< con:MyMethod>
<!-可选:->
< con:request>
< data:Id>?</data:Id>
</con:request>
</con:MyMethod>
</soapenv:Body>
</soapenv:信封>
在DataMember上,就像示例中的ID一样,我可以放置属性
[DataMember(IsRequired = true,EmitDefaultValue = false)]
公共字符串ID {get;设置;}
I noticed using SoapUI that the DataContract (request) parameter of my OperationContract is considered "optional". (I also tested it and in fact i can avoid to pass the request at my wcf method, or pass it as null)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:con="..." xmlns:data="...">
<soapenv:Header/>
<soapenv:Body>
<con:MyMethod>
<!--Optional:-->
<con:request>
<data:Id>?</data:Id>
</con:request>
</con:MyMethod>
</soapenv:Body>
</soapenv:Envelope>
On a DataMember, like Id in the example, i can put the attributes
[DataMember(IsRequired = true, EmitDefaultValue = false)]
public string Id {get; set;}
在这种情况下,如果我根本不放置xml Id部分,或者我将其放置为null,则会得到serializzation异常作为响应,并且永远不会调用我的OperatationContract方法,因此我可以确定实现我方法的ID将会是!= null.
我不知道如何使用请求本身来做到这一点.
In this case if I don't put at all the xml Id part, or I put it as null I get a serializzation exception as response, and my OperatationContract method is never called, and therefore i can be sure in the implementation of my method that Id will be != null.
I don't know how to do this with the request itself though.
推荐答案
您好rng94375,
Hi rng94375,
根据您对我的经验的描述,DataMemberAttribute属性
According to your description based on my experience, the DataMemberAttribute attribute
具有IsRequired属性(默认为false).
has an IsRequired property (the default is false).
该属性指示给定数据成员是否必须存在于序列化数据中
The property indicates whether a given data member must be present in the serialized data
反序列化时.如果IsRequired设置为true,(这表示必须
when it is being deserialized. If IsRequired is set to true, (which indicates that a value must
存在)并将EmitDefaultValue设置为false(指示该值不得为
be present) and EmitDefaultValue is set to false (indicating that the value must not be
(如果将其设置为默认值,则显示),此数据成员的默认值不能为
present if it is set to its default value), default values for this data member cannot be
序列化,因为结果会矛盾.如果将这样的数据成员设置为其
serialized because the results would be contradictory. If such a data member is set to its
默认值(通常为null或零)并尝试进行序列化,SerializationException
default value (usually null or zero) and a serialization is attempted, a SerializationException
被抛出.
有关更多信息,请参阅以下文章:
For more information, please refer to the following articles:
2.数据成员默认值
最好的问候,
这篇关于WCF DataContract属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!