c# - Correctly set Scope when using WebApi, SimpleInjector and MediatR -
controller
public class locationscontroller : apicontroller { private readonly imediator _mediator; public locationscontroller(imediator mediator) { _mediator = mediator; } public ienumerable<place> get() { return _mediator.send(new getlatestmapdata<place>()); } }
on first request of get() action, handler instantiated simpleinjector , executed correctly.
on second request (f5 in browser e.g.), fails :
handler not found request of type ....
container or service locator not configured or handlers not registered container.
and inner exception of:
cannot access disposed object.
object name: 'the threadlocal object has been disposed.'
owin startup
public class startup { public void configuration(iappbuilder app) { // simpleinjector var container = compositionroot.createcontainer(); var config = globalconfiguration.configuration; config.dependencyresolver = new simpleinjectorwebapidependencyresolver(container); // routing config.maphttpattributeroutes(); config.routes.maphttproute("defaultapi", "api/{controller}/{id}", new { id = routeparameter.optional }); config.ensureinitialized(); app.usewebapi(config); } }
simpleinjector ipackage webapi project
public class installer : ipackage { public void registerservices(container c) { c.options.defaultscopedlifestyle = new webapirequestlifestyle(); c.registerwebapicontrollers(globalconfiguration.configuration); } }
i think what's happening handler correctly created, , disposed after first request. now, don't know why, on subsequent requests, handler isn't re-created. know because if change webapirequestlifestyle 'not dispose when scope ends', works every request:
c.options.defaultscopedlifestyle = new webapirequestlifestyle(false /*disposeinstancewhenscopeends*/);
questions
- should keep
disposeinstancewhenscopeends
parameter set false? - if not, correct solution?
- i see has been solved before creating lifetimescopedecorator... however, surely functionality provided simpleinjector webapi integration library? missing?
(and thank reading)
this link provides guidance on dependency resolution , using idependencyresolver / idependencyscope interfaces.
immediately see touch on life spans tend little tricky.
this section in particular interesting:
dependenecy scope , controller lifetime
controllers created per request. manage object lifetimes, idependencyresolver uses concept of scope.
the dependency resolver attached httpconfiguration object has global scope. when web api creates controller, calls beginscope. method returns idependencyscope represents child scope.
web api calls getservice on child scope create controller. when request complete, web api calls dispose on child scope. use dispose method dispose of controller’s dependencies.
conventionally bootstrapping service occur once during app start-up , know resolve dependencies @ time. when worker process shutting down (no activity, example) invoke dispose.
ordinarily think quite normal resolved instances remain life cycle unless imperative destroyed after use. example given explains must correctly dispose once request complete. recommend dispose of instances correctly using examples provided guidance.
this helped me when working ioc , webapi. hope helps!
Comments
Post a Comment