我想知道如何根据需要在 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/