Skip to content

Instantly share code, notes, and snippets.

@LevYas
LevYas / RegFreeComActivationContext.cs
Last active August 28, 2024 04:49
Utility to unit test code with COM components in .NET using xUnit
// I worked on a C# WPF application that used C++/CLI application that, in turn,
// used regfree COM component.
// I'm a huge fan of automated tests, so I wanted to find a way how to run integration tests given
// the conditions above. I managed to find a way to do it. In short:
// 1. Disable shadow copying, because the modules need to be near each other
// 2. Disable parallelization
// (use provided xunit.runner.json to achieve that, place it in the root of your test project)
// 3. Make tests run in Single Thread Apartment instead of default MTA by using the Xunit.StaFact
// (see TestSample.cs)
// 4. Use a magical utility (from this file) to prepare activation context to use COM components.