我使用[DataMember(Isrequired = true)]来生成所需的DataContract属性.对于OperationContract参数似乎没有一些Isrequired.如何使它们成为必需而不允许null?
在SoapUI工具中,OperationContract中的参数似乎是可选的.虽然这绝不是可选的或null.
WCF接口:
[OperationContract] IsClientUpdaterequiredResult IsClientUpdaterequired(IsClientUpdaterequiredInput versie); ... [DataContract] public class IsClientUpdaterequiredInput { [DataMember(Isrequired=true)] public string clientName { get; set; } [DataMember(Isrequired = true,Order = 0)] public int major { get; set; } [DataMember(Isrequired = true,Order = 1)] public int minor { get; set; } [DataMember(Isrequired = true,Order = 2)] public int build { get; set; } [DataMember(Isrequired = true,Order = 3)] public int revision { get; set; } }
soapUI请求模板:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:tem="http://tempuri.org/" xmlns:pir="http://schemas.datacontract.org/2004/07/PirIS.Web.WCF.InputClasses"> <soap:Header/> <soap:Body> <tem:IsClientUpdaterequired> <!--Optional:--> <tem:versie> <pir:clientName>?</pir:clientName> <pir:major>?</pir:major> <pir:minor>?</pir:minor> <pir:build>?</pir:build> <pir:revision>?</pir:revision> </tem:versie> </tem:IsClientUpdaterequired> </soap:Body> </soap:Envelope>
解决方法
不幸的是,使用默认WCF无法完成.有一些解决方法:
>自定义RequiredParametersBehavior
属性
>使用企业库和associate a ruleset to your method中的Validation Application Block
但是,您可以实现FaultContract并在输入参数为null时抛出错误.