2012-04-19 2 views
1

Я пытаюсь создать службу wcf, которая потребляет мышь Bing's Geocoding. Но когда я пытаюсь установить init, новый GeoCodeRequest, используя его конструктор, он возвращает нуль. Когда я звоню 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; 
     } 

[UnitTest]

[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();' –

+0

Я предполагаю, что вы прошли через [Создание учетной записи Bing Maps] (http://msdn.microsoft.com/en-us/library/gg650598.aspx) –

+0

Что-то кажется выключенным. Вы уверены, что не получаете исключение с нулевой ссылкой где-нибудь в контексте вызова getter для свойства Query? Что произойдет, если вы добавите (если запрос == null) бросить новое исключение сразу после вызова конструктора? – Rich

ответ

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); 
      } 
Смежные вопросы