2012-04-19 74 views
1

我試圖創建一個使用Bing的Geocoding soap服務的wcf服務。但是,當我嘗試使用它的構造函數設置init一個新的GeoCodeRequest時,它將返回一個null。 當我撥打request.Query = address;時,我收到一個空值錯誤,指的是requestGeoCodeRequest()構造函數返回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."); 
     } 
+1

您似乎缺少憑證的初始化。 'request.Credentials = new GeocodeService.Credentials();' – 2012-04-19 16:57:09

+0

我假設你已經通過[創建Bing地圖帳戶](http://msdn.microsoft.com/en-us/library/gg650598.aspx) – 2012-04-19 17:59:40

+0

東西似乎關閉。您確定在調用Query屬性的getter的上下文中沒有找到空引用異常嗎?如果添加一個(如果request == null),則在構造函數調用之後立即拋出新異常會發生什麼? – Rich 2012-04-19 18:01:53

回答

1

首先代碼缺失證書的初始化。

request.Credentials = new GeocodeService.Credentials();

當你經過Creating a Bing Maps Account你必須使用他們的應用程序來
Create a Bing Maps Key有問題的具體應用。請注意,這與您的帳戶密鑰不同。

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