定义使用Web服务时要使用的终结点

本文关键字:结点 服务 Web 定义 | 更新日期: 2023-09-27 17:59:48

我是.NET新手,一直在学习本教程(http://johnwsaunders3.wordpress.com/2009/05/17/how-to-consume-a-web-service/)使用简单的天气web服务。我的小型控制台应用程序本质上是向用户请求邮政编码,将其发送到web服务,然后返回控制台中的响应。至少,它应该是这样运作的。

我正在使用的web服务是:http://wsf.cdyne.com/WeatherWS/Weather.asmx

这方面的问题是,使用服务的不同方式有多个端点:

  • 肥皂1.1
  • 肥皂1.2
  • HTTP获取
  • HTTP帖子

因此,当我运行控制台应用程序时,会出现以下错误:

Unhandled Exception: System.InvalidOperationException: An endpoint configuration section for contract 'Service1Reference.WeatherSoap'
could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name.

我的问题是,如何指定对web服务的调用应该使用其中一个SOAP端点?到目前为止,我的代码可以在下面找到:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ConsoleApplication1.Service1Reference;
namespace ConsoleApplication1
{
  class Program
  {
    static void Main(string[] args)
    {
      Console.Write("Enter ZipCode: ");
      var line = Console.ReadLine();
      if (line == null)
      {
        return;
      }
      WeatherSoapClient svc = null;
      bool success = false;
      try
      {
        svc = new WeatherSoapClient();
        var request = line;
        var result = svc.GetCityForecastByZIP(request);
        Console.WriteLine("The result is:");
        Console.WriteLine(result);
        Console.Write("ENTER to continue:");
        Console.ReadLine();
        svc.Close();
        success = true;
      }
      finally
      {
        if (!success && svc != null)
        {
          svc.Abort();
        }
      }
    }
  }
}

如有任何帮助,我们将不胜感激。

编辑:

我的App.config文件的内容可以在这里找到:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="WeatherSoap" />
            </basicHttpBinding>
            <customBinding>
                <binding name="WeatherSoap12">
                    <textMessageEncoding messageVersion="Soap12" />
                    <httpTransport />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="http://wsf.cdyne.com/WeatherWS/Weather.asmx"
                binding="customBinding" bindingConfiguration="WeatherSoap12"
                contract="Service1Reference.WeatherSoap" name="WeatherSoap12" />
        </client>
    </system.serviceModel>
</configuration>

定义使用Web服务时要使用的终结点

在您可能不需要的时候,.NET似乎试图为您生成SOAP 1.2绑定提供帮助(有关更多信息,请参阅此问题)。

为了解决这个问题,您可以通过指定要使用的端点名称来明确地告诉服务客户端在实例化它时要使用哪个绑定:

svc = new WeatherSoapClient("WeatherSoap");

其中,"WeatherSoap"endpoint节点上name属性的值。