为自定义响应标头创建wsdl

时间:2019-12-17 20:53:24

标签: c# asp.net wcf soap wsdl

我必须调用第三方SOAP Web服务。我正在使用c#,Visual Studio和WCF。供应商无法为我提供wsdl,因此我自己编写,然后使用我创建的wsdl添加服务引用。

这是一个示例请求:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
  <s:Header>
    <AuthHeader xmlns="http://sample.com/">
      <Username>...</Username>
      <Password>...</Password>
    </AuthHeader>
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <GetAttachment xmlns="http://sample.com/">
      <AttachmentID >4851888</AttachmentID>
    </Get>
  </s:Body>
</s:Envelope>

以下是示例回复:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <StatusType xmlns="http://sample.com/">
      <StatusNumber>0</StatusNumber>
      <Description>Success</Description>
    </StatusType>
  </soap:Header>
  <soap:Body>
    <GetAttachmentResponse xmlns="http://sample.com/">
      {
      ..json content
      }
    </GetAttachmentResponse>
  </soap:Body>
</soap:Envelope>

我创建的wsdl如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions 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:tns="http://sample.com/"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:s="http://www.w3.org/2001/XMLSchema"
                  xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
                  xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
                  targetNamespace="http://sample.com/"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
    <wsdl:types>
      <s:schema elementFormDefault="qualified" targetNamespace="http://sample.com/">

      <s:element name="AuthHeader" type="tns:AuthHeader" />      
      <s:complexType name="AuthHeader">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="1" name="Username" type="s:string" />
          <s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string" />
        </s:sequence>
        <s:anyAttribute />
      </s:complexType>

      <s:element name="GetAttachment">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="1" maxOccurs="1" name="AttachmentID" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>

      <s:element name="StatusHeader" type="tns:StatusHeader" />
      <s:complexType name="StatusHeader">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="1" name="StatusNumber" type="s:string" />
          <s:element minOccurs="0" maxOccurs="1" name="Description" type="s:string" />
        </s:sequence>
        <s:anyAttribute />
      </s:complexType>

      <s:element name="GetAttachmentResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="1" maxOccurs="1" name="Attachment">
              <s:complexType>
                <s:sequence>
                  <s:element minOccurs="0" maxOccurs="1" name="mimetype" type="s:string" />
                  <s:element minOccurs="0" maxOccurs="1" name="filename" type="s:string" />
                  <s:element minOccurs="0" maxOccurs="1" name="content" type="s:base64Binary" />
                  <s:element minOccurs="0" maxOccurs="1" name="description" type="s:string" />                  
                </s:sequence>
              </s:complexType>
            </s:element>
          </s:sequence>
        </s:complexType>
      </s:element>

        </s:schema>
    </wsdl:types>

  <wsdl:message name="GetAttachmentSoapIn">
    <wsdl:part name="parameters" element="tns:GetAttachment" />
  </wsdl:message>

  <wsdl:message name="GetAttachmentSoapOut">
    <wsdl:part name="response" element="tns:GetAttachmentResponse" />
  </wsdl:message>

  <wsdl:message name="GetAttachmentAuthenticationHeader">
    <wsdl:part name="AuthenticationHeader" element="tns:AuthHeader" />
  </wsdl:message>

  <wsdl:message name="GetAttachmentStatusHeader">
    <wsdl:part name="StatusHeader" element="tns:StatusHeader" />
  </wsdl:message>


    <wsdl:portType name="AttachmentsSOAP">
        <wsdl:operation name="GetAttachment">
            <wsdl:input message="tns:GetAttachmentSoapIn"/>
            <wsdl:output message="tns:GetAttachmentSoapOut"/>
        </wsdl:operation>
    </wsdl:portType>

    <wsdl:binding name="AttachmentsSOAP" type="tns:AttachmentsSOAP">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="GetAttachment">
            <soap:operation soapAction=""/>
      <wsdl:input>
        <soap:body use="literal" />
        <soap:header message="tns:GetAttachmentAuthenticationHeader" part="AuthenticationHeader" use="literal" />
      </wsdl:input>
            <wsdl:output>
                <soap:body use="literal"/>
        <soap:header message="tns:GetAttachmentStatusHeader" part="StatusHeader" use="literal" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>

    <wsdl:service name="ApplyV1">
        <wsdl:port name="AttachmentsSOAP" binding="tns:AttachmentsSOAP">
            <soap:address location="https://api.sample.com/service"/>
        </wsdl:port>    
    </wsdl:service>

</wsdl:definitions>

当我添加服务引用时,生成的代理类将包含我期望的GetAttachment函数。问题是响应头作为函数返回类型返回,而肥皂包的实际响应(主体)作为out参数返回:

public AttachmentAPI.StatusHeader GetAttachment(AttachmentAPI.AuthHeader AuthHeader, AttachmentAPI.GetAttachment GetAttachment1, out AttachmentAPI.GetAttachmentResponse GetAttachmentResponse) {...}

我可以调用GetAttachment函数,它可以正确地进行肥皂呼叫。 soap服务返回一个结果,该结果反序列化到GetAttachmentResponse对象中,但不反序列化到StatusHeader对象中。理想情况下,签名应类似于...

public AttachmentAPI.GetAttachmentResponse GetAttachment(AttachmentAPI.AuthHeader AuthHeader, AttachmentAPI.GetAttachment GetAttachment) {...}

...其中,AttachmentAPI.GetAttachmentResponse包含响应正文和自定义响应标头。任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

大多数情况下,我通过在消息中放入正确的部分并从portType和绑定中引用消息来解决此问题。这确实起作用,尽管它仍然使Visual Studio生成具有我认为不受欢迎的函数签名的代理类。从函数返回标头对象,并将响应主体反序列化为的对象作为out参数返回:

public AttachmentAPI.StatusResponseHeaderType GetAttachment(AttachmentAPI.AuthRequestHeaderType AuthHeader, AttachmentAPI.GetAttachment GetAttachment1, out AttachmentAPI.GetAttachmentResponse GetAttachmentResponse) {...}

但是至少它能正常工作,我可以检索Status标头和实际的正文内容。

这是我修改过的wsdl:

