0

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>
jezzipin
  • 4,110
  • 14
  • 50
  • 94
  • Do you have an `app.config` with a `system.serviceModel` entry? From the error it seems like you have too much information in your config –  Apr 24 '14 at 15:30
  • @DaveParsons I do indeed. There is a basicHTTPBinding and a customBinding. Is this what you mean? – jezzipin Apr 24 '14 at 15:32
  • Could you edit your question and include that section of the config? –  Apr 24 '14 at 15:33
  • Please find the edit above. I tried removing one of the Endpoints under client but now I get a different error regarding no endpoint listening at the target URL. – jezzipin Apr 24 '14 at 15:38
  • Added an answer, you'll need to put that endpoint back into the config. Hope it helps :) –  Apr 24 '14 at 15:47

1 Answers1

1

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.

Community
  • 1
  • 1
  • Great. Thanks for this. That seems to actually return something without error now even if it is just: ConsoleApplication1.Service1Reference.ForecastReturn . The problem is I don't know why it's not actually returning the value of any of the nodes in the XML returned or indeed the actual XML response. Am I missing something? – jezzipin Apr 24 '14 at 15:57
  • Put a breakpoint on this line `var result = svc.GetCityForecastByZIP(request);` and inspect the `result` object. It should contain all the information from the web service call. –  Apr 24 '14 at 15:58
  • 1
    I think it's simpler than that. In Visual Studio if I type result. it comes up with all of the options that are available to be returned. – jezzipin Apr 24 '14 at 15:59
  • Actually looking at it, that's not the case. I though that using result.Temperature would allow me to display the temperature but apparently Forecast return doesn't contain a definition for temperature. – jezzipin Apr 24 '14 at 16:10
  • @jezzipin: Experiment, research, debug, pull some hair out, repeat and if you still are having issues then I'd say open a new question :) –  Apr 25 '14 at 07:40