【问题标题】:C# SOAP Service - Remove method node and have header in soapheader nodeC# SOAP 服务 - 删除方法节点并在soapheader 节点中有标头
【发布时间】:2019-02-08 21:02:30
【问题描述】:

我的任务是创建一个符合特定 wsdl 的 Web 服务,我之前没有使用过 SOAP 或 asmx。

当我在 SoapUI 中创建请求时,我得到以下结构,这与客户端用于发送请求的结构相同。 (使用占位符名称)

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:par="http://www.foo.com/schemas/method">
   <soapenv:Header>
      <par:SOAPHeaderRequest>
         <par:ApplicationID>ID</par:ApplicationID>
      </par:SOAPHeaderRequest>
   </soapenv:Header>
   <soapenv:Body>
      <par:Body>
      </par:Body>
   </soapenv:Body>
</soapenv:Envelope>

但是,当我尝试创建服务时,我有这样的结构:

<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:Body>
    <Method xmlns="http://www.foo.com/schemas/method">
      <request>
        <SOAPHeaderRequest>
          <ApplicationID>string</ApplicationID>
        </SOAPHeaderRequest>
        <body>
          <Property>string</Property>
        </body>
      </request>
    </Method>
  </soap:Body>
</soap:Envelope>

我想知道如何删除 Method 节点包装器,以及如何将 SOAPHeaderREquest 移动到 soap:Header 中。

这是我的示例代码:

接口和对象

 [ServiceContract(Namespace = "http://www.foo.com/schemas/method")]
 public interface IServiceContract
 {
     [XmlSerializerFormat]
     [OperationContract]
     ResponseObject Method(RequestObject request);
 }

 [System.Serializable()]
 [System.Xml.Serialization.XmlType(AnonymousType = true, Namespace = "http://www.foo.com/schemas/method")]
 [MessageContract(IsWrapped = false)]
 public class RequestObject
 {
     [System.ServiceModel.MessageHeader(Namespace = "http://www.foo.com/schemas/method")]
     public SOAPHeaderRequest SOAPHeaderRequest;

     [System.ServiceModel.MessageBodyMember(Namespace = "http://www.foo.com/schemas/method", Order = 0)]
     public Body body;

     public RequestObject()
     {
     }

     public RequestObject(SOAPHeaderRequest SOAPHeaderRequest, Body body)
     {
         this.body = body;
     }
 }

 [System.Serializable()]
 [System.Xml.Serialization.XmlType(AnonymousType = true, Namespace = "http://www.foo.com/schemas/method")]
 [MessageContract(IsWrapped = false)]
 public class ResponseObject
 {
     [System.ServiceModel.MessageHeader(Namespace = "http://www.foo.com/schemas/method")]
     public SOAPHeaderResponse SOAPHeaderResponse;

     [System.ServiceModel.MessageBodyMember(Namespace = "http://www.foo.com/schemas/method", Order = 0)]
     public Body body;
 }


 [System.Serializable()]
 public class Body
 {
     public string Property { get; set; }
 }

asmx

[WebService(Namespace = "http://www.foo.com/schemas/method")]
[WebServiceBinding(ConformsTo = WsiProfiles.None)]
public class M5NapaPartUpdateService : WebService, IServiceContract
{
    [WebMethod]
    [SoapMethod(SoapAction = "method")]
    public ResponseObject Method(RequestObject request)
    {
        return new ResponseObject();
    }
}

如果您还有其他需要,请告诉我。

谢谢!

【问题讨论】:

    标签: c# soap asmx


    【解决方案1】:

    WSDL 区分两种消息样式: 文档和 RPC。

    消息样式影响 SOAP Body 的内容: 文档样式: SOAP 主体包含一个或多个称为部件的子元素。正文包含的内容没有 SOAP 格式规则;它包含发送方和接收方同意的任何内容。

    **RPC 风格:**RPC 暗示 SOAP 主体包含一个元素,该元素带有被调用的方法或操作的名称。该元素又包含该方法/操作的每个参数的元素。

    您的 wsdl 是用 Document Literal 样式编写的。

    如果你使用的是服务契约,那么我相信你是在使用 WCF 框架来编写服务代码。

    您可以指定以下参数以按照您的预期制作 WSDL。

     [ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples"), XmlSerializerFormat(Style = OperationFormatStyle.Document, 
                                 Use = OperationFormatUse.Literal)]
    

    参考-https://www.ibm.com/support/knowledgecenter/en/SSB27H_6.2.0/fa2ws_ovw_soap_syntax_lit.html

    希望这会有所帮助。

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多