InvalidOperationException被粗暴处理

时间:2016-06-09 09:48:16

标签: c# .net wpf visual-studio xaml

我正在基于本网站的教程在Visual Studio 2015中构建C#解决方案。 https://msdn.microsoft.com/en-us/library/dd221354.aspx

代码如下:

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

 }
}

注意:我已经注册了密钥并在代码中使用了它。

运行解决方案时,会出现输出屏幕。 但是当我输入“1 Microsoft Way,Redmond,WA”并单击该按钮时,会出现一条错误消息。

错误消息如下:

  

InvalidOperationException被粗暴处理

     

未处理的类型&#39; System.InvalidOperationException&#39;发生在System.ServiceModel.dll

中      

附加信息:合同的端点配置部分&#39; GeocodeService.IGeocodeService&#39;无法加载,因为找到了该合同的多个端点配置。请按名称指明首选端点配置部分。

出现错误的代码行如下:

   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>

如何更正此错误?

谢谢。

1 个答案:

答案 0 :(得分:2)

由于您的app.config中为url: 'data/c3_test.csv'定义了2个端点,因此您需要提供有关您要使用的端点配置的信息。

例如,如果您想使用名称为“BasicHttpBinding_IGeocodeService”的端点配置,您可以编写

IGeocodeService

以下是msdn关于服务客户端用法的链接:https://msdn.microsoft.com/en-us/library/ms734691(v=vs.110).aspx