Gajesh
Gajesh

Reputation: 21

InvalidOperationException was manhandled

I was building a C# solution in Visual Studio 2015 based on the tutorial in this website. https://msdn.microsoft.com/en-us/library/dd221354.aspx

The codes are as follows:

MainWindow.xaml

<Window x:Class="BingMapsSample.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:BingMapsSample"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <TextBox x:Name="textInput" HorizontalAlignment="Left" Height="23" Margin="94,30,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="178"/>
    <Label x:Name="labelResults" Content="Label" HorizontalAlignment="Left" Margin="65,134,0,0" VerticalAlignment="Top" Width="79" RenderTransformOrigin="-0.079,0.769" Height="28"/>
    <Image x:Name="imageResults" HorizontalAlignment="Left" Height="28" Margin="65,134,0,0" VerticalAlignment="Top" Width="79" RenderTransformOrigin="0.47,-0.56" Visibility="Hidden" Stretch="None"/>
    <Button x:Name="Geocode" Content="Geocode" HorizontalAlignment="Left" Margin="80,84,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.733,0.409" Click="button_Click"/>

</Grid>

MainWindow.xaml.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using BingMapsSample.GeocodeService;
using BingMapsSample.SearchService;
using BingMapsSample.ImageryService;
using BingMapsSample.RouteService;

namespace BingMapsSample
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void button_Click(object sender, RoutedEventArgs e)
    {
        labelResults.Content = GeocodeAddress(textInput.Text);
    }
    private String GeocodeAddress(string address)
    {
        string results = "";
        string key = "insert your Bing Maps key here";
        GeocodeRequest geocodeRequest = new GeocodeRequest();

        // Set the credentials using a valid Bing Maps key
        geocodeRequest.Credentials = new GeocodeService.Credentials();
        geocodeRequest.Credentials.ApplicationId = key;

        // Set the full address query
        geocodeRequest.Query = address;

        // Set the options to only return high confidence results 
        ConfidenceFilter[] filters = new ConfidenceFilter[1];
        filters[0] = new ConfidenceFilter();
        filters[0].MinimumConfidence = GeocodeService.Confidence.High;

        // Add the filters to the options
        GeocodeOptions geocodeOptions = new GeocodeOptions();
        geocodeOptions.Filters = filters;
        geocodeRequest.Options = geocodeOptions;

        // Make the geocode request
        GeocodeServiceClient geocodeService = new GeocodeServiceClient();
        GeocodeResponse geocodeResponse = geocodeService.Geocode(geocodeRequest);

        if (geocodeResponse.Results.Length > 0)
            results = String.Format("Latitude: {0}\nLongitude: {1}",
              geocodeResponse.Results[0].Locations[0].Latitude,
              geocodeResponse.Results[0].Locations[0].Longitude);
        else
            results = "No Results Found";

        return results;
    }

 }
}

Note: I have registered for the key and have used it in the code.

When I run the solution, The ouput screen appears. But when I type “1 Microsoft Way, Redmond, WA” and click the button, an error message appears.

The error message is as follows:

InvalidOperationException was manhandled

An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll

Additional information: An endpoint configuration section for contract 'GeocodeService.IGeocodeService' could not be loaded because more than one endpoint configuration for that contract was found. Please indicate the preferred endpoint configuration section by name.

The line of code that has the error is as follows:

   GeocodeServiceClient geocodeService = new GeocodeServiceClient();

App.config

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IGeocodeService" />
                <binding name="BasicHttpBinding_ISearchService" />
                <binding name="BasicHttpBinding_IImageryService" />
                <binding name="BasicHttpBinding_IRouteService" />
            </basicHttpBinding>
            <customBinding>
                <binding name="CustomBinding_IGeocodeService">
                    <binaryMessageEncoding />
                    <httpTransport />
                </binding>
                <binding name="CustomBinding_ISearchService">
                    <textMessageEncoding messageVersion="Soap12" />
                    <httpTransport />
                </binding>
                <binding name="CustomBinding_IImageryService">
                    <binaryMessageEncoding />
                    <httpTransport />
                </binding>
                <binding name="CustomBinding_IRouteService">
                    <binaryMessageEncoding />
                    <httpTransport />
                </binding>
            </customBinding>
        </bindings>
        <client>
            <endpoint address="http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IGeocodeService"
                contract="GeocodeService.IGeocodeService" name="BasicHttpBinding_IGeocodeService" />
            <endpoint address="http://dev.virtualearth.net/webservices/v1/geocodeservice/GeocodeService.svc/binaryHttp"
                binding="customBinding" bindingConfiguration="CustomBinding_IGeocodeService"
                contract="GeocodeService.IGeocodeService" name="CustomBinding_IGeocodeService" />
            <endpoint address="http://dev.virtualearth.net/webservices/v1/searchservice/searchservice.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ISearchService"
                contract="SearchService.ISearchService" name="BasicHttpBinding_ISearchService" />
            <endpoint address="http://dev.virtualearth.net/webservices/v1/searchservice/searchservice.svc/binaryHttp"
                binding="customBinding" bindingConfiguration="CustomBinding_ISearchService"
                contract="SearchService.ISearchService" name="CustomBinding_ISearchService" />
            <endpoint address="http://dev.virtualearth.net/webservices/v1/imageryservice/imageryservice.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IImageryService"
                contract="ImageryService.IImageryService" name="BasicHttpBinding_IImageryService" />
            <endpoint address="http://dev.virtualearth.net/webservices/v1/imageryservice/imageryservice.svc/binaryHttp"
                binding="customBinding" bindingConfiguration="CustomBinding_IImageryService"
                contract="ImageryService.IImageryService" name="CustomBinding_IImageryService" />
            <endpoint address="http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRouteService"
                contract="RouteService.IRouteService" name="BasicHttpBinding_IRouteService" />
            <endpoint address="http://dev.virtualearth.net/webservices/v1/routeservice/routeservice.svc/binaryHttp"
                binding="customBinding" bindingConfiguration="CustomBinding_IRouteService"
                contract="RouteService.IRouteService" name="CustomBinding_IRouteService" />
        </client>
    </system.serviceModel>
</configuration>

How can I correct this error?

Thank you.

Upvotes: 0

Views: 222

Answers (1)

yeah-buddy
yeah-buddy

Reputation: 424

Since you have 2 endpoints defined in your app.config for IGeocodeService, you'll need to provide information about which endpoint configuration you'll want to use.

For instance, if you wanted to use the endpoint configuration with the name 'BasicHttpBinding_IGeocodeService' you would write

GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");

Here's a link from msdn about service client usages: https://msdn.microsoft.com/en-us/library/ms734691(v=vs.110).aspx

Upvotes: 2

Related Questions