C# 定义使用Web服务时要使用的端点

C# 定义使用Web服务时要使用的端点,c#,.net,web-services,soap,wsdl,C#,.net,Web Services,Soap,Wsdl,我是.NET新手,一直遵循本教程()来使用一个简单的天气web服务。我的小型控制台应用程序本质上要求用户输入邮政编码,将其发送到web服务,然后返回控制台中的响应。至少,它应该是这样工作的 我正在使用的web服务是: 这方面的问题是,对于使用服务的不同方式,存在多个端点: 肥皂1.1 肥皂1.2 HTTP获取 HTTP POST 因此,在运行console应用程序时,出现以下错误: Unhandled Exception: System.InvalidOperationException:

我是.NET新手,一直遵循本教程()来使用一个简单的天气web服务。我的小型控制台应用程序本质上要求用户输入邮政编码,将其发送到web服务,然后返回控制台中的响应。至少,它应该是这样工作的

我正在使用的web服务是:

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

  • 肥皂1.1
  • 肥皂1.2
  • HTTP获取
  • HTTP POST
因此,在运行console应用程序时,出现以下错误:

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>

在您可能不需要的情况下,.NET似乎试图帮助您生成SOAP 1.2绑定(有关更多信息,请参阅)

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

svc = new WeatherSoapClient("WeatherSoap");

其中,
“WeatherSoap”
端点
节点上的
名称
属性的值。

是否有带有
system.serviceModel
条目的
app.config
?从错误的角度看,您的数据库中似乎有太多的信息config@DaveParsons我确实喜欢。有一个basicHTTPBinding和一个customBinding。这就是你的意思吗?你能编辑你的问题并包括配置的那个部分吗?请找到上面的编辑。我尝试删除客户端下的一个端点,但现在出现了另一个错误,即在目标URL上没有端点侦听。添加了一个答案,您需要将该端点放回配置中。希望有帮助:)太好了。谢谢你。现在,这似乎实际返回了一些没有错误的内容,即使它只是:ConsoleApplication1.Service1Reference.forecasterturn。问题是,我不知道为什么它没有实际返回返回的XML中任何节点的值,或者实际上是实际的XML响应。我遗漏了什么吗?在这一行上放置一个断点
var result=svc.GetCityForecastByZIP(请求)并检查
结果
对象。它应该包含来自web服务调用的所有信息。我认为它比这个简单。在VisualStudio中键入结果。它提供了所有可以返回的选项。非常好,编码很愉快:)实际上看,情况并非如此。我认为使用result.Temperature可以显示温度,但显然Forecast return不包含温度的定义。