c# - 如何根据需要指定 OperationContract 的参数

标签 c# wcf wsdl operationcontract

我想知道如何根据需要在 WCF 中指定 OperationContract 方法的参数,以便生成的 xsd 包含 minOccurs="1"而不是 minOccurs="0"。

例子:

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

生成这个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 文件中手动修复它。

最佳答案

您可能需要将参数包装在一个类中,然后您可以使用 DataMember 属性并指定 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; }
}

关于c# - 如何根据需要指定 OperationContract 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3406559/

相关文章:

c# - nHibernate - 在选择中按顺序进行子选择

wcf - WCF 4 REST 服务的正确配置是什么?

php - 通过 SOAP 检索数据并将其发送到 MYSQL 数据库

c# - 以编程方式为 WCF (NetTcpBinding) 指定自定义授权

wcf - IIS 7.5 Wcf https WSDL 总是返回空白(错误请求)

java - 如何从 WSDL 生成 EJB

java - Axis 自定义处理程序未针对 wsdl 运行

c# - 使用 DropdownFor 将 null 作为默认值传递

c# - 如何在 C# .net web 开发中使用 skype API

c# - 用一种方法打开文件,用另一种方法写入