@Shazwazza

Shannon Deminick's blog all about web development

Using metadata with MEF in Medium Trust

March 3, 2011 19:20

One of the awesome parts of MEF is that you can attach metadata to your objects. This allows you to use the new Lazy<T, TMetadata> object so you can inspect the meta data about an object without instantiating it. In order to attach metadata to an object, you need (should have) some entity that contains properties to support the meta data such as an interface (good tutorial site here ). For example, if you have a class of type MyObjectType and an interface to store metadata about the class of type IMyMetadata, you can then resolve/import Lazy<MyObjectType, IMyMetadata> which will expose a Metadata property of type IMyMetadata with the information filled in. You might already be able to tell that a bit of magic is happening behind the scenes since there is no concrete class for IMyMetadata but MEF has magically found a way to instantiate a class of type IMyMetadata and fill in the values. This is the problem area if you are running in Medium Trust, you will get a SecurityException stemming form the System.ComponentModel.Composition.MetadataViewGenerator. This same issue will occur when using Autofac to register metadata with the .WithMetadata extension method.

Solution

To work around this problem in Medium Trust, you’ll need to create a concrete class to store your metadata in instead of just referencing an interface. You’ll also need to ensure that the constructor of this class takes in one parameter of type IDictionary<string, object> and using this object you’ll need to manually do the heavy lifting of assigning the values to the properties of this class based on the values in the dictionary. Better yet, you can just make a base class that all your metadata classes can inherit from. In Umbraco v5, we’ve called this abstract class MetadataComposition just to keep inline with the naming conventions of the MEF namespace:

public abstract class MetadataComposition { /// <summary> /// constructor, sets all properties of this object based /// on the dictionary values /// </summary> /// <param name="obj"></param> protected MetadataComposition(IDictionary<string, object> obj) { var t = GetType(); var props = t.GetProperties(); foreach (var a in obj) { var p = props.Where(x => x.Name == a.Key).SingleOrDefault(); if (p != null) { p.SetValue(this, a.Value, null); } } } }

Hope this helps someone!

Developing a plugin framework in ASP.NET MVC with medium trust

January 7, 2011 10:06

I’ve recently spent quite a lot of time researching and prototyping different ways to create a plugin engine in ASP.NET MVC3 and primarily finding a nice way to load plugins (DLLs) in from outside of the ‘bin’ folder. Although this post focuses on MVC3, I am sure that the same principles will apply for other MVC versions.

The Issues

Loading DLLs from outside of the ‘bin’ folder isn’t really anything new or cutting edge, however when working with MVC this becomes more difficult. This is primarily due to how MVC loads/finds types that it needs to process including controllers, view models (more precisely the generic argument passed to a ViewPage or used with the @model declaration in Razor), model binders, etc… MVC is very tied to the BuildManager which is the mechanism for compiling views, and locating other services such as controllers. By default the BuildManager is only familiar with assembies in the ‘bin’ folder and in the GAC, so if you start putting DLLs in folders outside of the ‘bin’ then it won’t be able to locate the MVC services and objects that you might want it to be referencing.

Another issue that needs to be dealt with is DLL file locking. When a plugin DLL is loaded and is in use the CLR will lock the file. This becomes an an issue if developers want to update the plugin DLL while the website is running since they won’t be able to unless they bump the web.config or take the site down. This holds true for MEF and how it loads DLLs as well.

.Net 4 to the rescue… almost

One of the new features in .Net 4 is the ability to execute code before the app initializes which compliments another new feature of the BuildManager that lets you add assembly references to it at runtime (which must be done on application pre-init). Here’s a nice little reference to these new features from Phil Haack: http://haacked.com/archive/2010/05/16/three-hidden-extensibility-gems-in-asp-net-4.aspx.  This is essential to making a plugin framework work with MVC so that the BuildManager knows where to reference your plugin DLLs outside of the ‘bin’. However, this isn’t the end of the story.

Strongly typed Views with model Types located in plugin DLLs

Unfortunately if you have a view that is strongly typed to a model that exists outside of the ‘bin’, then you’ll find out very quickly that it doesn’t work and it won’t actually tell you why. This is because the RazorViewEngine  uses the BuildManager to compile the view into a dynamic assembly but then uses Activator.CreateInstance to instantiate the newly compiled object. This is where the problem lies, the current AppDomain doesn’t know how to resolve the model Type for the strongly typed view since it doesn’t exist in the ‘bin’ or GAC.  An even worse part about this scenario is that you don’t get any error message telling you why this isn’t working, or where the problem is. Instead you get the nice MVC view not found error: “…or its master was not found or no view engine supports the searched locations. The following locations were searched: ….” telling you that it has searched for views in all of the ViewEngine locations and couldn’t find it… which is actually not the error at all.  Deep in the MVC3 source, it tries to instantiate the view object from the dynamic assembly and it fails so it just keeps looking for that view in the rest of the ViewEngine paths.

NOTE: Even though in MVC3 there’s a new IViewPageActivator which should be responsible for instantiating the views that have been compiled with the BuildManager, implementing a custom IViewPageActivator to handle this still does not work because somewhere in the MVC3 codebase fails before the call to the IViewPageActivator which has to do with resolving an Assembly that is not in the ‘bin’.

Full trust

When working in Full Trust we have a few options for dealing with the above scenario:

