You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A typical way of working with Simple Injector in Ambient Context-lacking applications (Read: Windows RT) is the following:
// Starting/ending scopesusing(varscope=new Scope(container)){// Attach runtime data to the scope to allow it to flow through the graph
scope.SetItem("requestId", Guid.NewGuid());// Resolve directly from the scope:varservice= scope.GetInstance<IMyService>();await service.DoYourThing();}// Using scopespublicclassCommandDispatcher:ICommandDispatcher{privatereadonlyScopescope;publicCommandDispatcher(Scopescope){this.scope =scope;}publicvoidDispatch<T>(Tcommand){// Services should again be resolved from the scope to continue flowing runtime data.varhandler=this.scope.GetInstance<ICommandHandler<T>>();
handler.Handle(newCommandEnvelope<T>(// Runtime data can be retrieved from the Scope again by injecting it into an adapter:
requestId:(Guid)this.scope.GetItem("requestId"),
command: command));}}
The previous code enables flowing runtime data; in case components must be registered as scoped, a custom scoped lifestyle must be created (since Simple Injector doesn't have anything for this in the box):
sealedclassNonAmbientScopedLifestyle:ScopedLifestyle{publicNonAmbientScopedLifestyle():base("Non-ambient Scoped"){}protectedoverrideFunc<Scope>CreateCurrentScopeProvider(Containerc){returnThrowNotSupported;}[DebuggerStepThrough]privatestatic Scope ThrowNotSupported(){thrownew NotSupportedException("You can't resolve directly from the container.");}}// Registrationvarcontainer=new Container();
container.Options.DefaultScopedLifestyle =new NonAmbientScopedLifestyle();
container.Register<IUserContext,UserContext>(Lifestyle.Scoped);
The text was updated successfully, but these errors were encountered:
A typical way of working with Simple Injector in Ambient Context-lacking applications (Read: Windows RT) is the following:
The previous code enables flowing runtime data; in case components must be registered as scoped, a custom scoped lifestyle must be created (since Simple Injector doesn't have anything for this in the box):
The text was updated successfully, but these errors were encountered: