lisardggy

Catching a layer's InitializationFailed without crashing.

Discussion created by lisardggy on May 17, 2012
Latest reply on May 17, 2012 by JNery-esristaff
Hello.

I'm trying to figure out some strange behavior I'm seeing with the old WPF API (not the new Engine), when initializing an ArcGISDynamicMapServiceLayer with an invalid URL.
As part of my tests, I tried configuring an invalid GIS Server URI for my layer, while handling the InitializationFailed event to catch the exception:

_dynamicBaseLayer = new ArcGISDynamicMapServiceLayer();
_dynamicBaseLayer.InitializationFailed += (sender, args) =>
        {
             var baseLayer = sender as ArcGISDynamicMapServiceLayer;
             if (_items.Contains(baseLayer)) // this is my LayerCollection
                 _items.Remove(baseLayer);
                                                              
             _notificationService.ShowDialog("Error loading layer");
             _logger.Error("Error loading base map layer, baseLayer.InitializationFailure);
        };

 _dynamicBaseLayer.Url = BaseLayerUrl;
_items.Add(_dynamicBaseLayer);


When I run, the InitializationFailed event is indeed raised and caught, and my error logged and my dialog displayed. But there is also another thread running that throws an exception which goes unhandled, and reaches my application root and crashes, with this stacktrace:

System.Net.WebException: The remote name could not be resolved: 'my_invalid_server_url'
   at ESRI.ArcGIS.Client.Layer.OnInitializationFailed(EventArgs e)
   at ESRI.ArcGIS.Client.Layer.Initialize()
   at ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer.<>n__FabricatedMethod19()
   at ESRI.ArcGIS.Client.ArcGISDynamicMapServiceLayer.<>c__DisplayClass17.<MapServiceInfoInitFailed>b__15()
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

This is annoying, since this occurs in a background thread, in an anonymous method, where I can't catch and handle it except as part of the whole application's UnhandledExceptionHandler.

I found this old thread that seems to be very similar, and it's the reason I tried to remove the layer from the LayerCollection on error, but it still doesn't seem to help, and I get my own dialog in addition to the unhandled exception.

Outcomes