c# - 防止 ServiceContractGenerator 生成消息契约(请求/响应包装器)

标签 c# web-services wcf soap wsdl

有一个specific WSDL为此,ServiceContractGenerator 不断生成消息契约(Contract)(请求/响应包装器对象),这是我不想要的(我想要直接参数)。其他 WSDL 工作正常。

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

但是,当我使用 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...

我应该怎么做才能让 ServiceContractGenerator 生成带有直接参数的 web 方法?

最佳答案

When I use Visual Studio to create a WCF client ("Add Service Reference") and I click on "Advanced...", the checkbox which says "Always generate message contracts" does properly control whether the message contract objects are generated.

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

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

// CODEGEN: Generating message contract since element name login from namespace http://localhost/FinSwitch/ is not marked nillable

这就是问题所在。当方法元素或响应元素包含“对象类型”(字符串、base64Binary 等)时,svcutil.exe、“添加服务引用”对话框和ServiceContractGenerator 都不会打开方法) 没有用 nillable="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>

产生

// 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);

加上消息联系人类。

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

<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>

然后生成的代码如预期的那样

[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);

并且没有消息契约类。

这是什么意思?该规则被硬编码在基础设施中(如果有人感兴趣,here 是引用来源)并且不能更改。可以预处理 WSDL 内容(毕竟,它是 XML)并在需要的地方插入 nillable="true",但我不确定这是否可以被认为是正确的操作 - AFAIK,这是服务提供商有责任提供正确的 WSDL,并且不保证更改它不会导致副作用。

关于c# - 防止 ServiceContractGenerator 生成消息契约(请求/响应包装器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27358165/

相关文章:

javascript - 使用javascript获取数据

iphone - 使用 MySQL 的 CoreData Web 服务

json - Web 服务 C# JSON 到 Arduino

iphone - WCF 日期时间到 NSDate

wcf - 如何在 Azure 中的 Web 角色的外部和内部终结点上托管 WCF 服务?

c# - c#中如何通过contain进行剪切

c# - 如何检测用户是否离开键盘?

WCF + SSL,获取方法调用的空结果

c# - 如何在 GAC 中注册 .NET DLL 文件?

c# - 将时间跨度格式化为 TotalMilliseconds