Lorenzo
Lorenzo

Reputation: 29427

Mixing Simple Injector Scopes in MVC, Web API and OWIN

I would like to migrate my setup with Ninject to SimpleInjector. At the moment I have a custom library which is referenced from - an ASP.NET MVC application - an ASP.NET Web API application

This library has a NinjectModule implementation which features some declaration like the following

Bind<MyDataContext>().To<MyDataContext>().InRequestScope();

My question is relative to the InRequestScope() lifetime. As I read on the SimpleInjector documentation, for the ASP.NET MVC application is suggested to use WebRequestLifestyle as the DefaultScopedLifestyle option while is suggested AsyncScopedLifestyle for the Web API.

Also when using Owin, which is my case in both the apps, the docs suggests to wrap everything within a block like

app.Use(async (context, next) => {
    using (AsyncScopedLifestyle.BeginScope(container)) {
        await next();
    }
});

For my understanding this means:

  1. every registration should be registered with the Lifestyle.Scoped lifestyle
  2. Both the applications should allow scoped instances to be resolved through the app.Use( ... ) code block

Is this correct?

Upvotes: 1

Views: 765

Answers (1)

Steven
Steven

Reputation: 172825

every registration should be registered with the Lifestyle.Scoped lifestyle

All registrations that would have been InRequestScope in Ninject can now be registered as Lifestyle.Scoped in Simple Injector.

Both the applications should allow scoped instances to be resolved through the app.Use( ... ) code block

When the app.Use( ... ) code block with the AsyncScopedLifestyle is applied to all requests, this means that MVC code can run within an Async Scope as well. This means that you don't need the WebApiRequestLifestyle anymore and can use AsyncScopedLifestyle for your MVC application as well.

Upvotes: 2

Related Questions