GeoCodeRequest() 构造函数返回 null

本文关键字:返回 null 构造函数 GeoCodeRequest | 更新日期: 2023-09-27 18:31:51

我正在尝试创建一个使用必应地理编码肥皂服务的 wcf 服务。但是当我尝试使用它的构造函数初始化一个新的地理编码请求时,它会返回一个空值。当我调用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.");
        }

GeoCodeRequest() 构造函数返回 null

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

request.Credentials = new GeocodeService.Credentials();

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

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