  • Use the AppDomain’s ResolveAssembly event
    • By subscribing to this event, you are able to instruct the AppDomain where to look when it can’t find a reference to a Type.
    • This is easily done by checking if your plugin assemblies match the assembly being searched for, and then returning the Assembly object if found:
static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) { var pluginsFolder = new DirectoryInfo(HostingEnvironment.MapPath("~/Plugins")); return (from f in pluginsFolder.GetFiles("*.dll", SearchOption.AllDirectories) let assemblyName = AssemblyName.GetAssemblyName(f.FullName) where assemblyName.FullName == args.Name || assemblyName.FullName.Split(',')[0] == args.Name select Assembly.LoadFile(f.FullName)).FirstOrDefault(); }
  • Shadow copy your plugin DLLs into the AppDomain’s DynamicDirectory.
    • This is the directory that the BuildManager compiles it’s dynamic assemblies into and is also a directory that the AppDomain looks to when resolving Type’s from Assemblies.
    • You can shadow copy your plugin DLLs to this folder on app pre-init and everything ‘should just work’
  • Replace the RazorViewEngine with a custom razor view engine that compiles views manually but makes references to the appropriate plugin DLLs
    • I actually had this working in an Umbraco v5 prototype but it is hugely overkill and unnecessary plus you actually would have to replace the RazorViewEngine which is pretty absurd.

The burden of Medium Trust

In the MVC world there’s only a couple hurdles to jump when loading in plugins from outside of the ‘bin’ folder in Full Trust. In Medium Trust however, things get interesting. Unfortunately in Medium Trust it is not possible to handle the AssemblyResolve event and it’s also not possible to access the DynamicDirectory of the AppDomain so the above two solutions get thrown out the window. Further to this it seems as though you can’t use CodeDom in Medium Trust to custom compile views.

Previous attempts

For a while I began to think that this wasn’t possible and I thought I tried everything:

  • Shadow copying DLLs from the plugins folder into the ‘bin’ folder on application pre-init
    • This fails because even during app pre-init, the application pool will still recycle. Well, it doesn’t actually ‘fail’ unless you keep re-copying the DLL into the bin. If you check if it already exists and don’t copy into the bin than this solution will work for you but it’s hardly a ‘solution’ since you might as well just put all your DLLs into the ‘bin’ in the first place.
  • Trying to use sub folders of the ‘bin’ folder to load plugins.
    • Turns out that ASP.Net doesn’t by default load in DLLs that exist in sub folders of the bin, though from research it looks like standard .Net apps actually do.
    • Another interesting point was that if you try to copy a DLL into a sub folder of the bin during application pre-init you get a funky error:  “Storage scopes cannot be created when _AppStart is executing”. It seems that ASP.Net is monitoring all changes in the bin folder regardless of whether or not they are in sub folders but still doesn’t load or reference those assemblies.

An easy solution

So, the easy solution is to just set a ‘privatePath’ on the ‘probing’ element in your web.config to tell the AppDomain to also look for Assemblies/Types in the specified folders. I did try this before when trying to load plugins from sub folders in the bin and couldn’t get it to work. I’m not sure if I was ‘doing it wrong’ but it definitely wasn’t working then, either that or attempting to set this in sub folders of the bin just doesn’t work.

<runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <probing privatePath="Plugins/temp" />

DLL file locking

Since plugin DLLs get locked by the CLR when they are loaded, we need to work around this. The solution is to shadow copy the DLLs to another folder on application pre-init. As mentioned previously, this is one of the ways to get plugins loaded in Full Trust and in my opinion is the nicest way to do it since it kills 2 birds with one stone. In Medium Trust however, we’ll have to jump through some hoops and shadow copy the DLLs to a temp folder that exists within the web application. IMPORTANT: When you’re copying DLLs you might be tempted to modify the name of the DLL by adding a version number or similar, but this will NOT work and you’ll get a “The located assembly's manifest definition … does not match the assembly reference.” exception.

Solution

UPDATE: The latest version of this code can be found in the Umbraco v5 source code. The following code does work but there’s been a lot of enhancements to it in the Umbraco core. Here’s the latest changeset as of 16/16/2012 Umbraco v5 PluginManager.cs

Working in Full Trust, the simplest solution is to shadow copy your plugin DLLs into your AppDomain DynamicDirectory. Working in Medium Trust you’ll need to do the following:

  • On application pre-init:
    • Shadow copy all of your plugin DLLs to a temporary folder in your web application (not in the ‘bin’)
    • Add all of the copied DLLs to be referenced by the BuildManager
  • Add all folder paths to the privatePath attribute of the probing element in your web.config to point to where you will be copying your DLLs
    • If you have more than one, you need to semi-colon separate them

Thanks to Glenn Block @ Microsoft who gave me a few suggestions regarding DLL file locking with MEF, Assembly load contexts and probing paths! You put me back on track after I had pretty much given up.

Here’s the code to do the shadow copying and providing the Assemblies to the BuildManager on application pre-init (make sure you set the privatePath on the probing element in your web.config first!!)

using System.Linq; using System.Web; using System.IO; using System.Web.Hosting; using System.Web.Compilation; using System.Reflection; [assembly: PreApplicationStartMethod(typeof(PluginFramework.Plugins.PreApplicationInit), "Initialize")] namespace PluginFramework.Plugins { public class PreApplicationInit { static PreApplicationInit() { PluginFolder = new DirectoryInfo(HostingEnvironment.MapPath("~/plugins")); ShadowCopyFolder = new DirectoryInfo(HostingEnvironment.MapPath("~/plugins/temp")); } /// <summary> /// The source plugin folder from which to shadow copy from /// </summary> /// <remarks> /// This folder can contain sub folderst to organize plugin types /// </remarks> private static readonly DirectoryInfo PluginFolder; /// <summary> /// The folder to shadow copy the plugin DLLs to use for running the app /// </summary> private static readonly DirectoryInfo ShadowCopyFolder; public static void Initialize() { Directory.CreateDirectory(ShadowCopyFolder.FullName); //clear out plugins) foreach (var f in ShadowCopyFolder.GetFiles("*.dll", SearchOption.AllDirectories)) { f.Delete(); } //shadow copy files foreach (var plug in PluginFolder.GetFiles("*.dll", SearchOption.AllDirectories)) { var di = Directory.CreateDirectory(Path.Combine(ShadowCopyFolder.FullName, plug.Directory.Name)); // NOTE: You cannot rename the plugin DLL to a different name, it will fail because the assembly name is part if it's manifest // (a reference to how assemblies are loaded: http://msdn.microsoft.com/en-us/library/yx7xezcf ) File.Copy(plug.FullName, Path.Combine(di.FullName, plug.Name), true); } // Now, we need to tell the BuildManager that our plugin DLLs exists and to reference them. // There are different Assembly Load Contexts that we need to take into account which // are defined in this article here: // http://blogs.msdn.com/b/suzcook/archive/2003/05/29/57143.aspx // * This will put the plugin assemblies in the 'Load' context // This works but requires a 'probing' folder be defined in the web.config foreach (var a in ShadowCopyFolder .GetFiles("*.dll", SearchOption.AllDirectories) .Select(x => AssemblyName.GetAssemblyName(x.FullName)) .Select(x => Assembly.Load(x.FullName))) { BuildManager.AddReferencedAssembly(a); } // * This will put the plugin assemblies in the 'LoadFrom' context // This works but requires a 'probing' folder be defined in the web.config // This is the slowest and most error prone version of the Load contexts. //foreach (var a in // ShadowCopyFolder // .GetFiles("*.dll", SearchOption.AllDirectories) // .Select(plug => Assembly.LoadFrom(plug.FullName))) //{ // BuildManager.AddReferencedAssembly(a); //} // * This will put the plugin assemblies in the 'Neither' context ( i think ) // This nearly works but fails during view compilation. // This DOES work for resolving controllers but during view compilation which is done with the RazorViewEngine, // the CodeDom building doesn't reference the plugin assemblies directly. //foreach (var a in // ShadowCopyFolder // .GetFiles("*.dll", SearchOption.AllDirectories) // .Select(plug => Assembly.Load(File.ReadAllBytes(plug.FullName)))) //{ // BuildManager.AddReferencedAssembly(a); //} } } }

MVC Controllers as plugins with MEF and Autofac

December 1, 2010 01:38

Disclaimer

This blog posts talks about Umbraco v5 (Jupiter), however since the state of the codebase is in it’s infancy, the details about Umbraco v5 in this article may not be accurate once Umbraco v5 is released.

Plugins

In Umbraco v5 we’re introducing the concept of plugins. Currently in Umbraco, we have the notion of plugins such as Trees, Actions (used on context menus), Data types, etc… Each of these objects is found using a class called TypeFinder to search for specific types in all assemblies that are loaded in the ‘bin’ folder. Though this works, it is definitely not the most efficient approach to plugins. For Umbraco v5, we’re now using MEF as the plugin framework which is now part of .Net framework 4.  There will be many coming blog posts about the different types of plugins, how they work and how to create them using MEF, but this blog post is about overcoming a small hurdle we encountered when trying to integrate MEF, Autofac and MVC controllers as 3rd party plugins….

MVC Controllers as plugins

For Umbraco v5, we’re using Autofac as the IoC container which works really well for injecting dependencies into MVC controllers (amongst a ton of other things). Autofac also plays really nicely with MEF through it’s MEF integration library and requires all of 2 lines of code to get the MEF plugins into our IoC container:

var catalog = new DirectoryCatalog( m_HttpServer.MapPath("~/Plugins/Trees")); builder.RegisterComposablePartCatalog(catalog);

The above code tells Autofac to register all exportable MEF components that are found in all assemblies in the ~/Plugins/Trees folder. In our case, the plugins in this folder are actually MVC Controllers which will get registered in to the IoC container and since we are currently using the AutofacControllerFactory for our MVC controller factory, it ‘should’ be able to create the requested controller and inject it’s dependencies because we’ve stuck it in the container.

Problem 1

So far this is all very easy, but as soon as we want to route a request to this controller we end up with a 404 error stating that the page/controller cannot be found.  The reason for this is because the AutofacControllerFactory inherits from the DefaultControllerFactory which searches for controllers using the BuildManager which only searches for types registered in the ‘bin’ folder. I was hoping that the AutofacControllerFactory would have also searched within it’s registrations but this turns out to not be the case. (In fact, once Autofac releases it’s MVC3 implementation, it will no longer be shipped with an AutofacControllerFactory and instead be using MVC3’s new DependencyResolver).

Solution 1

In order for this to work we have to create our own ControllerFactory  (source code at the end of this article) and provide our own implementation for the methods:

IController GetControllerInstance(RequestContext context, Type controllerType) Type GetControllerType(RequestContext requestContext, string controllerName)

The GetControllerType implementation does the following:

  • Checks if the requested controllerName has already been resolved and exists in our factory’s internal cache
  • If not, try to find the controller by name from the underlying DefaultControllerFactory
  • If not found, search the IoC container’s registrations for the type,
    • if found, store the reference Type in the factory’s internal cache

The GetControllerInstance does the following:

  • Check if the controllerType has been registered in the factory’s internal cache
    • If so, get the return the resolved IController instance from the container
  • If not, try to create the controller from the underlying DefaultControllerFactory

Problem 2

The next problem we encountered was that Autofac couldn’t inject dependent objects into the controllers that weren’t registered in the container using the Autofac MEF extensions. A quick post on to the Autofac Google Group and I had my answer (read the post if you want examples)!

Solution 2

Essentially, if you want to expose objects to MEF in Autofac, you have to explicitly register them in the container with the ‘Exported’ extension method. Example:

//register the umbraco settings builder.Register<UmbracoSettings>(x => UmbracoSettings.GetSettings()) .As<IUmbracoSettings>() //ensure it's registered for MEF too .Exported(x => x.As<IUmbracoSettings>()) //only have one instance ever .SingleInstance();

Problem 3

The last problem was that by default it seems that exported MEF components are instantiated as singletons, meaning that each time you try to resolve a MEF component of a certain type it will always be the exact same instance. MVC really doesn’t like this when it comes to controllers, in fact MVC gives you a very informative error message regarding this and explains that if you are using dependency injection to create controllers to make sure the container is configured to resolve new controller objects, not the same instance.

Solution 3

All we need to do is add a PartCreationPolicyAttribute to the exported MEF type and set it to CreationPolicy.NonShared which tells the framework to create a new instance each time it is resolved. Example:

[Tree(typeof(ContentTreeController))] [PartCreationPolicy(CreationPolicy.NonShared)] public class ContentTreeController : DemoDataTreeController

UmbracoControllerFactory source

Here’s the source code for the custom controller factory, it currently inherits from AutofacControllerFactory but once we upgrade to use the new MVC 3 Autofac implementation, this will simply inherit from DefaultControllerFactory.

using System; using System.Web; using System.Web.Mvc; using System.Linq; using System.Web.Routing; using Autofac; using Autofac.Core; using Autofac.Integration.Web; using Autofac.Integration.Web.Mvc; using System.Collections.Generic; using Autofac.Features.Metadata; using UmbracoProjects.CMS.Web.Trees.Controllers; using System.Text.RegularExpressions; namespace UmbracoProjects.CMS.Web.Mvc.Controllers { public class UmbracoControllerFactory : AutofacControllerFactory { protected IContainer m_Container; private static readonly object m_Locker = new object(); /// <summary> /// Used to cache found controllers in the IoC container /// </summary> private static readonly Dictionary<string, ServiceTypeCache> m_IoCControllerCache = new Dictionary<string, ServiceTypeCache>(); private class ServiceTypeCache { public Service Service { get; set; } public Type ComponentType { get; set; } } /// <summary> /// Initializes a new instance of the <see cref="AutofacControllerFactory"/> class. /// </summary> /// <param name="containerProvider">The container provider.</param> public UmbracoControllerFactory(IContainerProvider containerProvider) : base(containerProvider) { m_Container = containerProvider.ApplicationContainer; } /// <summary> /// Creates the controller based on controller type /// </summary> /// <param name="context">The context.</param> /// <param name="controllerType">Type of the controller.</param> /// <returns>The controller.</returns> /// <remarks> /// This first checks our IoC service internal cache to see if it exists, if it does, we'll try to resolve the controller /// from IoC, otherwise we'll try to resolve the controller from the underlying factory /// </remarks> protected override IController GetControllerInstance(RequestContext context, Type controllerType) { if (context == null) throw new ArgumentNullException("context"); //first, check if this service by this type is in our cache if (m_IoCControllerCache.Where(x => x.Value.ComponentType.Equals(controllerType)).Any()) { var controllerService = m_IoCControllerCache.Where(x => x.Value.ComponentType.Equals(controllerType)).SingleOrDefault().Value.Service; object controller; if (m_Container.TryResolveService(controllerService, out controller)) { //if the controller is created by MEF, then resolve if (controller is System.ComponentModel.Composition.Primitives.Export) { return (IController)((System.ComponentModel.Composition.Primitives.Export)controller).Value; } else if (controller is IController) { return (IController)controller; } } throw new HttpException(404, string.Format("Controller type " + controllerType + " could not be resolved", controllerService, controllerType.FullName, context.HttpContext.Request.Path)); } //otherwise, try to create from the underlying factory return base.GetControllerInstance(context, controllerType); } /// <summary> /// Finds a controller type based on it's name. /// </summary> /// <param name="requestContext"></param> /// <param name="controllerName"></param> /// <returns></returns> /// <remarks> /// This searches using the DefaultControllerFactory's implementation and /// also searches the IoC container for registered types since types that are registered in the container /// may exist outside the bin folder /// </remarks> protected override Type GetControllerType(RequestContext requestContext, string controllerName) { var controllerKeyName = controllerName.ToUpper(); //first, check if this is already in our internal cache if (m_IoCControllerCache.ContainsKey(controllerKeyName)) { return m_IoCControllerCache[controllerKeyName].ComponentType; } //next, try to resolve it from the underlying factory var type = base.GetControllerType(requestContext, controllerName); //if the controller can't be resolved by name using the standard method, then we should check our container if (type == null) { //we need to search the registered components in IoC for a match foreach(var reg in m_Container.ComponentRegistry.Registrations) { foreach (var s in reg.Services) { //match namespaces/classe that then end with "controllerName + Controller" if (Regex.IsMatch(s.Description, @"(?:\w|\.)+\." + controllerName + "Controller$", RegexOptions.Compiled | RegexOptions.IgnoreCase)) { var controller = m_Container.ResolveService(s); var cType = controller.GetType(); //add to the internal cache if (!m_IoCControllerCache.ContainsKey(controllerKeyName)) { lock (m_Locker) { //double check if (!m_IoCControllerCache.ContainsKey(controllerKeyName)) { m_IoCControllerCache.Add(controllerKeyName, new ServiceTypeCache() { Service = s, ComponentType = cType }); } } } return cType; } } } } return type; } } }