如何强制WCF使用所需的方法参数(minoccurs ="1")自动生成WSDL?

Dav*_*ney 16 wcf soa soap wsdl

在使用WCF和OperationContracts时,我定义了以下方法:

    [OperationContract]
    [FaultContract(typeof(ValidationFault))]
    [FaultContract(typeof(FaultException<ExceptionDetail>))]
    int DoSomething(int someId, MyComplexType messageData);
Run Code Online (Sandbox Code Playgroud)

当它由WCF运行时转换为WSDL时,最终会为参数someId和messageData列出minoccurs ="0"(如果缺少这些参数,则会抛出运行时错误).

如果我使用SoapUI生成代理,我会得到如下所示的内容:

  <com:DoSomething>
     <!--Optional-->
     <com:EventId>1</com:EventId>
     <!--Optional-->
     <com:myComplexType >
        <com:id>1</com:id>
     </com:myComplexType >
  </com:DoSomething>
Run Code Online (Sandbox Code Playgroud)

MyComplexType中的id字段使用IsRequired ="true"标记为DataMemeber属性,因此公开为必需.

显然,WSDL指定参数是不可选的是可选的,但是我看不到任何明显的方法来标记OperationContract以迫使WCF根据需要识别和公开这些参数.

我有点困惑,似乎没有明显的方法这样做(阅读intellisense/msdn/google).或者我会失明并忽略一些明显的东西.

有线索吗?

Tho*_*rin 18

我刚刚写了一篇关于这个主题的博客文章,因为我上周遇到了这个问题.它解释了如何修改WCF在运行时生成的元数据.

除了下载源文件之外,您只需要在合同定义中添加一个属性.像这样:

[ServiceContract]
[RequiredParametersBehavior]
public interface ICalculatorService
{
    [OperationContract]
    int Add(int firstValue, int secondValue);
}
Run Code Online (Sandbox Code Playgroud)

以下是更详细解释它的博客文章:使用WCF控制WSDL minOccurs