正在中调用web服务.Net,并将对象作为参数

本文关键字:对象 参数 Net 调用 web 服务 | 更新日期: 2023-09-27 18:00:39

我正试图调用一个需要一些身份验证的web服务,尽管我对如何传入此身份验证的凭据有点困惑。web服务提供商提供的SOAP请求示例如下:

<s:Envelopexmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<h:ManagedUserHeaderxmlns:h="http://www.url.com/web/services/"xmlns="http://www.axumtech.com/web/services/"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<RegistrationKey>12345678910</RegistrationKey>
<CompanyName>TEST</CompanyName>
</h:ManagedUserHeader>
</s:Header>
<s:Bodyxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<GetContinentsxmlns="http://www.url.com/web/services/" />
</s:Body>
</s:Envelope>

我将使用来消费web服务。Net中的C#。我以前做过一些简单的web服务请求,但只在其中传递了一个简单的字符串作为请求的一部分来检索响应。我知道,为了让这个请求生效,我必须传入一个ManagedUserHeader对象,该对象包含两个属性,一个注册密钥和一个公司名称,尽管每当我尝试以这种方式对其进行编程时,我都会收到过载错误。

这是我到目前为止的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Umbraco.Core;
using Umbraco.Core.Services;
using website.AxumStaticData;
namespace website.umbraco
{
  public partial class StaticDataService : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      StaticDataWebServiceSoapClient service = null;
      bool success = false;
      var guid = "12345678910";
      var companyName = "Test";
      var compressionSoapHeader = new AxumStaticData.CompressionSoapHeader();
      var managedUserHeader = new AxumStaticData.ManagedUserHeader();
      managedUserHeader.CompanyName = companyName;
      managedUserHeader.RegistrationKey = new Guid(guid);
      try{
          service = new StaticDataWebServiceSoapClient();
          var result = service.GetContinents(compressionSoapHeader,managedUserHeader);
      }finally{
          if (!success && service != null){
            service.Abort();
          }
      }
    }
  }
}

根据我在Visual Studio中收到的错误,这一行有一些无效的参数:

var result = service.GetContinents(compressionSoapHeader,managedUserHeader);

然而,如果我删除参数,我会得到以下错误:

No overload for method 'GetContinents' takes 0 arguments

我真的很难解决这个问题,因为我以前只用纯XML调用过web服务,所以任何帮助都将不胜感激。如果需要,我很乐意提供更多信息。

正在中调用web服务.Net,并将对象作为参数

试试这个:

var result = service.GetContinents(ref compressionSoapHeader, ref managedUserHeader);

当方法具有ref参数时,您需要包括ref。当它有一个out参数时,您需要包括out

最终的问题是由于压缩soap头对象需要指定压缩方法。我以前尝试过这种方法,但压缩方法是enum类型,所以我必须做以下操作:

var compressionSoapHeader = new CompressionSoapHeader();
compressionSoapHeader.CompressionMethodUsed = CompressionMethods.None;

在所有这些之后,我的web服务请求成功完成。