I'm working on a plugin system that works with MVC3, DLL's are placed in the ~/Plugin/ directory. So far everything is working fine and dandy as models and controllers are found by the host and the views are properly embedded into the DLL's. The only problem is that views cannot be compiled by the Razor engine.
Models and controllers are added during the initialization phase of the application like this:
[assembly: PreApplicationStartMethod(typeof(Dashboard.PluginReader), "Initialize")]
namespace Dashboard
{
public class PluginReader
{
public static void Initialize()
{
foreach (string plugin in Directory.GetFiles(HostingEnvironment.MapPath("~/Plugin"), "*.dll", SearchOption.AllDirectories))
{
Assembly assembly = Assembly.LoadFile(plugin);
BuildManager.AddReferencedAssembly(assembly);
}
}
}
}
In order to resolve the views I use a VirtualFile and a VirtualPathProvider which return the requested resource as stream like this:
class AssemblyResourceVirtualFile : VirtualFile
{
string path;
public AssemblyResourceVirtualFile(string virtualPath)
: base(virtualPath)
{
path = VirtualPathUtility.ToAppRelative(virtualPath);
}
public override System.IO.Stream Open()
{
// /~Plugin/path.of.dll/path.of.razor.view
string[] parts = path.Split('/');
string assemblyName = parts[2];
string resourceName = parts[3];
string path = HostingEnvironment.MapPath("~/Plugin") + "/"+ assemblyName;
System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(path);
if (assembly != null)
{
Stream resourceStream = assembly.GetManifestResourceStream(resourceName);
return resourceStream;
}
return null;
}
}
As Razor compiles them it returns an exception as it cannot find the references such as ViewBag. Does anyone have an idea on how to make these embedded resources work or know an existing plugin system?