调用 Request.Execute 方法以使用 REST 服务导致 ASP.NET 网页无限加载

Calling Request.Execute Method to use REST service causes ASP.NET Web Page to load infinitely

我正在尝试使用 Bing Maps Rest 工具包对英国邮政编码进行地理编码。该代码应对地址进行地理编码,然后将其传递给 ASP.Net 网页,其中地理编码地址用于在地图上绘制航路点。

应用程序构建没有错误,但每当我尝试 运行 它时,页面在显示任何内容之前超时。我推断当在这一行调用 Rest API 时它失败了:

var response = await request.Execute();

除此之外,我不确定还有什么问题。我对此很陌生,所以非常感谢一些指导。

在此先感谢,我的完整代码如下:

MapController.cs

public class MapController : Controller
{

    // GET: Map
    public ActionResult Index()
    {

        StopLists model = new StopLists();

        var Stops = new List<Stops>()
        {
            new Stops (1, "Stoke Station", null , "ST4 2AA"),

        };

        model.StopList = Stops;

        foreach (var StopLocation in model.StopList)
        {   
            Task<Location> gc = AddGeocode(StopLocation.Postcode);
            StopLocation.Geocode = gc.Result;
            
        };

        return View();

    }            


    public async Task<Location> AddGeocode(String Postcode)
    {
        
        var request = new GeocodeRequest()
        {
            Query = Postcode,
            IncludeIso2 = true,
            IncludeNeighborhood = true,
            MaxResults = 25,
            BingMapsKey = "Placeholder Bing Maps Key"
        };

        var response = await request.Execute();

        //sets responce type to location type, and assigns the value to the geocode
        Location Geocode = response.ResourceSets[0].Resources[0] as BingMapsRESTToolkit.Location;

        return Geocode;

    }
}

Index.cshtml

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta charset="utf-8" />

<title></title>

</head>
<body onload="GetMap();">
<form id="form1" runat="server">

    <div id="myMap" style='position:relative;width:600px;height:400px; top: 0px; left: 0px;'></div>
    <div id="itineraryContainer"></div>

    <script type="text/javascript">
        function GetMap() {

            var MapsKey = '<My Bing Maps Key>';

            var map = new Microsoft.Maps.Map('#myMap', {
                credentials: MapsKey,
                center: new Microsoft.Maps.Location(53.0146, -2.1864),
                zoom: 17
            });

            var center = map.getCenter();

            getRoute(map);

            getTraffic(map);

        }

        function getRoute(map) {

            Microsoft.Maps.loadModule('Microsoft.Maps.Directions', function () {
                var directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);

                //Set Route Mode to driving
                directionsManager.setRequestOptions({ routeMode: Microsoft.Maps.Directions.RouteMode.driving });
                
                @if (Model.StopList != null)
                {
                    foreach(var StopLocation in Model.StopList)
                    {
                        @:var Stop = new Microsoft.Maps.Directions.Waypoint({
                            @:address:'@StopLocation.Name', location: @StopLocation.Geocode;
                        @:});
                        @:directionsManager.addWaypoint(Stop);
                    }
                }

                // Shows where the written directions will be rendered
                directionsManager.setRenderOptions({ itineraryContainer: '#itineraryContainer' });

                directionsManager.calculateDirections();

            });
        }

        function getTraffic(map) {
            Microsoft.Maps.loadModule('Microsoft.Maps.Traffic', function () {
                var trafficManager = new Microsoft.Maps.Traffic.TrafficManager(map);
                trafficManager.show();
            });
        }

    </script>

    <script type="text/javascript" src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap' async defer></script>
    <p>
    </p>
</form>
<p>
    &nbsp;
</p>
</body>
</html>

编辑:删除了 Bing 地图键

AddGeocode 方法是异步的,但您从不等待它。在您的 C# 中创建一个 AddGeocode 任务数组,然后在循环外并行使用 await Task.WhenAll 到 运行 它们。这将有助于提高性能并确保在继续之前已处理完所有内容。

关于体系结构的其他一些注意事项:

  • 如果您要进行地理编码的位置是提前已知的并且不会经常更改,请考虑提前对这些位置进行地理编码并将结果存储在数据库中。这将显着降低成本并提高性能。这是大多数定位类型应用程序的标准做法。
  • 如果位置经常变化并且地理编码不是一个选项。目前您的代码从服务器端执行所有地理编码请求,然后发送结果。这意味着在您的服务器上有更多的计算和更多的 http 请求,与您将该处理卸载到客户端相比,这将降低可扩展性。考虑在浏览器中进行地理编码调用。这将意味着计算处理和那些 http 请求将由最终用户计算机发出,这将减少您服务器上的使用并允许它处理更多用户。我没有使用 Bing Maps 执行此操作的示例,但这里有一个使用 Azure Maps 的类似示例:https://azuremapscodesamples.azurewebsites.net/?sample=Methods%20for%20geocoding%20multiple%20addresses