问题
What lifestyle should be used to replace PerWebRequest when using the Castle Windsor MS Adapter?
https://github.com/volosoft/castle-windsor-ms-adapter
Before dotnet core I would use the PerWebRequest lifestyle for almost all of the components in the container. Now that we are disconnected from IIS modules and http context, I want to ensure my components are being created and disposed when the web requests starts and ends.
Example:
container.Register(Component.For<MyEntityFrameworkContext>)
.ImplementedBy<MyEntityFrameworkContext>()
.LifestyleTransient());
回答1:
ASP.NET Core has it's own 'scoped' lifecycle, which is 'per request'. See it's documentation: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection
Usage example:
services.AddScoped<ICharacterRepository, CharacterRepository>();
You should do it inside ConfigureServices
method in Startup
class.
来源:https://stackoverflow.com/questions/40592473/castle-windsor-lifestyle-in-dotnet-core-web-application