1

我正在尝试创建一个使用 Bing 的地理编码肥皂服务的 wcf 服务。但是,当我尝试使用它的构造函数设置初始化一个新的 GeoCodeRequest 时,它会返回一个空值。当我打电话时,request.Query = address; 我收到一个空值错误,指的是 request.

public string RequestLocation(string address)
        {
            const string key = "mybingapplicationId";
            var request = new GeocodeRequest();
            request.Credentials.ApplicationId = key;
            request.Query = address;

            var filters = new ConfidenceFilter[1];
            filters[0] = new ConfidenceFilter { MinimumConfidence = Confidence.High };

            var geocodeOptions = new GeocodeOptions { Filters = filters };

            request.Options = geocodeOptions;

            // Make the geocode request
            var geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
            var geocodeResponse = geocodeService.Geocode(request);

            return geocodeResponse.Results[0].DisplayName;
        }

[单元测试]

 [TestMethod()]
        [HostType("ASP.NET")]
        [AspNetDevelopmentServerHost("C:\\Development\\WcfService1\\WcfService1", "/")]
        [UrlToTest("http://localhost:24842/")]
        [DeploymentItem("WcfService1.dll")]
        public void RequestLocationTest()
        {
            var target = new TestService.BingEngineClient();
            var address = string.Format("1600 Pennsylvania Avenue, {0}, {1}","Washington", "DC"); 
            var expected = string.Empty;
            var actual = target.RequestLocation(address);
            Assert.IsNotNull(actual);
            Assert.Inconclusive("Verify the correctness of this test method.");
        }
4

2 回答 2

1

首先,代码缺少凭据的初始化。

request.Credentials = new GeocodeService.Credentials();

当您完成创建 Bing 地图帐户时,您必须使用他们的应用程序为相关的特定应用程序
创建 Bing 地图密钥。请注意,这与您的帐户密钥不同。

于 2012-04-19T18:05:53.890 回答
0
    public string RequestLocation(string address)
            {

                var request = new GeocodeRequest {Credentials = new Credentials {ApplicationId = _key}, Query = address};
                var filters = new ConfidenceFilter[1];
                filters[0] = new ConfidenceFilter { MinimumConfidence = Confidence.High };

                var geocodeOptions = new GeocodeOptions { Filters = filters };

                request.Options = geocodeOptions;

                // Make the geocode request
                var geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
                var geocodeResponse = geocodeService.Geocode(request);

                return string.Format("Longitude:{0},Latitude:{1}", geocodeResponse.Results[0].Locations[0].Longitude,
                                     geocodeResponse.Results[0].Locations[0].Latitude);
            }
于 2012-04-19T18:10:27.743 回答