抛出“尝试访问已卸载的AppDomain”后Outlook崩溃。例外

时间:2011-03-23 10:21:31

标签: c# vsto enterprise-library outlook-addin

我使用c#3.5,VSTO和Visual Studio 2008 for Outlook 2003开发了一个outlook插件。

代码几乎所有时间都能正常运行,但有时候在抛出“试图访问已卸载的AppDomain”后Outlook崩溃了。例外

异常stackTrace:

03/17/2011 8:17:05 AM : DoSomething_Outlook_Startup: 
      exception:The current build operation (build key Build 
      Key[Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicyImpl, DoSomething Notify Policy]) 
      failed: Attempted to access an unloaded AppDomain. 
      (Strategy type ConfiguredObjectStrategy, index 2)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.GetExceptionPolicy(Exception exception, String policyName, ExceptionPolicyFactory factory)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception exceptionToHandle, String policyName, ExceptionPolicyFactory policyFactory)
   at Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(Exception exceptionToHandle, String policyName)
   at DoSomething.OutlookAddIn.DoSomething_Outlook.PaintMainMenu(Application objApp)
   at DoSomething.OutlookAddIn.DoSomething_Outlook.DomeSomething_Outlook_Startup(Object sender, EventArgs e)

Outlook Addin Code:

//Startup handler
private void DoSomeThing_Outlook_Startup(object sender, System.EventArgs e)
        {
            try
            {
                applicationObject = this.Application;
..
..
..
                    PaintMainMenu(applicationObject);
            }
            catch (Exception ex)
            {
        //Exception being thrown from PaintMainMenu is handled here
        //
                Logger.Log("DoSomething_Outlook_Startup: exception:" + ex.Message + Environment.NewLine + ex.StackTrace);
            }
        }

//Method to add DoSomething menu item to File menu
private void PaintMainMenu(Outlook.Application objApp)
{
            try
            {

                objApp.ActiveExplorer().CommandBars["File"].Reset();


                menubar = objApp.ActiveExplorer().CommandBars.ActiveMenuBar;
                Office.CommandBarPopup cbc = (Office.CommandBarPopup)
                           menubar.FindControl
                           (Office.MsoControlType.msoControlPopup, 30002, Missing.Value, Missing.Value, Missing.Value);

                if ((menubar != null))
                {
                    _DoSomething = (Office.CommandBarButton)cbc.Controls.Add(
                                 Office.MsoControlType.msoControlButton, Missing.Value,
                                 Missing.Value, 6, true);

                    if (_DoSomething != null)
                    {
                        _DoSomething.Tag = AddInConstants.C_Menubar_Menu_Tag;
                        _DoSomething.Caption = AddInConstants.C_Menubar_Menu_Caption;

                        _DoSomething.Click += new _CommandBarButtonEvents_ClickEventHandler(DoSomething_Click);
            _DoSomething.Enabled = false;
                    }
                }
            }
            catch (Exception ex)
            {
  //below line throws "Attempted to access an unloaded AppDomain."
                bool rethrow = ExceptionPolicy.HandleException(ex, AddInConstants.C_Global_NotifyPolicy);
                if (rethrow)
                    throw;
            }
}

提前致谢, 与Hemant

1 个答案:

答案 0 :(得分:0)

这可能是竞争条件。

您正在尝试向Outlook菜单添加内容。如果您的代码在加载菜单之前运行,您将收到此错误。

您可以尝试在代码中等待,以确保已加载菜单。