GeoCodeRequest()构造函数返回null
问题描述:
我试图创建一个使用Bing的Geocoding soap服务的wcf服务。但是,当我尝试使用它的构造函数设置init一个新的GeoCodeRequest时,它将返回一个null。 当我拨打request.Query = address;
时,我收到一个空值错误,指的是request
。GeoCodeRequest()构造函数返回null
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.");
}
答
首先代码缺失证书的初始化。
request.Credentials = new GeocodeService.Credentials();
当你经过Creating a Bing Maps Account你必须使用他们的应用程序来
Create a Bing Maps Key有问题的具体应用。请注意,这与您的帐户密钥不同。
答
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);
}
您似乎缺少凭证的初始化。 'request.Credentials = new GeocodeService.Credentials();' – 2012-04-19 16:57:09
我假设你已经通过[创建Bing地图帐户](http://msdn.microsoft.com/en-us/library/gg650598.aspx) – 2012-04-19 17:59:40
东西似乎关闭。您确定在调用Query属性的getter的上下文中没有找到空引用异常吗?如果添加一个(如果request == null),则在构造函数调用之后立即抛出新异常会发生什么? – Rich 2012-04-19 18:01:53