c# - 尝试复制 BingMapsRESTToolkit 的示例,从我的 C# 控制台应用程序中的 API 调用接收空响应。
问题描述
经过过去一天的大量谷歌搜索和拖网搜索,我无法使用他们在 Github 上的示例代码让 BingMapsRESTToolkit 按预期工作。我安装了所有正确的依赖项(包括 BingMapsRESTToolkit),我当前的代码看起来像这样。
public static void Main(string[] args)
{
var apiCall = ApiCallAsync();
}
static async Task ApiCallAsync()
{
var request = new GeocodeRequest()
{
Query = "New York, NY",
IncludeIso2 = true,
IncludeNeighborhood = true,
MaxResults = 25,
BingMapsKey = ApiKey //Referencing a string constant here declared at the top of the project which is my Bing Query key from Azure.
};
var response = await ServiceManager.GetResponseAsync(request);
if (response != null &&
response.ResourceSets != null &&
response.ResourceSets.Length > 0 &&
response.ResourceSets[0].Resources != null &&
response.ResourceSets[0].Resources.Length > 0)
{
var result = response.ResourceSets[0].Resources[0] as BingMapsRESTToolkit.Location;
Console.WriteLine(result.Name.Length); // just a simple log in order to see if the request has definitely worked
}
}
之后,向它声称响应当前为空的响应行添加断点。控制台上也没有输出证明没有命中 WriteLine。
我的问题是,是否有任何公然导致 API 调用返回空值的东西?
解决方案
经过更多的故障排除后,事实证明代码在那个时候已经足够好了。但是,由于 main 方法中除了 API 调用之外没有太多其他内容,因此它没有足够的时间来获取 API 调用的响应。我的解决方案是在 main 方法中的线程上放置 15 秒的睡眠时间,这足以让单独的线程获得响应,并让 ApiCallAsync 的其余部分运行其所有代码。
推荐阅读
- javascript - 如何在 SpeechRecognition Web Speech API 中禁用句子级自动更正
- javascript - 我如何无法在 javascript 中滚动?
- dynamics-crm - CRM 2013 Onprem:在 CRM 中登录的用户访问区域
- php - 为sql中的列设置唯一约束
- ios - 可以将 PWA 应用发布到应用商店 Swift
- python - 我们如何在不中断实时流量的情况下更新已部署的 Flask 应用程序?
- angular - 错误:无法匹配任何路由。URL 段:'admin/projects/projectssCategory/list'
- reactjs - 反应 useEffect 钩子导致无限循环
- node.js - 如何删除带有猫鼬ID的文档?
- android - 如何从班级外部正确访问我的 MainActivity?