Bug 7088 - System.Xml.XmlException cannot be caught when WCF server responds badly (500 404 etc)
Summary: System.Xml.XmlException cannot be caught when WCF server responds badly (500 ...
Alias: None
Product: iOS
Classification: Xamarin
Component: XI runtime ()
Version: 5.4.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2012-09-12 05:50 UTC by uwantfries
Modified: 2013-12-05 18:35 UTC (History)
3 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description uwantfries 2012-09-12 05:50:27 UTC
Possible regression of https://bugzilla.novell.com/show_bug.cgi?id=676009 or https://bugzilla.novell.com/show_bug.cgi?id=691138

Error dump:

12 September 2012 _ 09:54:37 [4]  :HandleProxyGetWallCompleted:System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Xml.XmlException: Document Type Declaration (DTD) is prohibited in this XML.  Line 1, position 10.

  at Mono.Xml2.XmlTextReader.ReadDoctypeDecl () [0x00189] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml/XmlTextReader.cs:2486

  at Mono.Xml2.XmlTextReader.ReadDeclaration () [0x00064] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml/XmlTextReader.cs:2307

  at Mono.Xml2.XmlTextReader.ReadContent () [0x0012a] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml/XmlTextReader.cs:1343

  at Mono.Xml2.XmlTextReader.Read () [0x00141] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml/XmlTextReader.cs:626

  at System.Xml.XmlTextReader.Read () [0x0006b] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml/XmlTextReader2.cs:564

  at System.Xml.XmlSimpleDictionaryReader.Read () [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.Runtime.Serialization/System.Xml/XmlSimpleDictionaryReader.cs:253

  at System.Xml.XmlReader.MoveToContent () [0x00050] in /Developer/MonoTouch/Source/mono/mcs/class/System.XML/System.Xml/XmlReader.cs:563

  at System.ServiceModel.Channels.XmlReaderMessage.ReadEnvelopeStart () [0x00000] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System.ServiceModel.Channels/MessageImpl.cs:146

  at System.ServiceModel.Channels.XmlReaderMessage..ctor (System.ServiceModel.Channels.MessageVersion version, System.Xml.XmlDictionaryReader reader, Int32 maxSizeOfHeaders) [0x00026] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System.ServiceModel.Channels/MessageImpl.cs:70

  at System.ServiceModel.Channels.Message.CreateMessage (System.Xml.XmlDictionaryReader envelopeReader, Int32 maxSizeOfHeaders, System.ServiceModel.Channels.MessageVersion version) [0x00022] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System.ServiceModel.Channels/Message.cs:427

  at System.ServiceModel.Channels.TextMessageEncoder.ReadMessage (System.IO.Stream stream, Int32 maxSizeOfHeaders, System.String contentType) [0x00011] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System.ServiceModel.Channels/TextMessageEncoder.cs:87

  at System.ServiceModel.Channels.HttpRequestChannel.GotResponse (IAsyncResult result) [0x001d6] in /Developer/MonoTouch/Source/mono/mcs/class/System.ServiceModel/System.ServiceModel.Channels/HttpRequestChannel.cs:284

  --- End of inner exception stack trace ---

  at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary () [0x0002d] in /Developer/MonoTouch/Source/mono/mcs/class/System/System.ComponentModel/AsyncCompletedEventArgs.cs:54

  at GetWallCompletedEventArgs.get_Result () [0x00000] in <filename unknown>:0

  at Spray.SprayUtils.HandleProxyGetWallCompleted (System.Object sender, .GetWallCompletedEventArgs e) [0x00000] in <filename unknown>:0


I can't seem to catch this exception in HandleProxyGetWallCompleted() so it bubbles up and crashes the program.
My code:

void HandleProxyGetWallCompleted (object sender, GetWallCompletedEventArgs e)
			//We don't really care too much if this fails
				Util.Debug("HandleProxyGetWallCompleted:DONE" + e.ToString());	
				if (e.Result != null)
					List<SprayMessage> msgs = new List<SprayMessage>();
					Util.Debug("HandleProxyGetWallCompleted:e.Result:" + e.Result.ToString());	
					msgs = e.Result.ToList();
					Util.Debug(String.Format("HandleProxyGetWallCompleted:Got{0} messages:", msgs.Count));
					int gotSprays= Util.Defaults.IntForKey("SpraysReceived");
					gotSprays += NewMessagesSeen(msgs);
					OnProxyGetWallCompleted(new ProxyGetWallCompletedEventArgs(msgs));
			catch (System.TimeoutException ex)
				Util.Debug("HandleProxyGetWallCompleted:" + ex.ToString());
				OnProxyGetWallCompleted(new ProxyGetWallCompletedEventArgs(null));
			catch (Exception ex)
				Util.Debug("HandleProxyGetWallCompleted:" + ex.ToString());
				OnProxyGetWallCompleted(new ProxyGetWallCompletedEventArgs(null));
Comment 1 Rolf Bjarne Kvinge [MSFT] 2012-09-12 19:09:06 UTC
Does it happen both when you're debugging and when you're running without the debugger? And do you get the same behavior between device and simulator?
Comment 2 Brian G 2012-09-12 21:39:26 UTC
I am having a similar issue. We are using slsvcutil-generated client code in MfA, and exceptions are not catchable. We've got catch blocks and we check the e.Error property, but the exceptions are happening either on a different thread or somewhere in system code and does not bubble up to our own code. Looks like a manifestation of this: https://bugzilla.novell.com/show_bug.cgi?id=691138
Comment 3 Brian G 2012-09-12 21:50:11 UTC
For me it happens when I'm USB-debugging on a device and when I'm just running on a device without debugging (it's a debug build still). I have not tried running in an emulator.
Comment 4 Rolf Bjarne Kvinge [MSFT] 2012-09-13 06:19:57 UTC
Brian, can you add a sample I can try? I'm not able to reproduce the behavior you're seeing from just your description of it.
Comment 5 Brian G 2012-09-13 15:21:46 UTC
We found a workaround!

We used SLsvcUtil.exe to generate the client code that we're using in Mono for Android.

Details of the PROBLEM:

We used the [MethodName]Async() methods, and we registered completion event handlers.
Example: if the web method is called GetData, we did something like:

        proxy.GetDataCompleted += OnCompleted;
    catch (Exception ex)
        proxy.GetDataCompleted -= OnCompleted;
        //handle exception

Problem: if there are network exceptions, including EndpointNotFound (just instantiate proxy with a bad URL), then the exceptions are NOT caught in our catch block. Even if we check the result of the "Error" property inside our OnCompleted handler, that doesn't work. Result: our app crashes, even if we try to catch and handle exceptions.

Details of WORKAROUND:

If you use the Begin/End/AsyncCallback mechanism instead, the exception handling works correctly!
See this page: http://msdn.microsoft.com/en-us/library/cc197937(v=vs.95).aspx (scroll down to the part about "Alternative Asynchronous Invocation Model").

Example for "GetData" method:

    AsyncCallback onCallback = (asyncResult) =>
            var data = ((IServiceType)proxy).EndGetData(asyncResult);
            // process the result data
        catch (Exception ex)
                // handle error

        ((IServiceType)proxy).BeginGetData(onCallback, proxy);
    catch (Exception ex)
        // handle error

So this is still a bug; the GetDataAsync() with Completed event handlers SHOULD handle exceptions by populating the "Error" property of the completed event args. But in the meantime, the above workaround should get people by.
Comment 6 Rolf Bjarne Kvinge [MSFT] 2012-10-22 08:20:13 UTC
I tried the test in this bug report: https://bugzilla.novell.com/show_bug.cgi?id=691138 (which uses events to attach to the Completed event), and I was not able to reproduce any issues.

Could you attach a test case that fails for you?
Comment 7 PJ 2013-11-19 17:05:10 UTC
This bug has been in the NEEDINFO state with no changes for the last 90 days. Can we put this back into the NEW or CONFIRMED state, or are we still awaiting response?

If there is no change in the status of this bug over the next two weeks, this bug will be marked as NORESPONSE.
Comment 8 PJ 2013-12-05 18:35:32 UTC
This bug has not been changed from the NEEDINFO state since my previous comment, marking as RESOLVED NORESPONSE.

Please feel free to REOPEN this bug at any time if you are still experiencing the issue. Please add the requested information and set the bug back to the NEW (or CONFIRMED) state.