Last active
March 31, 2016 14:30
-
-
Save bcanzanella/a534c6b576522b7f7852cbc11a2bcd7b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[assembly: OwinStartupAttribute(typeof(Foo.Startup))] | |
namespace Foo | |
{ | |
public partial class Startup | |
{ | |
public void Configuration(IAppBuilder app) | |
{ | |
ConfigureAuth(app); | |
// add filter for your dlls | |
ControllerBuilder.Current.SetControllerFactory(new MefControllerFactory(MefConfig.Build("*Foo*Plugin.dll"))); | |
} | |
} | |
public static class MefConfig | |
{ | |
public static CompositionContainer Container { get; private set; } | |
public static CompositionContainer Build(string filter = "*.dll") | |
{ | |
var files = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, filter, SearchOption.AllDirectories) | |
.Where(_ => !_.Replace(AppDomain.CurrentDomain.BaseDirectory, "").Contains(@"obj\")); | |
var catalogAggregator = new AggregateCatalog(); | |
foreach (var file in files) | |
{ | |
catalogAggregator.Catalogs.Add(new AssemblyCatalog(Assembly.LoadFrom(file))); | |
} | |
Container = new CompositionContainer(catalogAggregator); | |
return Container; | |
} | |
} | |
public class MefControllerFactory : DefaultControllerFactory | |
{ | |
private readonly CompositionContainer _container; | |
public MefControllerFactory(CompositionContainer container) | |
{ | |
_container = container; | |
} | |
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType) | |
{ | |
IController controller = null; | |
var export = _container.GetExports(typeof(IController), null, controllerType.FullName).SingleOrDefault(); | |
if (export != null) | |
{ | |
controller = export.Value as IController; | |
if (controller != null) | |
{ | |
_container.SatisfyImportsOnce(controller); | |
} | |
} | |
return controller; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment