2011-03-23 32 views
0

我已經開發了C#3.5的前景插件,VSTO併爲Outlook 2003拋出後Outlook崩潰「試圖訪問卸載的AppDomain」。例外

代碼Visual Studio 2008中正常工作,幾乎所有的時間,但有時展望拋後崩潰「試圖訪問已卸載的AppDomain。」例外

異常堆棧跟蹤:

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) 

展望外接程序代碼:

//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

+0

只是好奇:你試圖處理的原始異常是什麼? – 2011-03-23 15:29:15

+0

任何一種異常。實際上,PaintMainMenu引發一個異常,並且此函數中的catch部分捕獲並傳遞給Enterprise Library的ExceptionPolicy.HandleException,以根據配置中定義的策略進行處理。但是它(即企業庫)引發了「訪問卸載的應用程序域」異常,然後通過調用者函數,即DoSomeThing_Outlook_Startup捕獲異常。 – 2011-03-25 13:25:46

回答

0

這是一個probabaly競爭條件。

您正試圖向Outlook菜單添加內容。如果你的代碼在菜單加載之前運行,你會得到這個錯誤。

您可以嘗試在代碼中等待,以確保菜單已加載。

+0

感謝hlep。但是,如果菜單項引發異常,企業庫應該能夠處理它。這裏的例外是關於從企業庫類中卸載的域。由於Logger.Log語句能夠執行,因此未卸載默認域。但我會試一試。再次感謝 – 2011-04-21 13:55:11