C# Soap WebService SoapHeader doesn't appear in response

1.9k views Asked by At

I have a Soap WebService with some WebMethods.

Some of this WebMethods receive input parameters and send an output value, but also send and custom class in its header (or at least that is what i want).

I read about SOAP Header and at some point i had a method working with a costum class in both request and response headers.

Not sure what I've done but now the code is not working.

NOTE: I'm using SOAP UI to test.

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

Here goes a test:

REQUEST

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

RESPONSE

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

If i check the header tab in SOAP UI there is no NPUHeader object

Header Response Data

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
1

There are 1 answers

0
FEST On

Found the solution reviewing the MSN doc again : https://msdn.microsoft.com/en-US/library/8728chd5(v=vs.80).aspx

The thing is, when we want something to travel as a SOAPHeader we need to do 3 things:

  1. Create the class extendind SOAPHeader
  2. Create a public attribute in the webservice of the class
  3. Tag the webmethod with it (

    [SoapHeader([attribute_name], Direction = SoapHeaderDirection.[choose one])]

When I first made the try I had it all, but then, since i wanted to receive the object as a parameter and use it in the header on response I removed the public attribute thinking the passed object would be used.

It seems that it can't work that way so the solution was:

  1. Make sure I have the 3 steps referenced above
  2. Make sure the passed parameter and the attribute won't conflit in name
  3. (In my case) Copy the necessary attributes of the given parameter to the attribute

Follows the code for my specific case but should help anyone facing similiar problem:

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 REQUEST

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

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