我試圖創建一個使用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();' – 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