0

我正在使用图像服务从 bing 地图中获取图像,并且有时我的应用程序会无缘无故地退出,我发现这是因为图像服务没有处理 mapuri 请求超时。

获取图像:

    public static void GetBingMapImage(double longitude, double latitude, Size size, int zoomLevel, ImageryServiceParams imageResponseCallback)
    {
        var mapUriRequest = new MapUriRequest();
        var location = new GeocodeLocation { Latitude = latitude, Longitude = longitude };

        // Set credentials using a valid Bing Maps key
        mapUriRequest.Credentials = new Credentials();
        mapUriRequest.Credentials.ApplicationId = BingMapsKey;

        // Set the location of the requested image
        mapUriRequest.Center = new GeocodeLocation();
        mapUriRequest.Center.Latitude = location.Latitude;
        mapUriRequest.Center.Longitude = location.Longitude;

        mapUriRequest.Pushpins = new ObservableCollection<ImageryService.Pushpin>();
        mapUriRequest.Pushpins.Add(new ImageryService.Pushpin { Location = location, IconStyle = "10" });
        // Set the map style and zoom level
        var mapUriOptions = new MapUriOptions();
        mapUriOptions.Style = MapStyle.AerialWithLabels;
        mapUriOptions.ZoomLevel = zoomLevel;

        // Set the size of the requested image to match the size of the image control
        mapUriOptions.ImageSize = new SizeOfint();
        mapUriOptions.ImageSize.Height = Convert.ToInt16(size.Height);
        mapUriOptions.ImageSize.Width = Convert.ToInt16(size.Width);

        mapUriRequest.Options = mapUriOptions;

        var imageryService = new ImageryServiceClient("BasicHttpBinding_IImageryService");
        imageryService.GetMapUriCompleted += ImageryServiceGetMapUriCompleted;

        imageryService.GetMapUriAsync(mapUriRequest, imageResponseCallback);
    }

此处回复:

        public MyApplication.ImageryService.MapUriResponse EndGetMapUri(System.IAsyncResult result) {
            object[] _args = new object[0];
            MyApplication.ImageryService.MapUriResponse _result = ((MyApplication.ImageryService.MapUriResponse)(base.EndInvoke("GetMapUri", _args, result)));
            return _result;

例外:

对“http://dev.virtualearth.net/webservices/v1/imageryservice/imageryservice.svc”的 HTTP 请求已超过分配的超时时间 00:01:00。分配给此操作的时间可能是较长超时的一部分。

您可以通过在请求后放置一个断点并在那里等待大约一分钟左右来轻松地重现这一点。

我应该怎么做才能处理这个异常,我没有找到任何解决方案,也没有找到与这个主题相关的问题......

我也在这里谈论这个问题:http ://forums.create.msdn.com/forums/p/103502/616465.aspx#616465

提前致谢。

4

1 回答 1

0

处理 getImageryCompleted 事件中的异常似乎可以解决这个问题:

        private static void ImageryServiceGetMapUriCompleted(object sender, GetMapUriCompletedEventArgs e) 
        { 
// This is where you want to handle the timeout or web service errors.  This will prevent
// the error that the debugger stops on from getting back to the main thread and from your
// application having an UnhandledException.
try
{
            if (e.Result == null) return; 

            var imageryServiceParams = e.UserState as ImageryServiceParams; 
            if (imageryServiceParams != null) 
                imageryServiceParams.ImageResponseCallback.DynamicInvoke(new ImageryServiceResult(new Uri(e.Result.Uri, UriKind.Absolute)) { UserState = imageryServiceParams.UserState }); 
          }
catch
{
// The "MessageBox.Show is in a try/catch since it throws an exception if it is called when another
// MessageBox.Show is open. This was at least the case in Pre-Mango releases.
try { MessageBox.Show("Caught the TimeOut or WebException.  Application still running."); }
catch {}
}
于 2012-05-14T12:41:01.467 回答