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

发布于 2024-10-25 05:34:10 字数 3366 浏览 2 评论 0原文

我使用 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;
            }
}

提前致谢, 赫曼特

I've developed a outlook addin with c# 3.5, VSTO and Visual Studio 2008 for Outlook 2003.

The code works fine almost all time but sometimes Outlook crashes after throwing "Attempted to access an unloaded AppDomain." exception

Exception 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;
            }
}

Thanks in advance,
Hemant

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失与倦" 2024-11-01 05:34:10

这可能是一种竞争条件。

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

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

This is probabaly a race condition.

You are trying to add something to the Outlook menu. If your code runs before the menu has loaded, you will get this error.

You could try putting a wait in your code, so that you are sure that the menu is loaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文