简体   繁体   English

已经与其底层RCW分离的COM object不能使用,只能在测试调试中使用

[英]COM object that has been separated from its underlying RCW cannot be used, only in test debug

I am starting a WPF application from an MSTest (as a STA thread).我正在从 MSTest(作为 STA 线程)启动 WPF 应用程序。 I start the application in the assembly initialize and wait for it to complete loading.我在程序集初始化中启动应用程序并等待它完成加载。 Both when I manually close the main window (which was opened from the test) and when I close it in the assembly cleanup (automated way), after the assembly cleanup method is finished I always get the COM object that has been separated from its underlying RCW cannot be used, only in test debug exception.当我手动关闭主要 window (从测试中打开)和当我在程序集清理(自动方式)中关闭它时,在程序集清理方法完成后,我总是得到COM object that has been separated from its underlying RCW cannot be used, only in test debug异常时使用。 I tried to wait for GC to finish and also call Dispatcher.CurrentDispatcher.InvokeShutdown();我试图等待 GC 完成并调用Dispatcher.CurrentDispatcher.InvokeShutdown(); in the cleanup as suggested in similar threads, but no luck.在类似线程中建议的清理中,但没有运气。

Note, that in the actual test case I was not doing anything, as I wanted to minimize the scenario and make sure that this exception is thrown just by opening and closing the application.请注意,在实际测试用例中,我没有做任何事情,因为我想最小化场景并确保仅通过打开和关闭应用程序来引发此异常。 Of course, when I just run the application manually and not from the test, I don't get any exception when closing the window.当然,当我只是手动运行应用程序而不是从测试中运行时,关闭 window 时不会出现任何异常。

Any other suggestions to prevent this?还有其他建议可以防止这种情况吗?

When running and no debugging my test, the execution completes successfully and VS can continue with running the tests of the next scheduled project (which is not related to starting the application).当运行并且没有调试我的测试时,执行成功完成并且VS可以继续运行下一个计划项目的测试(与启动应用程序无关)。

Stack trace堆栈跟踪

PresentationCore.dll!System.Windows.Input.TextServicesContext.StopTransitoryExtension() 

PresentationCore.dll!System.Windows.Input.TextServicesContext.Uninitialize(bool appDomainShutdown)  

WindowsBase.dll!MS.Internal.ShutDownListener.HandleShutDown(object sender, System.EventArgs e)  


Repro :复制

  • Create a new C# WPF .NET Framework solution (eg. WpfApp1 )创建新的 C# WPF .NET 框架解决方案(例如WpfApp1
  • In the autogenerated MainWindow.xaml add a simple empty grid在自动生成的MainWindow.xaml添加一个简单的空网格
<Window x:Class="WpfApp1.MainWindow"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
       xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
       xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
       mc:Ignorable="d"
       Title="MainWindow" Height="450" Width="800">
   <Grid>
   </Grid>
</Window>
  • Add a Program.cs file with the following code:添加具有以下代码的Program.cs文件:
public static class Program
   {
       [STAThread]
       public static void Startup()
       {
           var window = new MainWindow();
           window.Show();
           window.Close();
       }
   }
  • Create a C# MSTest project in the solution (eg. UnitTestProject1 )在解决方案中创建一个 C# MSTest 项目(例如UnitTestProject1
  • Add the WPF project as a reference in the MSTest project在 MSTest 项目中添加 WPF 项目作为参考
  • Add following code in the unit test class在单元测试 class 中添加以下代码
[TestClass]
   public class UnitTest1
   {
       [AssemblyInitialize]
       public static void Initialize(TestContext _)
       {
           var mainThread = new Thread(() =>
           {
               WpfApp1.Program.Startup();
           });
           mainThread.SetApartmentState(ApartmentState.STA);
           mainThread.Start();
           mainThread.Join();
       }

       [TestMethod]
       public void TestMethod1()
       {
       }
   }
  • Debug the TestMethod1调试TestMethod1

Dispatcher.CurrentDispatcher.InvokeShutdown(); needs to get called within the mainThread (right after the call to WpfApp1.Program.Startup(); )需要在mainThread中调用(在调用WpfApp1.Program.Startup();之后)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 静态C#对象原因:无法使用与其基础RCW分离的COM对象 - static C# object causes: COM object that has been separated from its underlying RCW cannot be used 无法使用已与其基础RCW分离的COM对象 - COM object that has been separated from its underlying RCW cannot be used 与它的基础RCW分开的COM对象不能使用 - COM object that has been separated from its underlying RCW cannot be used 与基础RCW分开的COM对象不能使用? - COM object that has been separated from its underlying RCW cannot be used? 与基础RCW分离的COM对象不能与Outlook一起使用 - COM object that has been separated from its underlying RCW cannot be used with Outlook UnitTesting:无法使用已与其基础RCW分离的COM对象 - UnitTesting : COM object that has been separated from its underlying RCW cannot be used Visual Studio OrderedTests:无法使用已与其底层 RCW 分离的 COM 对象 - Visual Studio OrderedTests: COM object that has been separated from its underlying RCW cannot be used System.Runtime.InteropServices.InvalidComObjectException: COM object 已与其底层RCW分离,无法使用 - System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used WPF线程:“无法使用与其基础RCW分离的COM对象。” - WPF Thread: “COM object that has been separated from its underlying RCW cannot be used.” 不能使用与其底层 RCW 分离的 COM 对象。 在 oledb - COM object that has been separated from its underlying RCW cannot be used. in oledb
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM