C#Soap WebService SoapHeader没有出现在响应中

时间:2015-06-22 14:05:04

标签: c# soapui soapheader

我有一个带有一些WebMethods的Soap WebService。

这些WebMethods中的一些接收输入参数并发送输出值,但也在其标题中发送和自定义类(或者至少这是我想要的)。

我读到了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;
    }
}

这是一个测试:

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>

如果我检查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

1 个答案:

答案 0 :(得分:0)

找到解决方案再次审核MSN文档:https://msdn.microsoft.com/en-US/library/8728chd5(v=vs.80).aspx

问题是,当我们想要一些东西作为SOAPHeader旅行时,我们需要做三件事:

  1. 创建类extendind SOAPHeader
  2. 在班级
  3. 的网络服务中创建公共属性
  4. 用它标记网络方法(

    [SoapHeader([attribute_name],Direction = SoapHeaderDirection。[选择一个])]

  5. 当我第一次尝试时,我已经完成了所有操作,但是之后,因为我想接收对象作为参数并在响应中使用它,我删除了公共属性,认为将使用传递的对象。

    似乎无法以这种方式工作所以解决方案是:

    1. 确保我有上面提到的3个步骤
    2. 确保传递的参数和属性不会在名称
    3. 中混淆
    4. (在我的例子中)将给定参数的必要属性复制到属性
    5. 按照我的具体案例的代码,但应该帮助任何面临类似问题的人:

      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>