在執行以下兩個測試用例之後,將COM執行打印到控制檯。我究竟做錯了什麼?使用WPF退出時的COM異常
如果我單獨運行任何一個測試,或者如果我一起運行這兩個測試,那麼這個異常就會正好寫入控制檯一次。這讓我懷疑有某種每AppDomain資源,我沒有清理。
我已經嘗試過使用NUnit和MSTest的測試,在兩種環境中都有相同的行爲。 (其實,我不知道,如果在一個異常打印或兩個運行在MSTest的結果這兩個試驗。)
例外:
System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.
at System.Windows.Input.TextServicesContext.StopTransitoryExtension()
at System.Windows.Input.TextServicesContext.Uninitialize(Boolean appDomainShutdown)
at System.Windows.Input.TextServicesContext.TextServicesContextShutDownListener.OnShutDown(Object target)
at MS.Internal.ShutDownListener.HandleShutDown(Object sender, EventArgs e)
測試代碼:
using NUnit.Framework;
namespace TaskdockSidebarTests.Client
{
[TestFixture, RequiresSTA]
public class ElementHostRCWError
{
[Test]
public void WinForms()
{
var form = new System.Windows.Forms.Form();
var elementHost = new System.Windows.Forms.Integration.ElementHost();
form.Controls.Add(elementHost);
// If the form is not shown, the exception is not printed.
form.Show();
// These lines are optional. The exception is printed with or without
form.Close();
form.Controls.Remove(elementHost);
elementHost.Dispose();
form.Dispose();
}
[Test]
public void WPF()
{
var window = new Window();
// If the window is not shown, the exception is not printed.
window.Show();
window.Close();
}
}
}
也許http://social.msdn.microsoft.com/forums/en-US/vststest/thread/e53fdc45-23f3-4aee-aad9-f63769f2c638/幫助 – 2011-06-03 22:03:27
可悲的是,我不能使用MTA,因爲WPF需要STA。在SetUp中創建表單和元素主機似乎也無法做到這一點。哎呀。 – 2011-06-03 22:50:04
如果我沒有弄錯,這個異常不會導致unittest失敗,是嗎?在單元測試我的WPF控件時,我遇到了同樣的異常,我選擇忽略它;;) – Bubblewrap 2011-06-04 11:18:10