I am using imagery service to get an image from bing maps, and from time to time my apllication used to quit without a reason, I figured out that it's because imagery service is not handling the mapuri request timeout.
Get Image:
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);
}
Response here:
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;
The exception:
The HTTP request to 'http://dev.virtualearth.net/webservices/v1/imageryservice/imageryservice.svc' has exceeded the allotted timeout of 00:01:00. The time allotted to this operation may have been a portion of a longer timeout.
You can easily reproduce this by putting a break point after the request and waiting there about a minute or so.
What should I do to handle this exception, I didn't find any solution, nor question related to this topic...
I'm also talking about this problem here: http://forums.create.msdn.com/forums/p/103502/616465.aspx#616465
Thanks in advance.
Handling the exception in the getImageryCompleted event seemed to fix this problem:
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 {}
}