VS2012 R#7 How to catch Exception thrown by callback using Enqueue

Oct 3, 2012 at 2:25 PM


I'm running my unit tests from VS2012 with R#7 and I have a problem with the following test:

        public void Some_Test()
            var fileTestData = new FileTestData();

            Action callback = () =>
                fileTestData.Done = true;

            EnqueueCallback(() =>

            EnqueueConditional(() => fileTestData.Done);


SomeAsyncMethodWithCallback is a method that performs some async operation, then call the callback when done with a code like this one:

        private static void SomeMethodCompleted(object sender, SomeMethodCompletedEventArgs e)
            var manager = (Manager)sender;
            manager.SomeMethodCompleted -= SomeMethodCompleted;
            var callback = (Action)manager.ContextState;
            if (e.Error != null)
                throw e.Error;

              // do some stuff
            if (callback != null)

When the exception is thrown in the callback (red line)

1/ I can't catch it

2/ the unit test never ends

I have the same problem if I try to send the exception on the UI thread using:


new DispatcherSynchronizationContext().Post(_ => { throw e.Error; }, null);



I tried to register to the AppDomain.CurrentDomain.UnhandledException in the SilverlightTestBase.Initialize() method (marked with [TestInitialize]) but it doesn't work (MethodAccessException because of Security Critical section)



Oct 3, 2012 at 3:11 PM

Try this version of AgUnit, It has better error handling:

UnhandledException in Silverlight you need to do on Application:
Application.Current.UnhandledException += ...

I assume this problem originates in StatLight, which is what AgUnit uses under the covers.
You might have better luck searching through their issues / discussions.
You can also try using their command-line runner end see if your tests work with that. 


I'll try to reproduce your issue but I don't have time to spare for the next few days.