Error while unloading AppDomain. (Exception from HRESULT: 0x80131015) - c#

I am having this error after using ReportViewer. Upon exit it has this error. Don't know what is causing this. I am using C#.

This is a reported Microsoft bug. There is a workaround for it - to call reportViewer.LocalReport.ReleaseSandboxAppDomain() method before closing the parent form.
Example:
private void frmMyForm_FormClosing(object sender, FormClosingEventArgs e)
{
reportViewer1.LocalReport.ReleaseSandboxAppDomain();
}
Reference: Weird behaviour when I open a reportviewer in WPF

Related

C# .NET VSTO Excel Add-In Global Error Handler Options

I have an Excel Add-In created using VSTO and C# .NET. I am interested in creating a handler for unhandled exceptions. Based on some research and other posts, there are a couple of events that I can register for and they seem to do the trick.
So, something like this:
private void ThisAddIn_Startup(object sender, EventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += this.CurrentDomain_UnhandledException;
}
private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
var errorDialog = new ErrorDialog("Something went wrong", e.ExceptionObject as Exception);
errorDialog.ShowDialog();
}
Great. HOWEVER, as soon as the error dialog shows and exits, i.e. the code above returns/completes, the code that triggered the exception resumes exactly where it left off, thus re-triggering the exception. It becomes an infinite loop.
In WPF, we would simply mark it as handled, (e.Handled = true;) and that would take care of it. However I don't see a similar option here in WinForms.
What are my options with Excel Add-Ins VSTO/Win Form exception handlers?
The only thing I can think of is to catch the first event and ask the user to restart the app.
There has to be something better. How can I really handle the exception?

InvalidOperationException on Application.Exit

Can someone tell me why the exitToolStripMenuItem_Click throws an InvalidOperationException. I know it happens due to plugin.Close() being called. However, I do not understand why. Closing the Form1 via the "X" button does not trigger the exception. Calling Application.Exit() does though. Below is a sample to demonstrate what is happening in my main app. In my main app events are triggered off certain forms closing so I need to make sure I call Close on each forms. I could change Application.Exit() to a Close() however after reading MSDN I don't feel like this is the correct solution. Any ideas would be helpful, thanks.
Note: The main application I'm working on is multi-threaded.
public partial class Form1 : Form
{
Form plugin = new Form();
public Form1()
{
InitializeComponent();
plugin.Show();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
plugin.Close();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
The exception thrown is:
Exception thrown: 'System.InvalidOperationException' in mscorlib.dll
System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.Collections.ArrayList.ArrayListEnumeratorSimple.MoveNext()
at System.Windows.Forms.Application.ExitInternal()
at System.Windows.Forms.Application.Exit(CancelEventArgs e)
at System.Windows.Forms.Application.Exit()
at WindowsFormsApplication2.Form1.exitToolStripMenuItem_Click(Object sender, EventArgs e)
You don't need Application.Exit() in exitToolStripMenuItem_Click(). Calling Application.Exit, will close and dispose the form, while resuming the execution at exitToolStripMenuItem_Click, ObjectDisposedException would be thrown. Also, System.InvalidOperationException would be thrown as Windows Forms collection has been modified.

RuntimeBinderException occured in use dynamic keyword

I have a silverlight 5 application running in Out-Of-Browser mode and with elevated trust mode. I have added a button with the below mentioned click handler code.
private void Button_Click(object sender, RoutedEventArgs e)
{
using (dynamic shell = AutomationFactory.CreateObject("WScript.Shell"))
{
shell.Run(#"C:\windows\notepad.exe");
}
}
Upon executing the application and clicking on the button, I am getting a RuntimeBinderException stating
'object' does not contain a definition for 'Run'
I found a similar question posted here but no one has provided a solution for the same. Hence reposting the question.
Please help.

Why is Method.Invoke generating unhandled exceptions? Unable to catch even with TargetInvocationException

I'm trying to use Method.Invoke to call a windows form dialog, have a user perform some selections/interaction and continue execution. This invoke call is happening in an asynchronous method.
While everything works fine, should an error occur on the windows form, an unhandled exception is thrown, even when trying to catch TargetInvocationException or just Exception.
Both forms are in the same winforms project. I realize where are other ways to perform an async call but this is just to illustrate the issue.
The form dialog is as follows:
public partial class FakeDialog : Form
{
public FakeDialog()
{
InitializeComponent();
}
private void btnOK_Click(object sender, EventArgs e)
{
throw new Exception("oh noes!");
this.DialogResult = DialogResult.OK;
this.Close();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
this.Close();
}
public new DialogResult ShowDialog()
{
base.ShowDialog();
return this.DialogResult;
}
}
And here is the calling code. None if the catch blocks are being executed, even when not debugging (my problem is not debugging exceptions in the IDE as mentioned here. The following results in an unhandled exception).
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MethodInvoker simpleDelegate = new MethodInvoker(InvokeForm);
IAsyncResult tag = simpleDelegate.BeginInvoke(null, null);
simpleDelegate.EndInvoke(tag);
MessageBox.Show("All done");
}
private void InvokeForm()
{
try
{
Type t = typeof(FakeDialog);
MethodInfo showDialogMethod = t.GetMethod("ShowDialog", new Type[] { });
object dialog = Activator.CreateInstance(t);
System.Windows.Forms.DialogResult result = (System.Windows.Forms.DialogResult)showDialogMethod.Invoke(dialog, new object[] { });
MessageBox.Show(result.ToString());
}
catch (TargetInvocationException tie)
{
MessageBox.Show("Tie exception");
}
catch (Exception ex)
{
MessageBox.Show("general exception");
}
}
}
UPDATE:
Strangely, I'm able to catch the exception when running with debugging (I'm sure the IDE is helping here). Running without debugging causes the unhandled exception.
Also, invoking via an async call doesnt seem to make a difference. If i just call InvokeForm() (ignore all the methodInvoker stuff), I can achieve the same result.
Operating on .NET 2.0 using Visual Studio 2008.
Ok, figured it out.
The result from the code was unhandled exceptions. And while using Method.Invoke for a simple method in another class would behave correctly, the environment changes with the source of the exception occurring in a form. A form event. And I eventually found on Microsoft Support that unhandled exceptions in Windows Form events are not propagated up call stack. There's some pretty interesting reasons for this ("Windows Forms applications have a top-level exception handler that allows the program to continue to run if it can recover").
It also gives credence to what Marc mentioned over putting things in the Load event. Sigh. So the reason for all this is pretty obvious now.
As for the code running fine while debugging (as opposed to without), I think I can thank the JIT debugger for that. #fluf pointed to me that specifically enabling the JIT debugger gave the same result as running with debugging. #Marc Gravell, depending on VS settings this might explain only I and fluf could reproduce. There's some more info on it here but it's not a production fix.
So the real solution is either handling the exceptions in the event handlers themselves or use the solution as mentioned in the Microsoft Support article above, which solves my issues.
I can't actually repro from your code, but: the Load event is.... different, and some odd things can happen if you get an exception inside the Load event. Simply my advice would be: move this code out of the Load event. It also doesn't help that attaching a debugger here changes the behaviour (a Heisenbug).
Without seeing MethodInvoker's declaration i can only guess, but it is possible that InvokeForm() method is executed on non-UI thread.
MethodInvoker simpleDelegate = new MethodInvoker(InvokeForm);
IAsyncResult tag = simpleDelegate.BeginInvoke(null, null);
To show a dialog you may consider to rewrite this as follows:
Action simpleDelegate = new Action(InvokeForm);
this.BeginInvoke(simpleDelegate);

VSTO Outlook 2007 Addin Startup throws System.Runtime.InteropServices.COMException (0x80020009): Cannot complete the operation. You are not connected

We are building an Outlook 2007 add-in using VSTO. We have code in the initialization logic of the addin which retrieves the MAPIOBJECT associated with the Application.Session. The code in question is as follows:
public partial class ThisAddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
try
{
var addIn = Globals.ThisAddIn;
var application = addIn.Application;
var mapiObject = application.Session.MAPIOBJECT;
}
catch (Exception e)
{
// HANDLE ERROR
}
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
// ...
}
// Other VSTO generated code
}
This code works like a charm for most times. However, we run into the following error when we attempt to access the MAPIOBJECT property from the Session property:
System.Runtime.InteropServices.COMException (0x86220009): Cannot complete the operation. You are not connected.
at Microsoft.Office.Interop.Outlook._NameSpace.get_MAPIOBJECT()
at MyTestAddIn.ThisAddIn.ThisAddIn_Startup(object, System.EventArgs) in C:\foo\MyTestAddIn\ThisAddIn.cs:line 19
We are unable to understand why the code works most of the time but fails at a few. We thought this issue was due a network interruption/user working offline. We tried several combinations of connection states with intentionally working offline from Outlook to disconnecting the network cable when Outlook is about to load the add-in. But we have not been able to reproduce the problem.
Any help is appreciated.
It's usually a bad idea to ask for the MAPI session that early, because it might not be properly logged on yet. Why not ask for the session the moment you actually need it?

Categories

Resources