gpt4 book ai didi

c# - CaSTLe.Windsor 生活方式取决于上下文?

转载 作者:IT王子 更新时间:2023-10-29 04:19:04 26 4
gpt4 key购买 nike

我有一个 Web 应用程序,其中许多组件都是使用 .LifestylePerWebRequest() 注册的,现在我决定实现 Quartz.NET,一个 .NET 作业调度库,它在单独的线程中执行,而不是在请求线程中执行。

因此,HttpContext.Current 产生 null。到目前为止,我的服务、存储库和 IDbConnection 都是使用 .LifestylePerWebRequest() 实例化的,因为这样可以更轻松地在请求结束时处理它们。

现在我想在两种情况下都使用这些组件,在网络请求期间我希望它们不受影响,而在非请求上下文中我希望它们使用不同的 Lifestyle,我想我可以自己处理,但是如何我应该根据当前环境为组件选择生活方式吗?

目前我注册服务(例如),像这样:

container.Register(
AllTypes
.FromAssemblyContaining<EmailService>()
.Where(t => t.Name.EndsWith("Service"))
.WithService.Select(IoC.SelectByInterfaceConvention)
.LifestylePerWebRequest()
);

我想我应该使用某种扩展方法,但我只是没有看到它..

最佳答案

你应该使用 Hybrid Lifestyle来自 castleprojectcontrib .

An hybrid lifestyle is one that actually blends two underlying lifestyles: a main lifestyle and a secondary lifestyle. The hybrid lifestyle first tries to use the main lifestyle; if it's unavailable for some reason, it uses the secondary lifestyle. This is commonly used with PerWebRequest as the main lifestyle: if the HTTP context is available, it's used as the scope for the component instance; otherwise the secondary lifestyle is used.

关于c# - CaSTLe.Windsor 生活方式取决于上下文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11873905/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com