<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions 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:tns="http://sample.com/"
                    xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                    xmlns:s="http://www.w3.org/2001/XMLSchema"
                  xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/"
                  xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
                    targetNamespace="http://sample.com/"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
  <wsdl:types>
    <s:schema elementFormDefault="qualified" targetNamespace="http://sample.com/">

      <s:element name="AuthHeader" type="tns:AuthRequestHeaderType" />

      <s:complexType name="AuthRequestHeaderType">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="1" name="Username" type="s:string" />
          <s:element minOccurs="0" maxOccurs="1" name="Password" type="s:string" />
        </s:sequence>
        <s:anyAttribute />
      </s:complexType>

      <s:element name="StatusType" type="tns:StatusResponseHeaderType" />

      <s:complexType name="StatusResponseHeaderType">
        <s:sequence>
          <s:element minOccurs="0" maxOccurs="1" name="StatusNumber" type="s:string" />
          <s:element minOccurs="0" maxOccurs="1" name="Description" type="s:string" />
        </s:sequence>
        <s:anyAttribute />
      </s:complexType>


      <s:element name="GetAttachment">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="1" maxOccurs="1" name="AttachmentID" type="s:string" />
          </s:sequence>
        </s:complexType>
      </s:element>

      <s:element name="GetAttachmentResponse">
        <s:complexType>
          <s:sequence>
            <s:element minOccurs="1" maxOccurs="1" name="Attachment">
              <s:complexType>
                <s:sequence>
                  <s:element minOccurs="0" maxOccurs="1" name="mimetype" type="s:string" />
                  <s:element minOccurs="0" maxOccurs="1" name="filename" type="s:string" />
                  <s:element minOccurs="0" maxOccurs="1" name="content" type="s:base64Binary" />
                  <s:element minOccurs="0" maxOccurs="1" name="description" type="s:string" />
                </s:sequence>
              </s:complexType>
            </s:element>
          </s:sequence>
        </s:complexType>
      </s:element>

    </s:schema>
  </wsdl:types>

  <wsdl:message name="GetAttachmentSoapIn">
    <wsdl:part name="AuthenticationHeader" element="tns:AuthHeader" />
    <wsdl:part name="parameters" element="tns:GetAttachment"   />
  </wsdl:message>

  <wsdl:message name="GetAttachmentSoapOut">
    <wsdl:part name="StatusHeader" element="tns:StatusType" />
    <wsdl:part name="response" element="tns:GetAttachmentResponse" />
  </wsdl:message>

  <!--<wsdl:message name="GetAttachmentAuthenticationRequestHeaderMessage">
    <wsdl:part name="AuthenticationHeader" element="tns:AuthHeader" />
  </wsdl:message>

  <wsdl:message name="GetAttachmentStatusResponseHeaderMessage">
    <wsdl:part name="StatusHeader" element="tns:StatusHeader" />
  </wsdl:message>-->

  <!-- PortType defines the abstract interface of a web service. 
       Port type is implemented by the binding and service elements -->
  <wsdl:portType name="AttachmentsSoap">
    <wsdl:operation name="GetAttachment">
      <wsdl:input message="tns:GetAttachmentSoapIn"/>
      <wsdl:output message="tns:GetAttachmentSoapOut"/>
    </wsdl:operation>
  </wsdl:portType>

  <!-- the binding specifies concrete implementation details and 
        essentially maps a portType to a set of protocols (HTTP and SOAP) 
        message styles (Document/RPC) and encodings (literal) -->
  <wsdl:binding name="AttachmentsSoap" type="tns:AttachmentsSoap">
    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
    <wsdl:operation name="GetAttachment">
      <soap:operation soapAction=""/>
      <wsdl:input>
        <soap:header message="tns:GetAttachmentSoapIn" part="AuthenticationHeader" use="literal" />
        <soap:body use="literal" parts="parameters" />
      </wsdl:input>
      <wsdl:output>
        <soap:header message="tns:GetAttachmentSoapOut" part="StatusHeader" use="literal" />
        <soap:body use="literal" parts="response"/>
      </wsdl:output>
    </wsdl:operation>
  </wsdl:binding>

  <wsdl:service name="ApplyV1">
    <wsdl:port name="AttachmentsSoap" binding="tns:AttachmentsSoap">
      <soap:address location="https://api.sample.com/soap/apply/v1"/>
    </wsdl:port>
  </wsdl:service>

</wsdl:definitions>

答案 1 :(得分:0)

你是如此坚强,坚韧。我很欣赏您解决问题的能力。我对WSDL不太了解,更不用说能够手动编写WSDL了。我认为,只有我们附加用户名/密码令牌才能解决身份验证问题。
从客户端请求的格式来看,我认为我们可以使用以下绑定定义WCF客户端。

<customBinding>
   <binding name="mybinding">
     <textMessageEncoding messageVersion="Soap12WSAddressing10">
     </textMessageEncoding>
     <security authenticationMode="UserNameOverTransport" includeTimestamp="false" >
     </security>
     <httpsTransport></httpsTransport>
   </binding>
 </customBinding>

如果服务器通过HTTP协议工作,请用httpstransport替换httpTransport
然后我们可以构造客户请求。

Uri uri = new Uri("https://abcd:8008/service");
            BindingElementCollection bec = new BindingElementCollection();
            bec.Add(SecurityBindingElement.
                CreateUserNameOverTransportBindingElement());
            //for http server with a certificate.
            //bec.Add(SecurityBindingElement.CreateUserNameForCertificateBindingElement());
            bec.Add(new TextMessageEncodingBindingElement());
            bec.Add(new HttpsTransportBindingElement());
            CustomBinding binding = new CustomBinding(bec);
            ChannelFactory<IService1> channelFactory = new ChannelFactory<IService1>(binding, new EndpointAddress(uri));
            IService1 service = channelFactory.CreateChannel();
            var result=service.myoperation()

请随时让我知道问题是否仍然存在。