微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决抛出“尝试访问已卸载的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,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);

                if ((menubar != null))
                {
                    _DoSomething = (Office.CommandBarButton)cbc.Controls.Add(
                                 Office.MsoControlType.msoControlButton,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菜单中添加一些内容。如果您的代码在菜单加载之前运行,则会出现此错误。 您可以尝试在代码中稍等一下,以确保菜单已加载。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。