本文介绍了如何根据需要指定OperationContract的的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这样生成的XSD包含的minOccurs =1代替的minOccurs =0。

I wonder how I can specify a parameter of an OperationContract method in WCF as required so that the generated xsd contains minOccurs="1" instead of minOccurs="0".

例如:

[ServiceContract(Namespace = "http://myUrl.com")]  
public interface IMyWebService  
{  
   [OperationContract]  
   string DoSomething(string param1, string param2, string param3);  
}



生成此XSD:

generates this xsd:

<xs:element name="DoSomething">  
  <xs:complexType>  
    <xs:sequence>  
      <xs:element minOccurs="0" name="param1" nillable="true" type="xs:string" />  
      <xs:element minOccurs="0" name="param2" nillable="true" type="xs:string" />  
      <xs:element minOccurs="0" name="param3" nillable="true" type="xs:string" />  
    </xs:sequence>  
  </xs:complexType>  



不过,我想定义的minOccurs =1而不必手动修复它在XSD文件中的代码中。

But I want to define minOccurs="1" within the code without the necessity to manually fix it in the xsd file.

推荐答案

您可能需要换你在类的参数,那么你可以使用数据成员属性,并指定 IsRequired = TRUE

You might need to wrap your parameters in a class, then you can use the DataMember attribute and specify IsRequired=true:

[ServiceContract(Namespace = "http://myUrl.com")]  
public interface IMyWebService  
{  
   [OperationContract]  
   string DoSomething(RequestMessage request);  
}

[DataContract]
public class RequestMessage
{
   [DataMember(IsRequired = true)]
   public string param1 { get; set; }

   [DataMember(IsRequired = true)]
   public string param3 { get; set; }

   [DataMember(IsRequired = true)]
   public string param3 { get; set; }
}

这篇关于如何根据需要指定OperationContract的的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 07:54