GeoCodeRequest()构造函数返回null

时间:2012-04-19 16:17:30

标签: c# wcf soap bing

我正在尝试创建一个消耗Bing的Geocoding soap服务的wcf服务。但是当我尝试使用它的构造函数设置init一个新的GeoCodeRequest时,它返回一个null。 当我拨打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.");
        }

2 个答案:

答案 0 :(得分:1)

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

request.Credentials = new GeocodeService.Credentials();

当您通过Creating a Bing Maps Account时,您必须使用他们的申请 Create a Bing Maps Key针对相关的具体应用。请注意,这与您的帐户密钥不同。

答案 1 :(得分: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);
            }