定义使用Web Service时要使用的端点

时间:2014-04-24 15:26:38

标签: c# .net web-services soap wsdl

我是.NET的新手,并且一直关注本教程(http://johnwsaunders3.wordpress.com/2009/05/17/how-to-consume-a-web-service/)来使用简单的天气Web服务。我的小型控制台应用程序基本上要求用户输入邮政编码,然后将其激活到Web服务,然后返回到控制台中的响应。至少,这就是应该运作的方式。

我使用的网络服务是: http://wsf.cdyne.com/WeatherWS/Weather.asmx

问题在于,有多种端点可供不同的服务使用方式:

  • SOAP 1.1
  • SOAP 1.2
  • HTTP GET
  • HTTP POST

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

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>

1 个答案:

答案 0 :(得分:1)

似乎.NET在您可能不需要时尝试为您生成SOAP 1.2绑定(请参阅this question以获取更多信息)。

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

svc = new WeatherSoapClient("WeatherSoap");

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