簡體   English   中英

C#Soap WebService SoapHeader沒有出現在響應中

[英]C# Soap WebService SoapHeader doesn't appear in response

我有一個帶有某些WebMethod的Soap WebService。

其中一些WebMethods接收輸入參數並發送輸出值,但它的標頭中還包含send和custom類(或者至少是我想要的)。

我閱讀了有關SOAP Header的文章,在某種程度上,我有一個在請求和響應頭中都使用costum類的方法。

不知道我做了什么,但是現在代碼無法正常工作。

注意:我正在使用SOAP UI進行測試。

[SoapHeader("npuHeader", Direction = SoapHeaderDirection.Out)]
    public string obterSiteDocumentacaoUrl(NpuHeader npuHeader, string pedido)
    {
        string url = null;

        if (validaNpuHeader(ref npuHeader))
        {
            url = dataAccess.obterSiteDocumentacaoUrl(pedido);
        }

        npuHeader.correlationNPU = npuHeader.npu;
        npuHeader.npu = CreateNPU("", "");
        npuHeader.systemCode = SistemaOrigem;
        npuHeader.creationTime = DateTime.Now;
        npuHeader.operationDate = DateTime.Now;

        return url;
    }

[Serializable]
public class NpuHeader : SoapHeader
{

    public NpuHeader() { }

    public string npu { get; set; }
    public string correlationNPU { get; set; }
    public string systemCode { get; set; }
    public DateTime creationTime { get; set; }
    public DateTime operationDate { get; set; }
    public List<GeneralResponseSuccess> responseSuccess { get; set; }
}
[Serializable]
public class GeneralResponseSuccess
{
    public string errorCode { get; set; }
    public string message { get; set; }
    public string description { get; set; }

    public GeneralResponseSuccess() { }
    public GeneralResponseSuccess(string errorCode, string message, string description)
    { this.errorCodeField = errorCode; this.messageField = message; this.descriptionField = description; }
    public GeneralResponseSuccess(WebServiceBusinessResult error, string description)
    {
        this.errorCode = error.errorCode;
        this.message = error.message;
        this.description = description;
    }
}

這里進行測試:

請求

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:obterSiteDocumentacaoUrl>
         <tem:npuHeader>
           <tem:npu>12345678901234567890123456789012345678901234567890</tem:npu>
            <tem:systemCode>0253</tem:systemCode>
            <tem:creationTime>2015-06-17T00:00:00</tem:creationTime>
            <tem:operationDate>2015-06-17T00:00:00</tem:operationDate>
         </tem:npuHeader>
         <tem:pedido>11SEB9999</tem:pedido>
      </tem:obterSiteDocumentacaoUrl>
   </soapenv:Body>
</soapenv:Envelope>

響應

   <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <obterSiteDocumentacaoUrlResponse xmlns="http://tempuri.org/">
         <obterSiteDocumentacaoUrlResult>www.google.com</obterSiteDocumentacaoUrlResult>
      </obterSiteDocumentacaoUrlResponse>
   </soap:Body>
</soap:Envelope>

如果我在SOAP UI中檢查標題選項卡,則沒有NPUHeader對象

標頭響應數據

X-AspNet-Version : 2.0.50727
Date : Mon, 22 Jun 2015 13:53:18 GMT
Content-Length : 422
#status# : HTTP/1.1 200 OK
Content-Type : text/xml; charset=utf-8
Connection : Close
Server : ASP.NET Development Server/9.0.0.0
Cache-Control : private, max-age=0

找到了再次查看MSN文檔的解決方案: https : //msdn.microsoft.com/zh-CN/library/8728chd5(v=vs.80).aspx

問題是,當我們希望某些東西作為SOAPHeader傳遞時,我們需要做三件事:

  1. 創建類extendind SOAPHeader
  2. 在該類的Web服務中創建一個公共屬性
  3. 用它標記網絡方法(

    [SoapHeader([attribute_name],Direction = SoapHeaderDirection。[選擇一項])]

當我第一次嘗試時,我擁有了全部,但是隨后,由於我想接收對象作為參數並在響應的標頭中使用它,因此我刪除了公共屬性,以為將使用傳遞的對象。

似乎不能這樣工作,所以解決方案是:

  1. 確保我有上面提到的3個步驟
  2. 確保傳遞的參數和屬性不會名稱沖突
  3. (以我為例)將給定參數的必要屬性復制到該屬性

遵循我的特定情況的代碼,但應該可以幫助遇到類似問題的任何人:

public NpuHeader npuHeaderOut = new NpuHeader();
...
[SoapHeader("npuHeaderOut", Direction = SoapHeaderDirection.Out)]
public string obterSiteDocumentacaoUrl(NpuHeader npuHeader, string pedido)
{
    string url = null;

    if (validaNpuHeader(ref npuHeader))
    {
        try
        {
            url = dataAccess.obterSiteDocumentacaoUrl(pedido);

            npuHeader.ResponseSuccess.Add(
                    new GeneralResponseSuccess(WebServiceBusinessResult.SUCESSO, "Sucesso")
                    );
        }
        catch (Exception ex)
        {
            npuHeader.ResponseSuccess.Add(
                new GeneralResponseSuccess(WebServiceBusinessResult.OUTROS, ex.Message)
            );
        }
    }

    npuHeaderOut.ResponseSuccess = npuHeader.ResponseSuccess;
    npuHeaderOut.correlationNPU = npuHeader.npu;
    npuHeaderOut.npu = CreateNPU("", "");
    npuHeaderOut.systemCode = SistemaOrigem;
    npuHeaderOut.creationTime = DateTime.Now;
    npuHeaderOut.operationDate = DateTime.Now;

    return url;
}

SOAP請求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">
   <soapenv:Header/>
   <soapenv:Body>
      <tem:obterSiteDocumentacaoUrl>
         <tem:npuHeader>
            <tem:npu>12345678901234567890123456789012345678901234567890</tem:npu>
            <tem:systemCode>0253</tem:systemCode>
            <tem:creationTime>2015-06-17T00:00:00</tem:creationTime>
            <tem:operationDate>2015-06-17T00:00:00</tem:operationDate>
         </tem:npuHeader>
         <tem:pedido>11SEB9999</tem:pedido>
      </tem:obterSiteDocumentacaoUrl>
   </soapenv:Body>
</soapenv:Envelope>

SOAP響應

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Header>
      <NpuHeader xmlns="http://tempuri.org/">
         <npu>0253201506250954188630000000000000000000</npu>
         <correlationNPU>12345678901234567890123456789012345678901234567890</correlationNPU>
         <systemCode>253</systemCode>
         <creationTime>2015-06-25T09:54:18.8632144+01:00</creationTime>
         <operationDate>2015-06-25T09:54:18.8632144+01:00</operationDate>
         <ResponseSuccess>
            <GeneralResponseSuccess>
               <errorCode>EPVSEB000</errorCode>
               <message>Sucesso</message>
               <description>Sucesso</description>
            </GeneralResponseSuccess>
         </ResponseSuccess>
      </NpuHeader>
   </soap:Header>
   <soap:Body>
      <obterSiteDocumentacaoUrlResponse xmlns="http://tempuri.org/">
         <obterSiteDocumentacaoUrlResult>www.google.com</obterSiteDocumentacaoUrlResult>
      </obterSiteDocumentacaoUrlResponse>
   </soap:Body>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM