首页 > 解决方案 > 尝试复制 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 调用返回空值的东西?

标签: c#restbing-maps

解决方案


经过更多的故障排除后,事实证明代码在那个时候已经足够好了。但是,由于 main 方法中除了 API 调用之外没有太多其他内容,因此它没有足够的时间来获取 API 调用的响应。我的解决方案是在 main 方法中的线程上放置 15 秒的睡眠时间,这足以让单独的线程获得响应,并让 ApiCallAsync 的其余部分运行其所有代码。


推荐阅读