Loading RazorGeneratorMvcStart.Start's at runtime

Mar 16, 2013 at 3:38 AM
Edited Mar 16, 2013 at 3:39 AM
Hi All,

I'm trying to implement a plugin architecture (VS2012, CLR 4) where MVC controller and pre-compiled views are in stand-alone, class library project assemblies. The idea is to put DLL files in a place where the MVC website can find and load them when the website spins up.

I created a single, test plugin with a controller, model, and single view, which has a RazorGenerator custom build step. If I create a reference from the class lib to the MVC website, all works well - the contained controller/view works fine.

What I cannot figure out is how to load the assembly when the website spins up. I can use Reflection to get to the RazorGeneratorMvcStart Start method in the assembly, and I can invoke it, but there's no HttpContext Request yet, so it fails. I tried invoking it in the website PreApplicationStartMethod event, as well as in the Application_start, but no luck with no Request yet.

If someone can give me some advice on how I can make this plugin architecture work, that would be very appreciated.

Thanks all!
Mar 16, 2013 at 4:42 AM
See this test project for an example of having precompiled views in a standalone library. You don't need to do anything special to get this to start, as it relies on WebActivator to call the start method (in this file).

As an aside, there is no need for HttpContext at the time the Start method is called.
Mar 16, 2013 at 5:18 AM
Edited Mar 16, 2013 at 5:21 AM
Hi David. Thanks for the reply. I had already looked at the sample project you referred to and it doesn't quite fit the problem I'm trying to solve. In the sample, Start() is called because there is a project reference between the MVC website project and the standalone class library project. Without that reference, Start() is not called, hense the problem I am trying to solve.

I want the controller/precompiled views to be standalone plugins. I.e.assemblies loaded at runtime by the website without a reference between the projects. What I am trying to figure out is how to get the same registration of view and controller class in this scenario.

I tried, using Reflection, to invoke Start during application start, and pre app start in the website, but it throws because there's is no httpcontext request object (=null) when it tries to create 'engine'.

There is a project on CodeProject which does what I want with precompiled views, etc., but it only works for VS2010 and with the 'other' Razor class generator VS add-in.

Thanks for any advice.
Mar 16, 2013 at 5:22 AM
There is no need for a reference from the MVC project. WebActivator works on any assembly in the bin folder. Of course, if you don't have a project reference, then the library assembly might not get copied into bin at all, so you'll have to get it in there by some other means, but that's a separate concern.
Mar 16, 2013 at 5:33 AM
Ooooo. I was deploying them to a separate directory then searching them out and manually loading the assemblies via reflection from the website. That was the pattern that the other implementation was using. Bad on me. Sounds like I should be just letting WebActivator do it for me. I'll try it out and see if that resolves it. Thanks for the quick replies!!