Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF服务+;SvcUtil生成意外的对象结构_C#_Wcf_Soap_Wsdl_Svcutil.exe - Fatal编程技术网

C# WCF服务+;SvcUtil生成意外的对象结构

C# WCF服务+;SvcUtil生成意外的对象结构,c#,wcf,soap,wsdl,svcutil.exe,C#,Wcf,Soap,Wsdl,Svcutil.exe,我正在尝试创建一个WCF应用程序,它将侦听来自供应商系统的请求。供应商向我提供了一个WSDL,因此我需要创建一个服务并向他们公开其端点 我曾使用SvcUtil.exe生成C#类,但它输出的类型非常奇怪 这是提供给我的WSDL的一个片段: <?xml version="1.0" encoding="utf-8"?> <wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http

我正在尝试创建一个WCF应用程序,它将侦听来自供应商系统的请求。供应商向我提供了一个WSDL,因此我需要创建一个服务并向他们公开其端点

我曾使用SvcUtil.exe生成C#类,但它输出的类型非常奇怪

这是提供给我的WSDL的一个片段:

<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified">
      <s:element name="Submit">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="Incident">
              <s:complexType>
                <s:sequence>
                  <s:element minOccurs="0" maxOccurs="1" form="unqualified" name="TransactionId" type="s:string" />
                  <s:element minOccurs="0" maxOccurs="1" form="unqualified" name="TransactionType" type="s:string" />
                  <s:element minOccurs="0" maxOccurs="1" form="unqualified" name="TransactionSubType" type="s:string" />
                  <s:element minOccurs="0" maxOccurs="unbounded" form="unqualified" name="ConfigurationItem">
                    <s:complexType>
                      <s:sequence>
                        <s:element minOccurs="0" maxOccurs="1" form="unqualified" name="AssetTag" type="s:string" />
                        <s:element minOccurs="0" maxOccurs="1" form="unqualified" name="Name" type="s:string" />
                    ....
我希望这会创建一个这样的结构:

class Submit { ... }
class Incident { ... }
class ConfigurationItem { ... }
因此,当它被序列化时,我得到如下结果:

<Submit>
    <Incident>
        <TransactionId>12345</TransactionId>
        <TransactionType>12345</TransactionType>
        <TransactionSubType>12345</TransactionSubType>
        <ConfigurationItem>
            <AssetTag>xyz</AssetTag>
            <Name>name</Name>
        </ConfigurationItem>
    </Incident>
</Submit>
它似乎将Submit和Incident对象组合成一个,并且在每个嵌套元素上预先添加了“SubmitIncident”。因此,当序列化时,我得到以下结果:

<SubmitIncident>
    <TransactionId>...</TransactionId>
    <TransactionType>...</TransactionType>
    <TransactionSubType>...</TransactionSubType>
    <SubmitIncidentConfigurationItem>
        <AssetTag>...</AssetTag>
        <Name>...</Name>
    </SubmitIncidentConfigurationItem>
</SubmitIncident>

...
...
...
...
...

这会导致供应商(我的服务与他们的WSDL不匹配,因此他们无法与我交谈)和我在应用程序中进行的后续处理出现问题。有人能帮我理解为什么会发生这种情况,如何阻止它,并让svcuti正确输出。

事实证明,类定义和定义并不是这里真正的问题(它确实会在以后引起另一个问题,但一旦您了解了发生了什么,这也很容易解决)。这个问题源于我在一次性开发阶段所做的一些事情

我做的第一件事就是使用供应商的WSDL使用
svcutil.exe
生成服务器类。一旦我有足够的时间开始调试/测试,我自然会走向WCF测试客户端和VisualStudio的内置开发服务器。在使用客户机时,它不断出现各种模糊的错误,例如不支持https、合同和绑定问题等。其中许多是配置不一致,因此通过一点谷歌搜索来修复,但其中的错误是:

WCF测试客户端不支持该操作,因为它使用类型SubmitIncident

当任何
SubmitIncident
的属性类型为
object
(即使嵌套在其他属性的较低位置)时,就会发生这种情况。基本上,我只是一次处理一个错误,从中提取出直到最后才起作用的部分我主要是添加和删除属性和类的属性,例如DataMember、SoapAction、Namespace等。

最终,我能够使用WCF测试客户端成功地调用应用程序中的端点。然而,当涉及到与之通信的其他应用程序时,按照最初的问题,它是失败的。我为使其在WCF测试客户机中工作所做的更改实际上使服务对原始WSDL模式无效

TL;博士

不要使用WCF测试客户端构建WCF应用程序来测试它们。这一点我怎么强调都不过分。它迫使您删除会导致应用程序在正常使用中中断的内容。即使在早期开发阶段,也要使用行业标准的应用程序,如SoapUI

有用的链接


那么,有什么方法可以保留原始的wsdl层次结构,使用正确的类型命名,并且没有类型名称作为前缀?
class SubmitIncident { ... }
class SubmitIncidentConfigurationItem { ... }
<SubmitIncident>
    <TransactionId>...</TransactionId>
    <TransactionType>...</TransactionType>
    <TransactionSubType>...</TransactionSubType>
    <SubmitIncidentConfigurationItem>
        <AssetTag>...</AssetTag>
        <Name>...</Name>
    </SubmitIncidentConfigurationItem>
</SubmitIncident>