使用对象作为参数在.Net中调用Web服务

时间:2014-04-29 14:19:10

标签: c# .net web-services service-reference

我正在尝试调用需要某些身份验证的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>

我将在C#中使用.Net来使用Web服务。我之前做了一些简单的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服务,所以任何帮助都会受到高度赞赏。如果需要,我很乐意提供更多信息。

2 个答案:

答案 0 :(得分:0)

试试这个:

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

当方法具有ref参数时,您需要包含ref。如果它具有out参数,则需要包含out

答案 1 :(得分:0)

最后的问题是由于压缩soap标头对象需要指定压缩方法。我之前尝试过这个,但压缩方法的类型为枚举,所以我必须执行以下操作:

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

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