简体   繁体   English

Visual Studio OrderedTests:无法使用已与其底层 RCW 分离的 COM 对象

[英]Visual Studio OrderedTests: COM object that has been separated from its underlying RCW cannot be used

First off, the technologies being used here include: Visual Studio, C#, Selenium WebDriver, Google Chrome, and MS Excel.首先,这里使用的技术包括:Visual Studio、C#、Selenium WebDriver、Google Chrome 和 MS Excel。

I'm in the process of building an automation framework to test multiple web applications.我正在构建一个自动化框架来测试多个 Web 应用程序。 I'm using an Excel spreadsheet to pull variable data in so that other testers have a much easier time using the tests.我正在使用 Excel 电子表格来提取可变数据,以便其他测试人员可以更轻松地使用测试。 During our smoke test, we run through several of the Unit Tests that I've built in order.在我们的冒烟测试期间,我们运行了几个我按顺序构建的单元测试。 Therefor, I figured it would be a good idea to create an Ordered Test.因此,我认为创建有序测试是个好主意。 When running each test individually, they work just fine.单独运行每个测试时,它们工作得很好。 However, once I start running the Ordered Tests, the first test in the list will run, but then I get this error:但是,一旦我开始运行有序测试,列表中的第一个测试将运行,但随后出现此错误:

System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used. System.Runtime.InteropServices.InvalidComObjectException:无法使用已与其基础 RCW 分离的 COM 对象。

I create the references to my Excel spreadsheet in a class called ReadFile:我在一个名为 ReadFile 的类中创建了对我的 Excel 电子表格的引用:

static Excel.Application xlApp = new Excel.Application();
static Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"SPREADSHEET LOCATION");
static Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
static Excel.Range xlRange = xlWorksheet.UsedRange;

When my tests pass or fail, they call the CleanUp method, which consists of quitting the chromedriver and calling the ReadFile.Close() method:当我的测试通过或失败时,它们会调用 CleanUp 方法,该方法包括退出 chromedriver 并调用 ReadFile.Close() 方法:

xlWorkbook.Save();

xlWorkbook.Close(Type.Missing, Type.Missing, Type.Missing);

xlApp.Quit();

GC.Collect();
GC.WaitForPendingFinalizers();

I've done quite a bit of research and found that there may be need to invoke Mashall.FinalReleaseComObject.我做了很多研究,发现可能需要调用 Mashall.FinalReleaseComObject。 I've tried using this as well, but it doesn't seem to make a difference.我也尝试过使用它,但似乎没有什么不同。 I have also noticed that the Excel process continues to run for a few seconds after the first test finishes.我还注意到 Excel 进程在第一次测试完成后继续运行几秒钟。 So I tried sleeping the thread to make it wait for Excel to completely close before the next test starts, but it also immediately throws the exception when Excel is removed (even if the sleep timer hasn't completed).所以我尝试让线程休眠,让它在下一个测试开始之前等待 Excel 完全关闭,但是当 Excel 被删除时它也会立即抛出异常(即使睡眠定时器还没有完成)。

Anyways, at this point I'm looking for any guidance possible and it is greatly appreciated.无论如何,在这一点上,我正在寻找任何可能的指导,非常感谢。 Please keep in mind that I'm not an expert with any of these technologies.请记住,我不是任何这些技术的专家。

Are you running/testing this in debug mode?您是否在调试模式下运行/测试它? If so, the GC is overlooking the COM object and it is not being released properly.如果是这样,则 GC 忽略了 COM 对象并且它没有被正确释放。 That's about as well as I understand it.这和我理解的差不多。 I found this solution as an answer in another SE thread recently.我最近在另一个 SE 线程中找到了这个解决方案作为答案。 If I can find the thread I'll link it.如果我能找到线程,我会链接它。 The solution to this is to run in release mode and see if the issue persists.解决方案是在发布模式下运行,看看问题是否仍然存在。

Edit: Here is the thread explaining why debug mode causes GC to overlook some things.编辑:是解释为什么调试模式会导致 GC 忽略某些事情的线程。

Other than that, there are now good open source libraries (nuget packages) you coud use to read excel files, almost the same way you do with excel interop, but without excel, thus avoiding all possibe COM hurdles.除此之外,现在有很好的开源库(nuget 包)可以用来读取 excel 文件,几乎与使用 excel 互操作的方式相同,但没有 excel,从而避免了所有可能的 COM 障碍。 Take a look at EPPlus or ExcelDataReader for example.例如,看看EPPlusExcelDataReader

This exception:这个例外:

System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.

as well as:也:

System.Runtime.InteropServices.InvalidComObjectException: Attempt has been made to use a COM object that does not have a backing class factory.

Can be caused by releasing the COM object before you are done using it.可能是由于在您完成使用 COM 对象之前释放它造成的。 So, check and double check your calls to Marshal.ReleaseComObject and make sure you are not trying to access the object after the call.因此,请检查并仔细检查您对Marshal.ReleaseComObject的调用,并确保您在调用后没有尝试访问该对象。

Note: I don't this was the OP's problem, but this may help others who arrive here looking for information about these exceptions.注意:我不认为这是 OP 的问题,但这可能会帮助到达这里的其他人寻找有关这些异常的信息。

暂无
暂无

声明:本站的技术帖子网页,遵循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 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 “无法使用与其基础RCW分开的COM对象”? - “COM object that has been separated from its underlying RCW cannot be used”?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM