Archive for the ‘MEF’ Category

MyGet now supports pushing from the command line

One of the work items we had opened for MyGet was the ability to push packages to a private feed from the command line. Only a few hours after our initial launch, David Fowler provided us with example code on how to implement NuGet command line pushes …

Creating your own private NuGet feed: MyGet

Ever since NuGet came out, I’ve been thinking about leveraging it in a corporate environment. I’ve seen two NuGet server implementations appear on the Internet: the official NuGet gallery server and Phil Haack’s NuGet.Server package. A…

Microsoft .NET Framework 4 Platform Update 1 KB2478063 Service Pack 5 Feature Set 3.1 R2 November Edition RTW

As you can see, a new .NET Framework version just came out. Read about it at http://blogs.msdn.com/b/endpoint/archive/2011/04/18/microsoft-net-framework-4-platform-update-1.aspx. Now why does my title not match with the title from the blog post I refer…

Official Belgium TechDays 2011 Windows Phone 7 app released

I’m proud to announce that we (RealDolmen) have released the official Belgium TechDays 2011 Windows Phone 7 app! The official Belgium TechDays 2011 gives you the ability to browse current & upcoming sessions, as well as provide LIVE feedback to t…

Thank you for getting me in Vegas!

I wish to thank everyone who has been voring for getting me in Vegas, speaking at MIX11. Without having expectations, I was really really surprised (and happy!) my session got selected. Thanks a bunch! Oh and thanks, RealDolmen, for supporting me in…

ASP.NET MVC and the Managed Extensibility Framewok on NuGet

If you search on my blog, there’s a bunch of posts where I talk about ASP.NET MVC and MEF. And what’s cool: these posts are the ones that are actually being read quite often. I’m not sure about which bloggers actually update their pos…

Viva, Las Vegas!

I have asked it last year, and I’ll ask it again. One of my session proposals made it to the “short”list for MIX11. One thing left though: votes are the only currency to get my session proposal in Vegas. Here’s the session abstract: Fun wi…

ASP.NET MVC 3 and MEF sitting in a tree…

As I stated in a previous blog post: ASP.NET MVC 3 preview 1 has been released! I talked about some of the new features and promised to do a blog post in the dependency injection part. In this post, I’ll show you how to use that together with MEF.

Download my sample code: Mvc3WithMEF.zip (256.21 kb)

kick it on DotNetKicks.com

Dependency injection in ASP.NET MVC 3

First of all, there’s 4 new hooks for injecting dependencies:

  • When creating controller factories
  • When creating controllers
  • When creating views (might be interesting!)
  • When using action filters

In ASP.NET MVC 2, only one of these hooks was used for dependency injection: a controller factory was implemented, using a dependency injection framework under the covers. I did this once, creating a controller factory that wired up MEF and made sure everything in the application was composed through a MEF container. That is, everything that is a controller or part thereof. No easy options for DI-ing things like action filters or views…

ASP.NET MVC 3 shuffled the cards a bit. ASP.NET MVC 3 now contains and uses the Common Service Locator’s IServiceLocator interface, which is used for resolving services required by the ASP.NET MVC framework. The IServiceLocator implementation should be registered in Global.asax using just one line of code:

MvcServiceLocator.SetCurrent(new SomeServiceLocator());

This is, since ASP.NET MVC 3 preview 1, the only thing required to make DI work. In controllers, in action filters and in views. Cool, eh?

Leveraging MEF with ASP.NET MVC 3

First of all: a disclaimer. I already did posts on MEF and ASP.NET MVC before, and in all these posts, I required you to explicitly export your controller types for composition. In this example, again, I will require that, just for keeping code a bit easier to understand. Do note that are some variants of a convention based registration model available.

As stated before, the only thing to build here is a MefServiceLocator that is suited for web (which means: an application-wide catalog and a per-request container). I’ll still have to create my own controller factory as well, because otherwise I would not be able to dynamically compose my controllers. Here goes…

