CurrentDomain.AssemblyResolve not hit from Razor view

Topics: ASP.NET Web Pages
Jul 23, 2013 at 7:12 AM
I have a MVC 4 project where I load views from dynamically loaded assemblies. This works fine, as long as I do not use strongly typed views, as I get a "Type or Namespace not found" exception in that case.

I then tried to add a
AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve; 
to my Application_Start, but the event is never hit to resolve the assemblies searched by the razor view engine.
Same problem exists also for resources, but there it just uses the base culture and at least does not crash.

I know my event resolves the assemblies fine, as when I use the same classes in my code the event is hit and I can use the classes/resources fine.

Does anyone know how to resolve assemblies used in Razor views?

For now I use a dirty hack by copying the assemblies to the /bin folder, which basically works, but produces some other problems aside from beeing just a ugly hack.
I also tried AppendPrivatePath, which also did not help for the razor views.
Jul 25, 2013 at 2:43 PM
I found the solution at http://shazwazza.com/post/Developing-a-plugin-framework-in-ASPNET-with-medium-trust:
  • add a private path to the web.config
  • add a PreApplicationStartMethod and do the following in that method:
  • copy all plugin DLLs to the folder linked as a private path above
  • load the assemblies via Assembly.Load(path) and add them to the BuildManager via BuildManager.AddReferencedAssembly(a);
Now the razor engine is able to load types out of this assemblies.