C#C
C#11mo ago
6 replies
kniemiec

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:

see code sample 1 in attached screenshot


The Product class reads the attribute in its static constructor:

see code sample 2 in attached screenshot


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:

see code sample 3 in attached screenshot


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:

see code sample 4 in attached screenshot


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?
code-sample.png
Was this page helpful?