C# GeoCodeRequest()构造函数返回null
我正在尝试创建一个使用Bing地理编码soap服务的wcf服务。但当我尝试使用它的构造函数设置init一个新的GeoCodeRequest时,它会返回null。 当我调用C# GeoCodeRequest()构造函数返回null,c#,wcf,soap,bing,C#,Wcf,Soap,Bing,我正在尝试创建一个使用Bing地理编码soap服务的wcf服务。但当我尝试使用它的构造函数设置init一个新的GeoCodeRequest时,它会返回null。 当我调用request.Query=address时我在引用请求时收到一个空值错误 public string RequestLocation(string address) { const string key = "mybingapplicationId"; var r
request.Query=address时代码>我在引用请求时收到一个空值错误
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()代码>
当您浏览时,您必须使用他们的应用程序来
对于所讨论的具体应用。请注意,这与您的帐户密钥不同。首先,代码缺少凭据的初始化
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()代码>
当您浏览时,您必须使用他们的应用程序来
对于所讨论的具体应用。请注意,这与您的帐户密钥不同。您似乎缺少凭据的初始化request.Credentials=new GeocodeService.Credentials()代码>我想你经历了一些事情,似乎不对劲。您确定在调用查询属性的getter的上下文中没有得到null引用异常吗?如果在构造函数调用之后添加一个(if request==null)抛出新异常,会发生什么?@Rich它现在工作正常。将在下面发布更新代码。@atbyrd我已在下面发布了我的评论和一些其他信息。您似乎缺少凭据的初始化request.Credentials=new GeocodeService.Credentials()代码>我想你经历了一些事情,似乎不对劲。您确定在调用查询属性的getter的上下文中没有得到null引用异常吗?如果在构造函数调用之后添加一个(if request==null)抛出新异常,会发生什么?@Rich它现在工作正常。将在下面发布更新代码。@atbyrd我已在下面发布了我的评论和一些其他信息。
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);
}