MEF2 with MVC4 via Microsoft.Composition

Jul 27, 2012 at 3:34 PM

I'm trying to get this demo working

As far as I can tell, the demo was created using these two NuGet packages.

Both of these packages are listed as deprecated and suggest the use of Microsoft.Composition instead.

So in my sample project I have installed the Microsoft.Composition package and replicated the demo code.  When I run the app I get an error on my home controller stating that it wants a controller constructor with no parameters.  This makes me think that the regular MVC4 process is spinning up the controller rather than the MEF enabled one.

Do I have the right packages installed?  What do I need to run MVC4 and MEF2/.NET 4.5 together?

Jul 27, 2012 at 5:42 PM

Hello - we posted some details on this in an earlier thread where you might find some background info:

Our recommendation at present is to use the sample project available at in order to integrate Microsoft.Composition with ASP.NET MVC.

Hope this helps,

Jul 27, 2012 at 7:20 PM

Awesome - thanks for the reply.  I will work through these posts/links.

Jul 30, 2012 at 9:13 PM
Edited Jul 30, 2012 at 9:16 PM

As a follow up to anyone that might come across this later, I was able to get a solution up and running.  See the files from phobis / philcarbone at the end of this thread and the note about the AssemblyInfo file.

Oct 26, 2012 at 12:38 PM

Integrating MEF with MVC .NET 4.5 and especially WebAPI is as clear as mud. I can't put into words how frustrating it is to weed through it all.

If you're new to MEF with MVC and you're trying to build an extensible solution that uses IoC and a pluggable architecture you will literally download dozens of examples directly from Microsoft and others around the web but later find that it relies on pre-release software or that the implementation breaks routing or it is overkill or you name it...

Eventually you'll find the examples as referenced above and after much wasted time you'll discover that for some reason the MEF 2 and MVC integration story has stopped dead in it's tracks.

The recommendation by Nick Blumhardt above is a recipe for disaster in my mind. First of all it is a little vague and his direct reference to a single class within the demo causes even more confusion. Never mind the namespace conflicts and reliance upon custom MEF libraries that you'll never want to manage moving forward.

From Channel9 videos to MSDN documentation, to posts the like the ones above, you'll find so many "solutions" that you should never use in production it isn't even funny.

Okay so enough ranting. If you're looking for an up to date (Aug. 2012) MEF with MVC.NET 4.5 and WebAPI article to get started look here: I can't tell you how many times I skipped over this article after being wooed by examples tied to MEF2 with the promise of simplicity.

It's really too bad because as you know, the story for MVC was almost complete but some dickhead decided to drop the ball and because of it many of us are spending DAYS weeding through the bullcrap. Is Microsoft getting too big? So much for reliable guidance.

Anyway, start with the example code and slowly break out the classes into separate libraries while building and testing all the while. You'll see that it just continues to work and is pretty simple to implement.

Thanks for listening Microsoft and please, either continue supporting what you're articles advertise or take down articles that rely on something that was deprecated. This would help many of us tremendously.


Nov 7, 2012 at 3:43 PM


I couldn't agree more with you!!! I can't believe that with all of the beauty that MVC4 + WebApi brings, there isn't a better story around a simple dependency injection and IoC solution. Basically, I just want a NuGet package that I can download and have Mef-based DI working for my MVC 4 apps - including Controller injection and ApiController injection working in the same project.

If I'm missing something and there is a solution out there, I'd love to hear about it.

Nov 7, 2012 at 5:53 PM

I finally cobbled together a basic implementation that I'm going to use from now on. It uses the lightweight MEF framework from NuGet (Microsoft.Composition) along with some WebActivator magic and the code from the MEF team to jump-start a project.



Nov 7, 2012 at 9:14 PM

I've done something like that on my project (

In the sourcecode, you'll find a proof of concept (dev/V0/Mefisto4), where I use MEF and MVC4 together, allowing me to have some plugins, working without restarting the website.

The magic comes from my rewriting of the controller and view engines.

Just take a look when you'll have a moment ;)



Nov 24, 2012 at 7:53 PM


I have just spent couple of hours trying to do some MVC4 + MEF2 (with parts defined in separate assembly), also agree with cmschick, lot of samples but there are not 2 working the same... and none of them working as expected with the environment I have.

Anyway I finally managed to have it working as expected, and here are my findings, just in case these could help someone.

Here is my setup:

I have a simple ASP MVC 4 web application, and I want to separate some functionality into a separate assembly with the scope of reusing them in other projects (e.g.  security infrastructure and access control at very granular level - buttons...fields and so on). I would use RazorGenerator in the external assembly to precompile the views.

The only configuration that worked properly for me and my use case is as following:

- I had to uninstall what I installed from NuGet because I got an error "Ambiguous....." every time I wanted to put the external assembly into the catalog, eventually I tracked the problem to the generated class of the view with RazorGenerator tool, I don't understood why that class would affect the composition it was not in a Part folder it was not tagged with Export attribute.

But then I observed that the binaries from NuGet had version and the binaries that I obtained by compiling source code from CodePlex were so I decided to uninstall the NuGet and use the binaries I had compiled.

With these binaries I had to use a different approach to configure the composition actually a pretty simple one:

public static class CompositionConfig
    public static void RegisterComposition()
        var a = new Assembly [] { typeof(CompositionConfig).Assembly, typeof(IAuthService).Assembly };




This way the composition container is properly set and I had no errors, the parts exported from the other assembly get injected properly also the precompiled Razor views works.

There is no need to create a custom controller factory, in the past I follow such an approach - I mean custom controller factory - (with MEF 1) but for some reasons the application was leaking a lot and I changed MEF 1 to an earlier version of MEF 2 (with MVC 3), and I got rid of leaks, so watch out if your parts get collected as expected when you are using custom controller factory.




Nov 30, 2012 at 10:59 AM


I you want to use a ready a nuget package to test MEF with MVC4 and WebApi, you can get it from . I factored the code found in this post . If you need to install it you can find more info here.

Mar 13, 2013 at 4:49 PM
I am a bit confused as to what is the best option for utilizing MEF in and MVC 4 application.

Kenny, your solution seems to use the "older" version of MEF and not the lighter weight Microsoft.Composition nuget project. (unless I am completely confused and the new code is in .net 4.5??)

What is gained by using the newer nuget project if any?

I currently have my project utilizing the mvc integration providing by the now out of date MefContrib.MVC project (, which seems to be working but I am noticing a bit of overhead in production compared to my non MEF applications.

Does .net 4.5 include some of the new MEF 2 code? I find the whole MEF project(s) confusing even the home page for this project says to use the original MEF found in .net and not this project but then in the next box says to use the fully supported Microsoft.Composition which is it?

(from home page)
Important: this site hosts source code for unsupported, pre-release versions of MEF. If you want to use MEF in production, you should strongly consider using the supported versions included in the .NET Framework and Silverlight.

.NET 4.5 and Windows 8 Store apps can now install the fully-supported, lightweight Microsoft.Composition package from NuGet.
So in summary my question is:

If I am going to start a brand new project using MVC 4, WebAPI and MEF what project should I use and what is the best current example?
May 2, 2013 at 11:11 PM
Edited May 18, 2013 at 1:25 AM
For anyone that is interested in getting MVC to use MEF 2 using convention (attribute-less) based here is an article that can guide you how to get this setup.
May 29, 2013 at 12:51 PM
Nick provided the base for what is now WebApiContrib.IoC.Mef for use with Web API, as well. So if you want Web API support, take a look at that package.