阻止ServiceContractGenerator生成消息协定(请求/响应包装器)

bgh*_*bgh 11 c# wcf soap wsdl web-services

有一个特定的WSDL,ServiceContractGenerator会继续生成消息契约(请求/响应包装器对象),这是我不想要的(我想要直接的参数).其他WSDL的工作正常.

当我使用Visual Studio创建WCF客户端("添加服务引用")并单击"高级..."时,"始终生成消息合同"复选框可以正确控制是否生成消息合同对象.

但是,当我使用ServiceContractGenerator类以编程方式生成WCF客户端时,它会不断生成消息协定.我尝试将ServiceContractGenerator的选项设置为ServiceContractGenerationOptions.None,但结果是相同的.

这是我使用的代码:

MetadataSet metadataSet = new MetadataSet();
metadataSet.MetadataSections.Add(MetadataSection.CreateFromServiceDescription(System.Web.Services.Description.ServiceDescription.Read(wsdlStream)));
WsdlImporter importer = new WsdlImporter(metadataSet);
if (serviceDescription != null)
    importer.WsdlDocuments.Add(serviceDescription);
foreach (XmlSchema nextSchema in schemas)
    importer.XmlSchemas.Add(nextSchema);

ServiceContractGenerator generator = new ServiceContractGenerator();
generator.Options = ServiceContractGenerationOptions.None;
foreach (ContractDescription nextContract in importer.ImportAllContracts())
    generator.GenerateServiceContractType(nextContract);
if (generator.Errors.Count != 0)
    throw new Exception("Service assembly compile error: \r\n - " + string.Join("\r\n - ", generator.Errors.Select(e => e.Message)));

// Use generator.TargetCompileUnit to generate the code...
Run Code Online (Sandbox Code Playgroud)

我应该怎么做才能使ServiceContractGenerator使用直接参数生成Web方法?

Iva*_*oev 4

当我使用 Visual Studio 创建 WCF 客户端(“添加服务引用”)并单击“高级...”时,显示“始终生成消息协定”的复选框确实可以正确控制是否生成消息协定对象。

这是不正确的。尝试使用链接中存在问题的 WSDL,您将获得与使用ServiceContractGenerator. 事实上,ServiceContractGenerationOptions.TypedMessages标志(默认关闭)直接对应于上述对话框选项,并用于(打开时)强制创建消息契约。

话虽如此,问题出在 WSDL 中,并在生成的.cs文件中用如下行指示:

// CODEGEN:生成消息契约,因为来自命名空间http://localhost/FinSwitch/ 的元素名称登录未标记为 nillable

这就是问题所在。当方法元素或响应元素包含未用 标记的“对象类型”(字符串、base64Binary 等)成员时,svcutil.exe“添加服务引用”对话框都不会解包方法。ServiceContractGeneratornillable="true"

例如,以下是有问题的 WSDL 的一部分:

<s:element name="DownloadFile">
    <s:complexType>
        <s:sequence>
            <s:element type="s:string" name="login" maxOccurs="1" minOccurs="0"/>
            <s:element type="s:string" name="password" maxOccurs="1" minOccurs="0"/>
            <s:element type="s:string" name="fileType" maxOccurs="1" minOccurs="0"/>
            <s:element type="s:dateTime" name="fileDate" maxOccurs="1" minOccurs="1"/>
            <s:element type="s:boolean" name="onlyDownloadIfFileChanged" maxOccurs="1" minOccurs="1"/>
            <s:element type="s:string" name="companyCode"  maxOccurs="1" minOccurs="0"/>
            <s:element type="s:string" name="category" maxOccurs="1" minOccurs="0"/>
        </s:sequence>
    </s:complexType>
</s:element>

<s:element name="DownloadFileResponse">
    <s:complexType>
        <s:sequence>
            <s:element type="s:base64Binary" name="DownloadFileResult" maxOccurs="1" minOccurs="0"/>
        </s:sequence>
    </s:complexType>
</s:element>
Run Code Online (Sandbox Code Playgroud)

这会产生

// CODEGEN: Generating message contract since element name login from namespace http://localhost/FinSwitch/ is not marked nillable
[System.ServiceModel.OperationContractAttribute(Action="http://localhost/FinSwitch/FinSwitchWebServiceSoap/DownloadFileRequest", ReplyAction="http://localhost/FinSwitch/FinSwitchWebServiceSoap/DownloadFileResponse")]
DownloadFileResponse DownloadFile(DownloadFileRequest request);
Run Code Online (Sandbox Code Playgroud)

加上消息联系人类。

但是,如果我们将其修改为:

<s:element name="DownloadFile">
    <s:complexType>
        <s:sequence>
            <s:element type="s:string" name="login" nillable="true" maxOccurs="1" minOccurs="0"/>
            <s:element type="s:string" name="password" nillable="true" maxOccurs="1" minOccurs="0"/>
            <s:element type="s:string" name="fileType" nillable="true" maxOccurs="1" minOccurs="0"/>
            <s:element type="s:dateTime" name="fileDate" maxOccurs="1" minOccurs="1"/>
            <s:element type="s:boolean" name="onlyDownloadIfFileChanged" maxOccurs="1" minOccurs="1"/>
            <s:element type="s:string" name="companyCode"  nillable="true" maxOccurs="1" minOccurs="0"/>
            <s:element type="s:string" name="category" nillable="true" maxOccurs="1" minOccurs="0"/>
        </s:sequence>
    </s:complexType>
</s:element>

<s:element name="DownloadFileResponse">
    <s:complexType>
        <s:sequence>
            <s:element type="s:base64Binary" name="DownloadFileResult" nillable="true" maxOccurs="1" minOccurs="0"/>
        </s:sequence>
    </s:complexType>
</s:element>
Run Code Online (Sandbox Code Playgroud)

那么生成的代码就是预期的

[System.ServiceModel.OperationContractAttribute(Action="http://localhost/FinSwitch/FinSwitchWebServiceSoap/DownloadFileRequest", ReplyAction="http://localhost/FinSwitch/FinSwitchWebServiceSoap/DownloadFileResponse")]
byte[] DownloadFile(string login, string password, string fileType, System.DateTime fileDate, bool onlyDownloadIfFileChanged, string companyCode, string category);
Run Code Online (Sandbox Code Playgroud)

并且没有消息契约类。

这一切意味着什么?该规则被深深地硬编码在基础设施中(如果有人感兴趣,这里是参考源)并且无法更改。人们可以预处理 WSDL 内容(毕竟,它是 XML)并nillable="true"在需要的地方插入,但我不确定这是否可以被视为正确的操作 - AFAIK,提供正确的 WSDL 是服务提供商的责任,并且没有保证改变它不会产生副作用。