asp.net mvc - Import aspect is not working using MEF -
using mefcontrib.mvc3 on mvc web application. used following class initialize mef before application start event. class auto generated(downloaded) nuget. tiny changes applied catalog(for plugins folder) , controller factory segments.
[assembly: webactivator.preapplicationstartmethod(typeof(omr.cms.app_start.mefcontribmvc3), "start")] namespace omr.cms.app_start { using system.componentmodel.composition.hosting; using system.linq; using system.web.mvc; using mefcontrib.hosting.conventions; using mefcontrib.web.mvc; public static class mefcontribmvc3 { public static void start() { // register compositioncontainerlifetimehttpmodule httpmodule. // makes sure cleaned correctly after each request. compositioncontainerlifetimehttpmodule.register(); // create mef catalog based on contents of ~/bin. // // note class in referenced assemblies implementing in "icontroller" // automatically exported mef. there no need explicit [export] attributes // on asp.net mvc controllers. when implementing multiple constructors ensure // there 1 constructor marked [importingconstructor] attribute. var catalog = new aggregatecatalog( //new directorycatalog("bin"), new directorycatalog("plugins"), new conventioncatalog(new mvcapplicationregistry())); // note: add own (convention)catalogs here if needed. // tell mvc3 use mef dependency resolver. var dependencyresolver = new compositiondependencyresolver(catalog); dependencyresolver.setresolver(dependencyresolver); // tell mvc3 resolve dependencies in controllers controllerbuilder.current.setcontrollerfactory(new mymefcontrollerfactory(dependencyresolver)); // tell mvc3 resolve dependencies in filters filterproviders.providers.remove(filterproviders.providers.single(f => f filterattributefilterprovider)); filterproviders.providers.add(new compositionfilterattributefilterprovider(dependencyresolver)); // tell mvc3 resolve dependencies in model validators modelvalidatorproviders.providers.remove(modelvalidatorproviders.providers.oftype<dataannotationsmodelvalidatorprovider>().single()); modelvalidatorproviders.providers.add( new compositiondataannotationsmodelvalidatorprovider(dependencyresolver)); // tell mvc3 resolve model binders through mef. note model binder should decorated // [modelbinderexport]. modelbinderproviders.binderproviders.add( new compositionmodelbinderprovider(dependencyresolver)); } } }
and using followin code on application class:
using omr.core.web.filters; using omr.core.web.routing; using system; using system.collections.generic; using system.componentmodel.composition; using system.web.mvc; using system.web.optimization; using system.web.routing; namespace omr.cms { public class mvcapplication : system.web.httpapplication { [importmany(typeof(ifilterregistration))] public ienumerable<lazy<ifilterregistration>> filters { get; set; } protected void application_start() { arearegistration.registerallareas(); bundleconfig.registerbundles(bundletable.bundles); registerfilters(); registerroutes(); } private void registerfilters() { var filters = globalfilters.filters; var filterregistrations = dependencyresolver.current.getservices<ifilterregistration>(); foreach (var registar in filterregistrations) registar.register(filters); } public void registerroutes() { var routes = routetable.routes; var routeregistrations = dependencyresolver.current.getservices<irouteregistration>(); // ignores foreach (var registrar in routeregistrations) registrar.ignore(routes); // routes foreach (var registrar in routeregistrations) registrar.route(routes); } } }
the problem following property null on application start method.
importmany(typeof(ifilterregistration))] public ienumerable<lazy<ifilterregistration>> filters { get; set; }
but following lines working:
var filterregistrations = dependencyresolver.current.getservices<ifilterregistration>();
why import declaration isn't work?
Comments
Post a Comment