Question

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.

The web service I am using is: 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 GET
  • 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.

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.

Edit:

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>
Was it helpful?

Solution

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");

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top