1

Our continuous integration builds on Azure DevOps just started having this problem running MSTest unit tests. Didn't update any of this code. Happening with Unity.Container.dll version 5.11.11.0 s well as 5.11.8.0 on older projects. Not able to reproduce locally in visual studio. any ideas?? Testng c# class libraries using .net Framework 4.8. This is the code in TestInitialize:

var container = new UnityContainer();

Initialization method MyTestClass.ATestInitialize threw exception. System.TypeInitializationException: The type initializer for 'Unity.Lifetime.TransientLifetimeManager' threw an exception. ---> System.Security.VerificationException: Operation could destabilize the runtime..

Stack trace at Unity.Lifetime.TransientLifetimeManager..cctor() in C:\projects\abstractions\src\Lifetime\Managers\TransientLifetimeManager.cs:line 26

This is using an Azure Pipelines agent pool running Windows 2019.

dan
  • 801
  • 15
  • 41
  • Can you provide more details about what is happening in the `MyTestClass.ATestInitialize()` method? Also is this xUnit, MSTest, NUnit? What type of project is it testing (asp.net, wpf, class library, etc)? – bryanbcook Dec 17 '21 at 22:21
  • Also, because this is happening on your build server and not on your local machine, can you provide more details about the build agent? Are you using a microsoft-provided cloud-hosted build agent or are you run a self-hosted build agent. In both, please specify which OS you're using. – bryanbcook Dec 17 '21 at 22:23
  • @byranbcook thank you for response. I updated desc to include MSTest and Azure Pipelines Windows 2019 server. It already mentioned the code it is just creating new UnityContainer object. does this help? how can I track this down? thank you – dan Dec 20 '21 at 02:28
  • What type of app is being tested? Web app, class library, etc? – bryanbcook Dec 20 '21 at 05:32
  • class libraries. .Net Framework 4.8, c#, sorry i forgot to answer that part thank you – dan Dec 21 '21 at 14:12

2 Answers2

2

The reference to System.Security.VerificationException: Operation could destabilize the runtime can occur when the application code attempts to do something that isn't allowed under the current security context. Despite the fact that you haven't changed the code, it is likely environmental in nature. It's highly likely that you have some sort of security demands in your application code and the subject under test is doing something that requires something that isn't available on a different machine. The fact that this is occurring on a build agent is somewhat irrelevant as this problem would likely occur for someone else on a machine without the same demands.

For example, CAS policies are disabled on your machine, or the application requires a certificate or signing key that isn't installed on the remote. It could be that the application code is dynamically emitting IL instructions and your application requires that the code is signed but no longer isn't.

It's also possible that if you're using a Microsoft provided cloud-hosted build agent, they're running newer security patched versions of runtimes and your machine doesn't experience the same issue.

The description for VerificationException states:

The exception that is thrown when the security policy requires code to be type safe and the verification process is unable to verify that the code is type safe

There is a similar question posted here. Some evidence suggests:

bryanbcook
  • 16,210
  • 2
  • 40
  • 69
0

Issue: Long back, atleast 6 months ago, i was fixing a bug and removed one of the assembly from the IDE folder of Visual studio

Finding the Issue with Unity: Suddenly, my unit tests started failing and I was looking for solutions around. (The issue was exactly what is mentioned here, but not on Azure, it was on my local machine only)

Solution: Despite we updating our VS IDE doesnt resolve this problem, all i had to do is remove public and reference assemblies folder from my Visual studio IDE location Common7/IDE and remove these folders (take a backup) and then open VS installer and repair the Visual studio.

That fixed my issue with this exception.