Implementing ServiceLocatorControllerFactory

Starting in reverse, but this thing is the simple part :-)

[Export(typeof(IControllerFactory))][PartCreationPolicy(CreationPolicy.Shared)]public class ServiceLocatorControllerFactory    : DefaultControllerFactory{    private IMvcServiceLocator serviceLocator;

    [ImportingConstructor]    public ServiceLocatorControllerFactory(IMvcServiceLocator serviceLocator)    {        this.serviceLocator = serviceLocator;    }

    public override IController CreateController(RequestContext requestContext, string controllerName)    {        var controllerType = GetControllerType(requestContext, controllerName);        if (controllerType != null)        {            return this.serviceLocator.GetInstance(controllerType) as IController;        }

        return base.CreateController(requestContext, controllerName);    }

    public override void ReleaseController(IController controller)    {        this.serviceLocator.Release(controller);    }}

Did you see that? A simple, MEF enabled controller factory that uses an IMvcServiceLocator. This thing can be used with other service locators as well.

Implementing MefServiceLocator

Like I said, this is the most important part, allowing us to use MEF for resolving almost any component in the ASP.NET MVC pipeline. Here’s my take on that:

[Export(typeof(IMvcServiceLocator))][PartCreationPolicy(CreationPolicy.Shared)]public class MefServiceLocator    : IMvcServiceLocator{    const string HttpContextKey = "__MefServiceLocator_Container";

    private ComposablePartCatalog catalog;    private IMvcServiceLocator defaultLocator;

    [ImportingConstructor]    public MefServiceLocator()    {        // Get the catalog from the MvcServiceLocator.        // This is a bit dirty, but currently        // the only way to ensure one application-wide catalog        // and a per-request container.

        MefServiceLocator mefServiceLocator = MvcServiceLocator.Current as MefServiceLocator;        if (mefServiceLocator != null)        {            this.catalog = mefServiceLocator.catalog;        }

        // And the fallback locator...

        this.defaultLocator = MvcServiceLocator.Default;    }

    public MefServiceLocator(ComposablePartCatalog catalog)        : this(catalog, MvcServiceLocator.Default)    {    }

    public MefServiceLocator(ComposablePartCatalog catalog, IMvcServiceLocator defaultLocator)    {        this.catalog = catalog;        this.defaultLocator = defaultLocator;    }

    protected CompositionContainer Container    {        get        {            if (!HttpContext.Current.Items.Contains(HttpContextKey))            {                HttpContext.Current.Items.Add(HttpContextKey, new CompositionContainer(catalog));            }

            return (CompositionContainer)HttpContext.Current.Items[HttpContextKey];        }    }

    private object Resolve(Type serviceType, string key = null)    {        var exports = this.Container.GetExports(serviceType, null, null);        if (exports.Any())        {            return exports.First().Value;        }

        var instance = defaultLocator.GetInstance(serviceType, key);        if (instance != null)        {            return instance;        }

        throw new ActivationException(string.Format("Could not resolve service type {0}.", serviceType.FullName));    }

    private IEnumerable<object> ResolveAll(Type serviceType)    {        var exports = this.Container.GetExports(serviceType, null, null);        if (exports.Any())        {            return exports.Select(e => e.Value).AsEnumerable();        }

        var instances = defaultLocator.GetAllInstances(serviceType);        if (instances != null)        {            return instances;        }

        throw new ActivationException(string.Format("Could not resolve service type {0}.", serviceType.FullName));    }

    #region IMvcServiceLocator Members

    public void Release(object instance)    {        var export = instance as Lazy<object>;        if (export != null)        {            this.Container.ReleaseExport(export);        }

        defaultLocator.Release(export);    }

    #endregion

    #region IServiceLocator Members

    public IEnumerable<object> GetAllInstances(Type serviceType)    {        return ResolveAll(serviceType);    }

    public IEnumerable<TService> GetAllInstances<TService>()    {        var instances = ResolveAll(typeof(TService));        foreach (TService instance in instances)        {            yield return (TService)instance;        }    }

    public TService GetInstance<TService>(string key)    {        return (TService)Resolve(typeof(TService), key);    }

    public object GetInstance(Type serviceType)    {        return Resolve(serviceType);    }

    public object GetInstance(Type serviceType, string key)    {        return Resolve(serviceType, key);    }

    public TService GetInstance<TService>()    {        return (TService)Resolve(typeof(TService));    }

    #endregion

    #region IServiceProvider Members

    public object GetService(Type serviceType)    {        return Resolve(serviceType);    }

    #endregion}

HOLY SCHMOLEY! That is a lot of code. Let’s break it down…

First of all, I have 3 constructors. 2 for convenience, one for MEF. Since the MefServiceLocator will be instantiated in Global.asax and I only want one instance of it to live in the application, I have to do a dirty trick: whenever MEF wants to create a new MefServiceLocator for some reason (should in theory only happen once per request, but I want this thing to live application-wide), I’m giving it indeed a new instance which at least shares the part catalog with the one I originally created. Don’t shoot me for doing this…

Next, you will also notice that I’m using a “fallback” locator, which in theory will be the instance stored in MvcServiceLocator.Default, which is ASP.NET MVC 3’s default MvcServiceLocator. I’m doing this for a reason though… read my disclaimer again: I stated that everything should be decorated with the [Export] attribute when I’m relying on MEF. Now since the services exposed by ASP.NET MVC 3, like the IFilterProvider, are not decorated with this attribute, MEF will not be able to find those. When I find myself in that situation, the MefServiceLocator is simply asking the default service locator for it. Not a beauty, but it works and makes my life easy.

Wiring things

To wire this thing, all it takes is adding 3 lines of code to my Global.asax. For clarity, I’m giving you my entire Global.asax Application_Start method:

protected void Application_Start()
{
    // Register areas

    AreaRegistration.RegisterAllAreas();

    // Register filters and routes

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    // Register MEF catalogs

    var catalog = new DirectoryCatalog(
        Path.Combine(AppDomain.CurrentDomain.BaseDirectory, “bin”));
    MvcServiceLocator.SetCurrent(new MefServiceLocator(catalog, MvcServiceLocator.Default));
}

Can you spot the 3 lines of code? This is really all it takes to make the complete application use MEF where appropriate. (Ok, that is a bit of a lie since you would still have to implement a very small IFilterProvider if you want MEF in your action filters, but still.)

Hooks

The cool thing is: a lot of things are now requested in the service locator we just created. When browsing to my site index, here’s all the things that are requested:

  • Resolve called for serviceType: System.Web.Mvc.IControllerFactory
  • Resolve called for serviceType: Mvc3WithMEF.Controllers.HomeController
  • Resolve called for serviceType: System.Web.Mvc.IFilterProvider
  • Resolve called for serviceType: System.Web.Mvc.IFilterProvider
  • Resolve called for serviceType: System.Web.Mvc.IFilterProvider
  • Resolve called for serviceType: System.Web.Mvc.IFilterProvider
  • Resolve called for serviceType: System.Web.Mvc.IViewEngine
  • Resolve called for serviceType: System.Web.Mvc.IViewEngine
  • Resolve called for serviceType: ASP.Index_cshtml
  • Resolve called for serviceType: System.Web.Mvc.IViewEngine
  • Resolve called for serviceType: System.Web.Mvc.IViewEngine
  • Resolve called for serviceType: ASP._LogOnPartial_cshtml

Which means that you can now even inject stuff into views or compose their parts dynamically.

Conclusion

I have a strong sense of a power in here… ASP.NET MVC 3 will support DI natively if you want to use it, and I’ll be one of the users happily making use of it. There’s use cases for injecting/composing something in all of the above components, and ASP.NET MVC 3 made this just simpler and more straightforward.

Here’s my sample code with some more examples in it: Mvc3WithMEF.zip (256.21 kb)