【问题标题】:Calling a web service in .Net with an object as a parameter使用对象作为参数在 .Net 中调用 Web 服务
【发布时间】:2014-04-29 14:19:10
【问题描述】:

我正在尝试调用需要一些身份验证的 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 服务,因此我们将不胜感激。如果需要,我很乐意提供更多信息。

【问题讨论】:

  • GetContinents() 中需要什么?
  • @sr28 根据 Visual Studio 需要两个参数,虽然我知道第一个是可选的: ContinentData[]StaticDataWebServiceSoapClient.GetContinents(ref CompressionSoapHeader CompressionSoapHeader, ref ManagedUserHeader ManagedUserHeader)

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


【解决方案1】:

试试这个:

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

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

【讨论】:

  • 这不起作用,因为我应该能够为 each(var response in result){var something = response.Id}) 做一个,但 Visual Studio 建议这些选项不可用的意思它工作不正常。
  • 说真的,去看看参数的类型。您不知道如何使用对象浏览器查看类型吗?见johnwsaunders3.wordpress.com/2009/05/17/…
  • 我已经做到了,我正在学习,所以请放过我。
【解决方案2】:

问题归根结底是由于压缩肥皂头对象需要指定压缩方法。我之前尝试过,但压缩方法是枚举类型,所以我必须执行以下操作:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    • 2011-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多