<div style="position: absolute; top: -976px;left: -976px;"><a href="http://www.garishchristianlouboutin.com/christian-louboutin-sandals-c-2.html">christian louboutin sandals</a><a href="http://www.mywholeshop.com/tory-burch-shoes-tory-burch-flats-c-1_29.html">tory burch flats</a><a href="http://www.garishchristianlouboutin.com/christian-louboutin-pumps-c-5.html">christian louboutin pumps</a><a href="http://www.sopuma.com/puma-womens-2010-ferrari-shoes-c-34.html">puma ferrari</a> <a href="http://www.glassesgroup.com/">designer sunglass</a> 
<a href="http://www.garishchristianlouboutin.com/">louboutin sale</a><a href="http://www.garishchristianlouboutin.com/products_all.html">christian louboutin on sale</a><a href="http://www.glassesgroup.com/prada-sunglasses-c-8.html">prada sunglasses</a><a href="http://www.sopuma.com/specials.html">puma future cat</a><a href="http://www.glassesgroup.com/ray-ban-sunglasses-c-1.html">ray ban sunglasses</a><a href="http://www.justforbag.com/chanel-handbags-c-174.html">chanel handbags 2011 </a></div>

<?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; ASP.Net</title>
	<atom:link href="http://www.thegecko.org/index.php/tag/asp-net/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; title: ; notranslate">
&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; title: ; notranslate">
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>12</slash:comments>
		</item>
	</channel>
</rss>

