<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>techBox &#187; Development</title>
	<atom:link href="http://www.thegecko.org/index.php/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thegecko.org</link>
	<description>Buggies, tech and programming...</description>
	<lastBuildDate>Tue, 01 Jun 2010 21:09:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Pluggable MVC 2.0 using MEF and strongly-typed views</title>
		<link>http://www.thegecko.org/index.php/2010/06/pluggable-mvc-2-0-using-mef-and-strongly-typed-views/</link>
		<comments>http://www.thegecko.org/index.php/2010/06/pluggable-mvc-2-0-using-mef-and-strongly-typed-views/#comments</comments>
		<pubDate>Tue, 01 Jun 2010 21:09:23 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[MEF]]></category>
		<category><![CDATA[MVC]]></category>

		<guid isPermaLink="false">http://www.thegecko.org/?p=355</guid>
		<description><![CDATA[The problem&#8230;
I&#8217;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 [...]]]></description>
			<content:encoded><![CDATA[<h2>The problem&#8230;</h2>
<p>I&#8217;ve been putting together a pluggable MVC framework using MEF in C# 4.0.</p>
<p>MEF was chosen over other solutions such as <a title="Portable Areas" href="http://www.lostechies.com/blogs/hex/archive/2009/11/01/asp-net-mvc-portable-areas-via-mvccontrib.aspx">Portable Areas</a> as it allows all plugins to be composed together as a single site at runtime without the assemblies needing to reference each other.</p>
<p>After scouring the web for ideas, I stumbled upon Maarten Balliauw&#8217;s excellent posts for accomplishing this task:</p>
<p><a title="ASP.NET MVC and the Managed Extensibility Framework (MEF)" href="http://blog.maartenballiauw.be/post/2009/04/21/ASPNET-MVC-and-the-Managed-Extensibility-Framework-%28MEF%29.aspx">ASP.NET MVC and the Managed Extensibility Framework (MEF)</a></p>
<p><a title="Revised: ASP.NET MVC and the Managed Extensibility Framework (MEF)" href="http://blog.maartenballiauw.be/post/2009/06/17/Revised-ASPNET-MVC-and-the-Managed-Extensibility-Framework-%28MEF%29.aspx">Revised: ASP.NET MVC and the Managed Extensibility Framework (MEF)</a></p>
<p>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&#8217;s solution is to copy the plugins into the main website&#8217;s bin directory so that they can be discovered at runtime.</p>
<h2>The solution&#8230;</h2>
<p>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.<br />
In this way, your plugin directory needn&#8217;t be a sub-directory of the website&#8217;s bin folder.</p>
<p>Unfortunately, this doesn&#8217;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:</p>
<pre class="brush: csharp;">
&lt;%@ Assembly Name=&quot;Web.Plugins.Controls&quot; %&gt;
&lt;%@ Control Language=&quot;C#&quot; Inherits=&quot;System.Web.Mvc.ViewUserControl&lt;Web.Plugins.Controls.Models.MenuModel&gt;&quot; %&gt;
</pre>
<p>An example of using AssemblyResolve by David Morton can be found here:</p>
<p><a title="Assembly Resolution with AppDomain.AssemblyResolve" href="http://blog.codinglight.com/2009/07/assembly-resolution-with.html">Assembly Resolution with AppDomain.AssemblyResolve</a></p>
<p>Which can be bent to our will:</p>
<pre class="brush: csharp;">
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)
{
	try
	{
		// 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;
		}
	}
	catch
	{
	}

	assembly = null;
	return false;
}
</pre>
<h2>The download&#8230;</h2>
<p><a title="Download example" href="http://www.thegecko.org/uploads/MefMvc.zip">http://www.thegecko.org/uploads/MefMvc.zip</a></p>
<p>This is an example framework based on Maarten&#8217;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 (<a title="Don't Panic!" href="http://codeclimber.net.nz/archive/2009/04/22/how-to-improve-htmlhelper.renderpartial-performances-donrsquot-run-in-debug-mode.aspx">Don&#8217;t Panic</a>).</p>
<p>The sample framework includes the following functionality:</p>
<ul>
<li> Embedded resources handling</li>
<li>Plugin registration priority for selecting controllers and views</li>
<li>Menu and menu item registration with ordering</li>
<li>Sample authentication module</li>
<li>Automatic site.master resolving</li>
</ul>
<p>The resource handling is borrowed from the Spark view engine sample modules:</p>
<p><a title="Spark modules" href="http://github.com/loudej/spark/tree/cb60a413bf72dfd0bc7cf7bd6b6a111a8ea2ab63/src/Samples/Modules/Spark.Modules">Spark modules</a></p>
<p>This should serve as a nice starting point for your next MVC pluggable project <img src='http://www.thegecko.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegecko.org/index.php/2010/06/pluggable-mvc-2-0-using-mef-and-strongly-typed-views/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>BunnyBot version 1.0.1 released!</title>
		<link>http://www.thegecko.org/index.php/2010/01/bunnybot-version-1-0-1-released/</link>
		<comments>http://www.thegecko.org/index.php/2010/01/bunnybot-version-1-0-1-released/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 10:24:01 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[bunny]]></category>
		<category><![CDATA[msn]]></category>
		<category><![CDATA[nabaztag]]></category>

		<guid isPermaLink="false">http://www.thegecko.org/?p=350</guid>
		<description><![CDATA[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.
Enjoy!
http://code.google.com/p/bunnybot/
]]></description>
			<content:encoded><![CDATA[<p>Hot on the heels of my recent <a href="http://www.thegecko.org/index.php/2010/01/bunnybot-version-1-0-0-released/">announcement for version 1.0.0</a>, version 1.0.1 has been released.</p>
<p>This includes a minor update which automatically adds any new contacts to the nabaztag MSN contact list.</p>
<p>Enjoy!</p>
<p><a href="http://code.google.com/p/bunnybot/">http://code.google.com/p/bunnybot/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegecko.org/index.php/2010/01/bunnybot-version-1-0-1-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>BunnyBot version 1.0.0 released!</title>
		<link>http://www.thegecko.org/index.php/2010/01/bunnybot-version-1-0-0-released/</link>
		<comments>http://www.thegecko.org/index.php/2010/01/bunnybot-version-1-0-0-released/#comments</comments>
		<pubDate>Sun, 03 Jan 2010 19:15:42 +0000</pubDate>
		<dc:creator>Rob</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[bunny]]></category>
		<category><![CDATA[msn]]></category>
		<category><![CDATA[nabaztag]]></category>

		<guid isPermaLink="false">http://www.thegecko.org/?p=346</guid>
		<description><![CDATA[
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:
http://code.google.com/p/bunnybot/
]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-thumbnail wp-image-347" title="nabaztag" src="http://www.thegecko.org/wp-content/uploads/2010/01/nabaztag-150x150.jpg" alt="nabaztag" width="150" height="150" /></p>
<p>I have just released version 1.0.0 of my current pet project, BunnyBot.<br />
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.</p>
<p>To download the binaries, jump over to:</p>
<p><a href="http://code.google.com/p/bunnybot/">http://code.google.com/p/bunnybot/</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.thegecko.org/index.php/2010/01/bunnybot-version-1-0-0-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
