Castle Windsor lifestyle in dotnet core web application

故事扮演 提交于 2019-12-24 04:24:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!