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
I'd like to propose a more intuitive and fluent way of integrating Simple Injector with ASP.NET Core:
publicvoidConfigureServices(IServiceCollectionservices){
services
.AddSimpleInjector(container,options =>// .Integration.ServiceCollection{ options.AddAspNetCore()// .Integration.AspNetCore.AddControllerActivation()// .Integration.AspNetCore.Mvc.Core.AddViewComponentActivation()// .Integration.AspNetCore.Mvc.Core.AddPageModelActivation()// .Integration.AspNetCore.Mvc.AddTagHelperActivation();// .Integration.AspNetCore.Mvc});}publicvoidConfigure(IApplicationBuilderapp,IHostingEnvironmentenv){
app.UseSimpleInjector(container,options =>// .Integration.ServiceCollection{ options.UseMiddleware<CustomMiddleware1>(app);// .Integration.AspNetCore// Default value AutoCrossWireFrameworkComponents == true options.AutoCrossWireFrameworkComponents =true;// .Integration.AspNetCore// Or use CrossWire in case you don't want to auto cross wire options.CrossWire<ILoggerFactory>();// .Integration.AspNetCore options.CrossWire<IOptions<IdentityCookieOptions>>();});}
Explanation:
The AddSimpleInjector(Container, Action<Options>) extension method returns the IServiceCollection to allow other calls to be added fluently.
Additional configurations can be added by specifying an options => delegate.
This AddSimpleInjector extension method should do the following things:
Configures the container to make cross wiring possible (EnableSimpleInjectorCrossWiring)
AddAspNetCore integrates Simple Injector with ASP.NET Core and returns a builder to allow further configuration for e.g. MVC. This extension method should do the following things:
Set the Options.DefaultScopedLifestyle to AsyncScopedLifestyle, because this is the default lifestyle for ASP.NET Core applications.
Wrap each web request in an AsyncScopedLifestyle scope (i.e. UseSimpleInjectorAspNetRequestScoping)
Allow auto-cross-wired services to be resolved within the current web request, to prevent Torn Lifestyles.
Add an IHttpContextAccessor to IServiceCollection in case it is missing. An IHttpContextAccessor is required to achieve the auto cross wiring as described in the previous point.
AddControllerActivation is equivalent to adding the SimpleInjectorControllerActivator and calling RegisterMvcControllers
AddViewComponentActivation is equivalent to adding the SimpleInjectorViewComponentActivator and calling RegisterMvcViewComponents
AddPageModelActivation is equivalent to adding the SimpleInjectorPageModelActivatorProvider and calling RegisterPageModels
AddTagHelperActivation is equivalent to adding the AddSimpleInjectorTagHelperActivation and registering all tag helpers in Simple Injector (there currently is no RegisterTagHelpers!!)
UseSimpleInjector is called after ASP.NET Core's IServiceProvider has been built and allows to apply stuff that requires the availability of an IApplicationBuilder or IServiceProvider, making it impossible to apply them at an earlier stage:
options.UseMiddleware<T>(app) is equivalent to calling app.UseMiddleware<T>(container)
options.AutoCrossWireAspNetComponents() is equivalent to calling container.AutoCrossWireAspNetComponents(app)
options.CrossWire<T>() is equivalent to calling container.CrossWire<T>(app) and allows registering services that can't be auto cross wired (e.g. IEnumerable<T>), or when auto cross wiring is not enabled.
Questions:
As scoping is used by almost all users, should AddSimpleInjector enable request scoping automatically with the option to disable it, or the other way around? Downside of enabling this by default is that (in the current versions of ASP.NET Core) there is a performance penalty in having async state. Downside of disabling this by default is that 99% of the users actually need request scoping (or don't care about this performance penalty).
As cross wiring in ASP.NET Core requires an IHttpContextAccessor for resolving non-singleton services, should AddSimpleInjector automatically add an accessor with the risk of lowering the performance for users that don't require scoped services? (related to previous question)
As IServiceCollection (and the built-in container) can be used outside the context of ASP.NET Core, should these extension methods be designed in such way that it can possibly work outside of ASP.NET Core? (Related to Simplify ServiceCollection integration for (non-ASP) .NET Core projects #639) If so, this means that IHttpContextAccessor can't be added automatically by AddSimpleInjector. The same holds for UseSimpleInjector that depends on IApplicationBuilder, which is an ASP.NET Core-specific interface. And AutoCrossWireAspNetComponents currently depends on the existence of IHttpContextAccessor which is ASP.NET Core-specific as well. In the current design suggestion, the AddSimpleInjector and UseSimpleInjector methods are extracted to a non ASP.NET-specific library called (SimpleInjector.Integration.ServiceCollection).
The text was updated successfully, but these errors were encountered:
The current ASP.NET Core integration requires the user to add registrations to the
IServiceCollection
manually, for instance:I'd like to propose a more intuitive and fluent way of integrating Simple Injector with ASP.NET Core:
Explanation:
AddSimpleInjector(Container, Action<Options>)
extension method returns theIServiceCollection
to allow other calls to be added fluently.options =>
delegate.AddSimpleInjector
extension method should do the following things:EnableSimpleInjectorCrossWiring
)AddAspNetCore
integrates Simple Injector with ASP.NET Core and returns a builder to allow further configuration for e.g. MVC. This extension method should do the following things:Options.DefaultScopedLifestyle
toAsyncScopedLifestyle
, because this is the default lifestyle for ASP.NET Core applications.AsyncScopedLifestyle
scope (i.e.UseSimpleInjectorAspNetRequestScoping
)IHttpContextAccessor
toIServiceCollection
in case it is missing. AnIHttpContextAccessor
is required to achieve the auto cross wiring as described in the previous point.AddControllerActivation
is equivalent to adding theSimpleInjectorControllerActivator
and callingRegisterMvcControllers
AddViewComponentActivation
is equivalent to adding theSimpleInjectorViewComponentActivator
and callingRegisterMvcViewComponents
AddPageModelActivation
is equivalent to adding theSimpleInjectorPageModelActivatorProvider
and callingRegisterPageModels
AddTagHelperActivation
is equivalent to adding theAddSimpleInjectorTagHelperActivation
and registering all tag helpers in Simple Injector (there currently is noRegisterTagHelpers
!!)UseSimpleInjector
is called after ASP.NET Core'sIServiceProvider
has been built and allows to apply stuff that requires the availability of anIApplicationBuilder
orIServiceProvider
, making it impossible to apply them at an earlier stage:options.UseMiddleware<T>(app)
is equivalent to callingapp.UseMiddleware<T>(container)
options.AutoCrossWireAspNetComponents()
is equivalent to callingcontainer.AutoCrossWireAspNetComponents(app)
options.CrossWire<T>()
is equivalent to callingcontainer.CrossWire<T>(app)
and allows registering services that can't be auto cross wired (e.g.IEnumerable<T>
), or when auto cross wiring is not enabled.Questions:
AddSimpleInjector
enable request scoping automatically with the option to disable it, or the other way around? Downside of enabling this by default is that (in the current versions of ASP.NET Core) there is a performance penalty in having async state. Downside of disabling this by default is that 99% of the users actually need request scoping (or don't care about this performance penalty).IHttpContextAccessor
for resolving non-singleton services, shouldAddSimpleInjector
automatically add an accessor with the risk of lowering the performance for users that don't require scoped services? (related to previous question)IServiceCollection
(and the built-in container) can be used outside the context of ASP.NET Core, should these extension methods be designed in such way that it can possibly work outside of ASP.NET Core? (Related to Simplify ServiceCollection integration for (non-ASP) .NET Core projects #639) If so, this means thatIHttpContextAccessor
can't be added automatically byAddSimpleInjector
. The same holds forUseSimpleInjector
that depends onIApplicationBuilder
, which is an ASP.NET Core-specific interface. AndAutoCrossWireAspNetComponents
currently depends on the existence ofIHttpContextAccessor
which is ASP.NET Core-specific as well. In the current design suggestion, theAddSimpleInjector
andUseSimpleInjector
methods are extracted to a non ASP.NET-specific library called (SimpleInjector.Integration.ServiceCollection).The text was updated successfully, but these errors were encountered: