If we carefully consider the stack trace, we can see exactly what is happening here. The RegisterWebApiControllers extension method calls the GetControllerTypes method on the GetControllerTypes instance, which it grabs from HttpConfiguration , and it passes the IAssembliesResolver , which is also retrieved from the configuration. The GetControllerTypes method called ( WebHostHttpControllerTypeResolver ) calls GetControllerTypes DefaultHttpControllerTypeResolver , which ultimately calls the GetReferencedAssemblies call of the GetReferencedAssemblies class.
However, System.Web.Compilation.BuildManager cannot be called at the beginning of the ASP.NET pipeline or outside the ASP.NET context. Since you are in a test, BuildManage will throw an exception that you are experiencing.
So the solution (or the โtrickโ, so you will be here) should replace the standard IAssembliesResolver in unit testing. I assume the resolver is as follows:
public class TestAssembliesResolver : IAssembliesResolver { public ICollection<Assembly> GetAssemblies() { return AppDomain.CurrentDomain.GetAssemblies(); } } [TestMethod] public void TestMethod1() {
Itโs a little annoying that you have to deal with this, especially since Simple Injector was designed for verification. It looks like we missed this by integrating the RegisterWebApiControllers extension method so deep into the Web API. We should take a step back and think about how to simplify the verification of the web API configuration inside the unit test.
source share