首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# Soap WebService SoapHeader没有出现响应

C# Soap WebService SoapHeader没有出现响应
EN

Stack Overflow用户
提问于 2015-06-22 14:05:04
回答 1查看 1.8K关注 0票数 1

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

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

我读过有关SOAP头的文章,在某个时候,我在请求头和响应头中都有一个使用costum类的方法。

不知道我做了什么,但现在代码不起作用了。

注意:我正在使用SOAP进行测试。

代码语言:javascript
复制
[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;
    }
}

下面是一个测试:

请求

代码语言:javascript
复制
    <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>

响应

代码语言:javascript
复制
   <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中的头选项卡,就没有NPUHeader对象。

报头响应数据

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2015-06-25 08:58:56

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

问题是,当我们想要像SOAPHeader一样旅行的时候,我们需要做三件事:

  1. 创建类扩展SOAPHeader
  2. 在类的webservice中创建一个public属性
  3. 用它标记Tag方法( [SoapHeader(attribute_name,方位= SoapHeaderDirection.choose one)]

当我第一次尝试时,我拥有了全部,但后来,由于我希望接收该对象作为参数,并在响应时将其用于标头中,所以我删除了公共属性,认为将使用传递的对象。

它似乎不能这样工作,所以解决办法是:

  1. 确保上面提到的三个步骤
  2. 确保传递的参数和属性不会混为一谈
  3. (在我的例子中)将给定参数的必要属性复制到属性

遵循我的具体案例的代码,但应该帮助任何面临类似问题的人:

代码语言:javascript
复制
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请求

代码语言:javascript
复制
<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>

皂响应

代码语言:javascript
复制
<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>

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30982004

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档