dotnet核心Web应用程序中的Castle Windsor生活方式 [英] Castle Windsor lifestyle in dotnet core web application
问题描述
使用Castle Windsor MS Adapter时应采用哪种生活方式来代替PerWebRequest?
What lifestyle should be used to replace PerWebRequest when using the Castle Windsor MS Adapter?
https://github.com/volosoft/castle-windsor-ms-adapter
在dotnet核心之前,我将对容器中的几乎所有组件使用PerWebRequest生活方式.现在,我们已与IIS模块和http上下文断开连接,我想确保在Web请求开始和结束时都可以创建和处理我的组件.
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.
示例:
container.Register(Component.For<MyEntityFrameworkContext>)
.ImplementedBy<MyEntityFrameworkContext>()
.LifestyleTransient());
推荐答案
ASP.NET Core具有自己的作用域"生命周期,即按请求".请参阅它的文档: https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/dependency-injection
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
用法示例:
services.AddScoped<ICharacterRepository, CharacterRepository>();
您应该在 Startup
类的 ConfigureServices
方法内进行此操作.
You should do it inside ConfigureServices
method in Startup
class.
这篇关于dotnet核心Web应用程序中的Castle Windsor生活方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!