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



我正在尝试使用Bing Maps Rest Toolkit对英国邮政编码进行地理编码。代码应该对地址进行地理编码,然后将其传递到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地图进行此操作的示例,但这里有一个使用Azure地图的类似示例:https://azuremapscodesamples.azurewebsites.net/?sample=Methods%20for%20geocoding%20multiple%20addresses

最新更新