Castle.Windsor生活方式根据上下文? [英] Castle.Windsor lifestyle depending on context?

查看:222
本文介绍了Castle.Windsor生活方式根据上下文?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a web application where many components are registered using .LifestylePerWebRequest(), now I've decided to implement Quartz.NET, a .NET job scheduling library, which executes in separate threads, and not the Request thread.

因此​​, HttpContext.Current 收益率。我的服务,仓库和的IDbConnection 被实例化至今使用 .LifestylePerWebRequest(),因为它使人们更容易处置他们当请求结束。

As such, HttpContext.Current yields null. My services, repositories, and IDbConnection were instanced so far using .LifestylePerWebRequest() because it made it easier to dispose of them when the requests ended.

现在我想在这两种情况下使用这些组件,在Web请求我希望他们能不受影响,而在非请求的背景下,我希望他们使用不同的生活方式,我想我可以处理处置自己,但如何我应该去为它选择基于当前上下文的组件的生活方式吗?

Now I want to use these components in both scenarios, during web requests I want them to remain unaffected, and in non-request contexts I want them to use a different Lifestyle, I figure I can handle the disposing myself, but how should I go about it for choosing a lifestyle for the components based on the current context?

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

Currently I register services (for example), like this:

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

我想我应该用某种扩展方法,但我就是不明白它..

I figure I should be using some kind of extension method but I just don't see it..

推荐答案

您应该使用从混合生活 <一个href=\"https://github.com/castleprojectcontrib/Castle.Windsor.Lifestyles\">castleprojectcontrib.

这是混合型的生活方式是一个真正融合了由两个下层的生活方式:主要的生活方式和二次生活方式。混合生活方式首先尝试使用的主要生活方式;如果它是由于某种原因不可用,则使用二次生活方式。这通常与PerWebRequest用作主要生活方式:如果HTTP上下文是可用的,它被用来作为作用域组件实例;否则,使用二次生活方式。

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.

这篇关于Castle.Windsor生活方式根据上下文?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