Created
May 23, 2011 11:29
-
-
Save StephenRedd/986566 to your computer and use it in GitHub Desktop.
blog: MEF and MVC - Limitations and workarounds for partial trust environments
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
| Attempt by method 'DynamicClass.lambda_method(System.Runtime.CompilerServices.Closure)' | |
| to access type 'System.ComponentModel.Composition.Hosting.ComposablePartCatalogCollection' failed. | |
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
| <trust level="High"/> |
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
| var cat = new DirectoryCatalog(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin")); | |
| var parts = cat.Parts.ToArray(); |
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
| public class MefControllerFactory : IControllerFactory | |
| { | |
| public IController CreateController(RequestContext requestContext, string controllerName) | |
| { | |
| var catalog = new DirectoryCatalog(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin")); | |
| var requestContainer = new CompositionContainer(catalog); | |
| var controller = requestContainer.GetExportedValue(controllerName); | |
| if (controller == null){ throw new HttpException(404, "Not found");} | |
| return controller; | |
| } | |
| } |
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
| public static class ContainerManager | |
| { | |
| private static CompositionContainer _container; | |
| public static CompositionContainer ApplicationContainer | |
| { | |
| get | |
| { | |
| if (_container == null) | |
| { | |
| var catalog = new DirectoryCatalog(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin")); | |
| _container = new CompositionContainer(catalog); | |
| } | |
| return _container; | |
| } | |
| } | |
| } |
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
| public class MefHttpApplication : HttpApplication | |
| { | |
| public static ComposablePartCatalog RootCatalog { get; private set; } | |
| public static CompositionContainer ApplicationContainer { get; private set; } | |
| public static ComposablePartCatalog ControllerCatalog { get; private set; } | |
| protected virtual void Application_Start() | |
| { | |
| if (RootCatalog == null){ RootCatalog = CreateRootCatalog(); } | |
| if (ApplicationContainer == null) | |
| { | |
| ApplicationContainer = new CompositionContainer(RootCatalog, false); | |
| } | |
| if (ControllerCatalog == null) | |
| { | |
| var controllerTypes = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.GetInterfaces().Any(i => i == typeof(IController))); | |
| ControllerCatalog = new TypeCatalog(controllerTypes); | |
| } | |
| ControllerBuilder.Current.SetControllerFactory(new MefControllerFactory()); | |
| } | |
| protected virtual void Application_End() | |
| { | |
| if (ApplicationContainer != null){ApplicationContainer.Dispose();} | |
| } | |
| protected virtual ComposablePartCatalog CreateRootCatalog() | |
| { | |
| return new DirectoryCatalog(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "bin")); | |
| } | |
| } |
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
| public class MvcApplication : MefHttpApplication | |
| { | |
| public static void RegisterRoutes(RouteCollection routes) | |
| { | |
| //normal route stuff | |
| } | |
| protected override void Application_Start() | |
| { | |
| base.Application_Start(); | |
| AreaRegistration.RegisterAllAreas(); | |
| RegisterRoutes(RouteTable.Routes); | |
| } | |
| } |
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
| public class MefControllerFactory : IControllerFactory | |
| { | |
| public IController CreateController(RequestContext requestContext, string controllerName) | |
| { | |
| var requestContainer = GetRequestControllerContainer(requestContext.HttpContext.Items); | |
| var controller = requestContainer.GetExportedValue(controllerName); | |
| if (controller == null){throw new HttpException(404, "Not found");} | |
| return controller; | |
| } | |
| public void ReleaseController(IController controller){/*nothing to do*/} | |
| public static CompositionContainer GetRequestControllerContainer(IDictionary contextItemsCollection) | |
| { | |
| var app = (MefHttpApplication)HttpContext.Current.ApplicationInstance; | |
| if (contextItemsCollection == null) throw new ArgumentNullException("dictionary"); | |
| var container = (CompositionContainer)contextItemsCollection["MefRequestControllerContainer"]; | |
| if (container == null) | |
| { | |
| container = new CompositionContainer(MefHttpApplication.ControllerCatalog, false, MefHttpApplication.ApplicationContainer); | |
| contextItemsCollection["MefRequestControllerContainer"] = container; | |
| } | |
| return container; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment