RazorGenerator.Mvc.dll not loading when deployed
Asked Answered
W

1

7

have everything working fine with mvc.razor generator. Views from my library project show up fine in my web-site. It is a .net 4.0, MVC4, EF5 project. The app pool is set to .net 4.0 in IIS.

When I deploy to the server, I am getting an exception when it tries to load the PrecompiledMvcEngine type. Here is the exception (I have attached the full output of error in attachments):

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 


[ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.]
System.Reflection.RuntimeModule.GetTypes(RuntimeModule module) +0
System.Reflection.Assembly.GetTypes() +159
RazorGenerator.Mvc.PrecompiledMvcEngine..ctor(Assembly assembly, String baseVirtualPath) +1209
RazorGenerator.Mvc.PrecompiledMvcEngine..ctor(Assembly assembly) +53
MyProj.Common.App_Start.RazorGeneratorMvcStart.Start() in E:\UserFiles\sheam\Documents\Visual Studio 2010\Projects\MyProj\Project.Common_vs2010\App_Start\RazorGeneratorMvcStart.cs:11

As you can see this is coming from my common project.

Here are the contents of the bin director:

2012-10-17 09:06 PM <DIR> .
2012-10-17 09:06 PM <DIR> ..
2012-10-10 12:51 AM 105,528 Antlr3.Runtime.dll
2012-10-09 07:05 PM 1,118,296 EntityFramework.dll
2012-10-16 01:26 PM 359,424 MyProj.Common_vs2010.dll
2012-10-16 01:26 PM 112,128 MyProj.Common_vs2010.pdb
2012-10-09 06:46 PM 45,416 Microsoft.Web.Infrastructure.dll
2012-10-17 09:06 PM 29,696 MyProj.dll
2012-10-17 09:06 PM 42,496 MyProj.pdb
2012-10-13 08:04 PM 15,872 Mvc.Mailer.dll
2012-10-09 06:46 PM 374,784 Newtonsoft.Json.dll
2012-10-09 06:56 PM 15,872 RazorGenerator.Mvc.dll
2012-10-09 06:46 PM 180,832 System.Net.Http.dll
2012-10-09 06:46 PM 168,544 System.Net.Http.Formatting.dll
2012-10-09 06:46 PM 16,480 System.Net.Http.WebRequest.dll
2012-10-09 06:46 PM 138,328 System.Web.Helpers.dll
2012-10-09 06:46 PM 323,168 System.Web.Http.dll
2012-10-09 06:46 PM 73,312 System.Web.Http.WebHost.dll
2012-10-09 06:46 PM 506,976 System.Web.Mvc.dll
2012-10-10 12:51 AM 54,912 System.Web.Optimization.dll
2012-10-09 06:46 PM 264,792 System.Web.Razor.dll
2012-10-09 06:46 PM 41,048 System.Web.WebPages.Deployment.dll
2012-10-09 06:46 PM 204,376 System.Web.WebPages.dll
2012-10-09 06:46 PM 39,512 System.Web.WebPages.Razor.dll
2012-10-09 06:56 PM 8,704 WebActivator.dll
2012-10-10 12:51 AM 963,640 WebGrease.dll
24 File(s) 5,204,136 bytes
2 Dir(s) 1,519,661,289,472 bytes free

RazorGenerator.Mvc.dll exists in there, so I am not sure why it can't load the type.

The server is a fresh install if W7 Pro.

Wilderness answered 18/10, 2012 at 18:23 Comment(1)
What version of RazorGenerator are you running?Bultman
W
13

Turns out it wasn't actually razor generator that was the problem. The problem was that a class that RG was loading via reflection, was missing the correct assembly. It was the two Webmatrix assmebly's.

Turns out that even if copy local is set (in library project) Visual Studio 2010 and 2012 will not actually copy it over if it exists in the build machine's GAC. In my case build machine had that assembly installed (by MVC3), but web server did not. I ended up adding those assemblies to my web project (not library) and setting them copy local there.

This is a nasty bug to find. I build a special exception handler which points this problem out quickly if it happens again. The Start() function in my RazorGeneratorMvcStart looks like this now:

public static void Start() 
{
    PrecompiledMvcEngine engine = null;
    try
    {
        engine = new PrecompiledMvcEngine(typeof(RazorGeneratorMvcStart).Assembly)
        {
            UsePhysicalViewsIfNewer = HttpContext.Current.Request.IsLocal
        };
    }
    catch (System.Reflection.ReflectionTypeLoadException e)
    {
        StringBuilder exceptions = new StringBuilder("The following DLL load exceptions occurred:");
        foreach (var x in e.LoaderExceptions)
          exceptions.AppendFormat("{0},\n\n", x.Message);
        throw new Exception(string.Format("Error loading Razor Generator Stuff:\n{0}",exceptions));
    }

    ViewEngines.Engines.Insert(0, engine as PrecompiledMvcEngine);

    // StartPage lookups are done by WebPages. 
    VirtualPathFactoryManager.RegisterVirtualPathFactory(engine as PrecompiledMvcEngine);
}

Not nice, but I'll figure out how to make it look nicer, and reusable later. :)

Wilderness answered 20/10, 2012 at 18:34 Comment(3)
Started a discussion regarding incorporating this into the base RazorGen Nuget package.Trainer
Is this still happening with the newer version 2.3.4? I am currently running version 2.2.3Bultman
That was very helpful and saved me quite some time. Thanks!Acedia

© 2022 - 2024 — McMap. All rights reserved.