Skip to content

Instantly share code, notes, and snippets.

@cwiederspan
Created October 17, 2013 21:38
Show Gist options
  • Select an option

  • Save cwiederspan/7032636 to your computer and use it in GitHub Desktop.

Select an option

Save cwiederspan/7032636 to your computer and use it in GitHub Desktop.
This uses MEF configuration to enable dependency injection for the MVC 5 AccountController based on the new OWIN. The critical lines are 54-55 and 83-86.
using System.Linq;
using System.Composition.Convention;
using System.Composition.Hosting;
using System.Reflection;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Mvc;
using SourceMax.Web.IoC;
using Microsoft.AspNet.Identity;
using Vertex.Net.Web.Models;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Data.Entity;
[assembly: WebActivator.PreApplicationStartMethod(typeof(Vertex.Net.Web.MefConfig), "Initialize")]
namespace Vertex.Net.Web {
public class MefConfig {
/// <summary>
/// This method provides the entry point that is called by WebActivator based on the assembly directive above.
/// This method is responsible for bootstrapping the Mef configuration based on the assemblies and conventions
/// provided in the methods below.
/// </summary>
public static void Initialize() {
// Get an array of assemblies that will be providing the import and export functionality
var appAssemblies = GetAppAssemblies();
// Get the conventions that will be used to find the imports and exports within the application
var conventions = GetConventions();
// Use the assemblies and conventions to create the dependency container
var container = GetContainer(appAssemblies, conventions);
// Create and apply a MefDependencyResolver so ApiControllers can be composed
GlobalConfiguration.Configuration.DependencyResolver = new MefDependencyResolver(container);
// Create and apply a MefControllerFactory so (non-WebApi) controllers can be composed
ControllerBuilder.Current.SetControllerFactory(new MefControllerFactory(container));
}
/// <summary>
/// This method is responsible for compiling an array of assemblies that house the imports
/// and exports that the system will need to resolve.
/// </summary>
/// <returns>An array of Assembly objects.</returns>
private static Assembly[] GetAppAssemblies() {
var appAssemblies = new Assembly[] {
Assembly.GetExecutingAssembly(),
Assembly.GetAssembly(typeof(UserStore<>)),
Assembly.GetAssembly(typeof(UserManager<>))
};
return appAssemblies;
}
/// <summary>
/// This method is responsible for building up the conventions that can be used to resolve
/// import and export requests for the system. For example, all Controllers (that inherit
/// from IController) will be automatically exported.
/// </summary>
/// <returns>A ConventionBuilder object containing all of the conventions.</returns>
private static ConventionBuilder GetConventions() {
var conventions = new ConventionBuilder();
// TODO: Customize or add more conventions here as necessary
conventions.ForTypesDerivedFrom<IHttpController>().Export();
conventions.ForTypesDerivedFrom<IController>().Export();
conventions.ForTypesMatching(t => t.Namespace != null && t.Namespace.EndsWith(".Parts"))
.Export()
.ExportInterfaces();
// Setup the export rules for the ASP.Net Identity objects
conventions.ForType<ApplicationDbContext>().Export<DbContext>();
conventions.ForType(typeof(UserStore<>)).Export(b => b.AsContractType(typeof(IUserStore<>)));
conventions.ForType(typeof(UserManager<>)).Export();
conventions.ForType<UserManager<ApplicationUser>>().SelectConstructor(ctors => ctors.First(info => info.GetParameters().Length == 1));
return conventions;
}
/// <summary>
/// This method uses the assemblies and conventions to create the composition container that will
/// be used by the system for composition.
/// </summary>
/// <param name="assemblies">An array of assemblies that are used to supply imports and exports.</param>
/// <param name="conventions">A set of conventions to be used to automatically import and export.</param>
/// <returns>A Mef-based Composition host that is ready to resolve composition requests.</returns>
private static CompositionHost GetContainer(Assembly[] assemblies, ConventionBuilder conventions) {
var container = new ContainerConfiguration()
.WithAssemblies(assemblies, conventions)
.CreateContainer();
return container;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment