定义使用 Web 服务时要使用的端点 [英] Define which Endpoint to use when consuming a Web Service

查看:24
本文介绍了定义使用 Web 服务时要使用的端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to .NET and have been following this tutorial (http://johnwsaunders3.wordpress.com/2009/05/17/how-to-consume-a-web-service/) to consume a simple weather web service. My small console application essentially asks the user for a ZIP code, fires that to the web service then returns to response in the console. At least, that's the way it should work.

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

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

The problem with this is there are multiple endpoints for different ways of consuming the service:

  • SOAP 1.1
  • SOAP 1.2
  • HTTP 获取
  • HTTP POST

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

Because of this, when I run the console application, I am presented with the following error:

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 端点之一?到目前为止,我的代码可以在下面找到:

My question is, how do I specify that my call to the web service should use one of the SOAP endpoints? My code so far can be found below:

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();
        }
      }
    }
  }
}

对此的任何帮助将不胜感激.

Any help with this would be greatly appreciated.

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

The contents of my App.config file can be found here:

<?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 绑定(请参阅 这个问题了解更多信息).

It seems as though .NET is trying to be helpful in generating a SOAP 1.2 binding for you when you probably don't need it (see this question for more information).

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

To work around this you can explicitly tell the service client which binding to use when you instantiate it by specifying the endpoint name to use:

svc = new WeatherSoapClient("WeatherSoap");

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

Where "WeatherSoap" is the value of the name attribute on your endpoint node.

这篇关于定义使用 Web 服务时要使用的端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