asp.net mvc - How to extend WCF DataServices and EntityFramework Model using MEF -


i'm using vanilla wcf dataservice class exposing simple entity framework model. service contained in simple web application published iis. nothing fancy, right!

i'm trying build msdn describes way

request , application scope
instances of parts created in mvc app exist in 1 of 2 scopes: request scope or application scope. default, part instances created during web request in request scope. instances of parts in same request shared, if more 1 part imports given part, importers receive reference same instance. each web request has own scope, parts created in 1 request never used fill imports in request. when request ends, associated scope destroyed , parts created destroyed. parts implement idisposable object disposed. ensures sensitive resources, such database connections, closed. sometimes, might not want create part instance each request (for example, when implement app-wide cache). in these cases, can force part created in application scope applying applicationscoped attribute. parts in application scope shared requests in app. part in application scope may depend on other part in application scope imports. parts in application scope must careful thread-safe.

one of main problems don't know create requestscope container. think requestscope container should build outside of dataservice class, right?

any appredicated

just in case need similar, i've got working solution i've post here

public class mefdataservicehostfactory : dataservicehostfactory     {         protected override servicehost createservicehost(             type servicetype, uri[] baseaddresses)         {             servicehost host = base.createservicehost(servicetype, baseaddresses);             host.description.behaviors.add(new mefservicebehavior());             return host;         }     }      public class mefinstanceprovider : iinstanceprovider     {         #region fields          private readonly type _servicetype;         private idependencyscope _discope;          #endregion          #region ctor          public mefinstanceprovider(type servicetype)         {             _servicetype = servicetype;         }          #endregion          public object getinstance(instancecontext instancecontext)         {             return getinstance(instancecontext, null);         } 

here comes important section 1 can use mef or ever ioc.

        public object getinstance(instancecontext instancecontext, message message)         {            _discope = globalconfiguration.configuration.dependencyresolver.beginscope();             return _discope.getservice(_servicetype);         }          public void releaseinstance(instancecontext instancecontext, object instance)         {            if (_discope != null) _discope.dispose();         }     } 

get new instance , dispose request

    public class mefservicebehavior : iservicebehavior     {         public void applydispatchbehavior(servicedescription servicedescription, servicehostbase servicehostbase)         {             foreach (channeldispatcherbase cdb in servicehostbase.channeldispatchers)             {                 var cd = cdb channeldispatcher;                 if (cd != null)                 {                     foreach (endpointdispatcher ed in cd.endpoints)                     {                         ed.dispatchruntime.instanceprovider = new mefinstanceprovider(servicedescription.servicetype);                     }                 }             }         }          public void addbindingparameters(servicedescription servicedescription, servicehostbase servicehostbase,                                          collection<serviceendpoint> endpoints,                                          bindingparametercollection bindingparameters)         {         }          public void validate(servicedescription servicedescription, servicehostbase servicehostbase)         {         }     } 

register custom factory in svc file

<%@servicehost service="dataservice"                factory="mefdataservicehostfactory"                language=c# debug="true" %> 

or add new service route.

routes.add(new serviceroute(string.empty, new mefdataservicehostfactory(), typeof (dataservice))); 

happy coding!


Comments

Popular posts from this blog

php - Why I am getting the Error "Commands out of sync; you can't run this command now" -

linux - Does gcc have any options to add version info in ELF binary file? -

java - Are there any classes that implement javax.persistence.Parameter<T>? -