christian louboutin sandalstory burch flatschristian louboutin pumpspuma ferrari designer sunglass louboutin salechristian louboutin on saleprada sunglassespuma future catray ban sunglasseschanel handbags 2011

Pluggable MVC 2.0 using MEF and strongly-typed views

The problem…

I’ve been putting together a pluggable MVC framework using MEF in C# 4.0.

MEF was chosen over other solutions such as Portable Areas as it allows all plugins to be composed together as a single site at runtime without the assemblies needing to reference each other.

After scouring the web for ideas, I stumbled upon Maarten Balliauw’s excellent posts for accomplishing this task:

ASP.NET MVC and the Managed Extensibility Framework (MEF)

Revised: ASP.NET MVC and the Managed Extensibility Framework (MEF)

It seems that there are issues using strongly-typed views in this scenario, though, as the types referenced in the views are not found or loaded at runtime. Maarten’s solution is to copy the plugins into the main website’s bin directory so that they can be discovered at runtime.

The solution…

This can also be solved in another (more elegant?) way by using AssemblyResolve to tell the framework where to load a type from when it is requested by the views.
In this way, your plugin directory needn’t be a sub-directory of the website’s bin folder.

Unfortunately, this doesn’t work without a bit of fettling and the key to getting it working is to give the framework a hint to the assembly the type is in. This in turn forces the AssemblyResolve event to fire. To accomplish this, use an assembly directive at the top of your strongly-typed views as follows:

<%@ Assembly Name="Web.Plugins.Controls" %>
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Web.Plugins.Controls.Models.MenuModel>" %>

An example of using AssemblyResolve by David Morton can be found here:

Assembly Resolution with AppDomain.AssemblyResolve

Which can be bent to our will:

public void Initialize()
	// Add assembly handler for strongly-typed view models
	AppDomain.CurrentDomain.AssemblyResolve += PluginAssemblyResolve;

private Assembly PluginAssemblyResolve(object sender, ResolveEventArgs resolveArgs)
	Assembly[] currentAssemblies = AppDomain.CurrentDomain.GetAssemblies();

	// Check we don't already have the assembly loaded
	foreach (Assembly assembly in currentAssemblies)
		if (assembly.FullName == resolveArgs.Name || assembly.GetName().Name == resolveArgs.Name)
			return assembly;

	// Load from directory
	return LoadAssemblyFromPath(resolveArgs.Name, _fullPluginPath);

private static Assembly LoadAssemblyFromPath(string assemblyName, string directoryPath)
	foreach (string file in Directory.GetFiles(directoryPath))
		Assembly assembly;

		if (TryLoadAssemblyFromFile(file, assemblyName, out assembly))
			return assembly;

	return null;

private static bool TryLoadAssemblyFromFile(string file, string assemblyName, out Assembly assembly)
		// Convert the filename into an absolute file name for
		// use with LoadFile.
		file = new FileInfo(file).FullName;

		if (AssemblyName.GetAssemblyName(file).Name == assemblyName)
			assembly = Assembly.LoadFile(file);
			return true;

	assembly = null;
	return false;

The download…

This is an example framework based on Maarten’s using this method. It requires C# 4.0 and Visual Studio 2010 and it is recommended to run without debugging to avoid slow execution when view caching is disabled (Don’t Panic).

The sample framework includes the following functionality:

  • Embedded resources handling
  • Plugin registration priority for selecting controllers and views
  • Menu and menu item registration with ordering
  • Sample authentication module
  • Automatic site.master resolving

The resource handling is borrowed from the Spark view engine sample modules:

Spark modules

This should serve as a nice starting point for your next MVC pluggable project :)

Written by Rob in: Development | Tags: , , , ,

BunnyBot version 1.0.1 released!

Hot on the heels of my recent announcement for version 1.0.0, version 1.0.1 has been released.

This includes a minor update which automatically adds any new contacts to the nabaztag MSN contact list.


Written by Rob in: Development | Tags: , , ,

BunnyBot version 1.0.0 released!


I have just released version 1.0.0 of my current pet project, BunnyBot.
This C#/.Net project is a windows service or console applicatrion which logs your nabaztag bunny into an MSN account whenever it is awake. Any messages sent to the MSN account is spoken by the bunny.

To download the binaries, jump over to:

Written by Rob in: Development | Tags: , , ,

New blog, content still lacking

I have finally switched to WordPress as I was sick of filtering through all the spam comments I was getting with myBloggie.

I chose WP so I don’t have to spend a lot of time designing the site as there are loads of great existing designs out there. I was halfway to implementing BlogEngine.Net but ran out of time writing a decent theme.

Written by Rob in: Blog | Tags: ,

Nipples n stuff

I dry-assembled the front suspension today to get the car rolling.
Just need to mount the discs and wheels and that end is done.

Had some fun with an angle grinder removing some unwanted bits (included the front tow point) to make it fit correctly.

I also found that the grease nipples on the beam are right in the way of the mounting brackets, but have been assured that the grease will stay in if they are removed :)

I will be designing some sort of bonnet lifting mechanism in the coming weeks so watch this space for some more CAD drawings :D

The work continues…

Written by Rob in: Buggy | Tags: ,

Powered by WordPress | Theme: Aeros 2.0 by | Modifications: theGecko