Handling Custom Assembly Attributes in Unit Tests After Migrating from .NET Framework 4.8 to .NET 8
I recently migrated my app from .NET Framework 4.8 to .NET 8 and ran into an issue with unit tests in MS Test. The problem is related to the OriginSetupPackage custom attribute applied to the entry assembly.
In .NET Framework 4.8, the Product class loads this custom attribute from the entry assembly, like so:
The Product class reads the attribute in its static constructor:
This works fine in the app, but in unit tests, the entry assembly is testhost.dll, which doesn't have the required attribute. This causes the following exception:
In .NET Framework 4.8, we worked around this by manipulating AppDomain via AppDomainManager to set the test assembly as the entry assembly, like this:
This allowed us to set the entry assembly correctly for tests. However, in .NET 8, AppDomainManager is deprecated, and manipulating the entry assembly directly is no longer possible. In MS Test, the entry assembly is now testhost.dll, leading to the same exception.
How can this be resolved in .NET 8 without modifying the Product class?
In .NET Framework 4.8, the Product class loads this custom attribute from the entry assembly, like so:
The Product class reads the attribute in its static constructor:
This works fine in the app, but in unit tests, the entry assembly is testhost.dll, which doesn't have the required attribute. This causes the following exception:
In .NET Framework 4.8, we worked around this by manipulating AppDomain via AppDomainManager to set the test assembly as the entry assembly, like this:
This allowed us to set the entry assembly correctly for tests. However, in .NET 8, AppDomainManager is deprecated, and manipulating the entry assembly directly is no longer possible. In MS Test, the entry assembly is now testhost.dll, leading to the same exception.
How can this be resolved in .NET 8 without modifying the Product class?

