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

| 我已经使用c#3.5,VSTO和Visual Studio 2008 for Outlook 2003开发了一个Outlook加载项。 该代码几乎可以正常工作,但是有时Outlook抛出“尝试访问已卸载的AppDomain。”异常后会崩溃。 异常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加载项代码:
//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;
            }
}
提前致谢, 赫曼特
已邀请:
这很可能是比赛条件。 您正在尝试向Outlook菜单中添加一些内容。如果您的代码在菜单加载之前运行,则会出现此错误。 您可以尝试在代码中稍等一下,以确保菜单已加载。

要回复问题请先登录注册