瞬态生活方式需要HttpContext吗

瞬态生活方式需要HttpContext吗

本文介绍了瞬态生活方式需要HttpContext吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在这样注册我的组件:

I am registering my component like this:

public static void Register(IWindsorContainer container)
    {
    container.Register(Classes.FromAssembly(Assembly.GetAssembly(typeof(GenericBaseRepository)))
        .InSameNamespaceAs<GenericBaseRepository>()
        .WithService.DefaultInterfaces()
        .LifestyleTransient());
    }

然后我用一段没有HttpContext的代码来解决它: / p>

I am then resolving it in a piece of code that has no HttpContext:

var baseRepository = ContainerManager.Container.Resolve<IBaseRepository>();

(IBaseRepository是GenericBaseRepository实现的接口)。这将失败,并显示以下消息:

(IBaseRepository being an interface implemented by GenericBaseRepository). This fails with the following message:

HttpContext.Current为空。PerWebRequestLifestyle只能在ASP.Net中使用

这让我感到困惑,因为我选择的生活方式是短暂的,而不是PerWebRequest。
当然,在计划的任务中HttpContext不存在-但我并不是真的需要它,我只想要一个我的存储库实例,该实例不会与任何Web请求进行交互。

Which confuses me, because the lifestyle I choose is Transient, not PerWebRequest.Of course, HttpContext doesn't exist during a scheduled task - but I don't really need it, I just want an instance of my Repository which will not interact with any web request.

那么,为什么温莎城堡在解决组件时坚持要求HttpContext?

So, why does Castle Windsor insist in requiring an HttpContext when resolving my component?

推荐答案

有查看完整的异常消息。您的根组件可能是暂时的,但异常表明它是每个Web请求生活方式所使用的依赖项之一。

Have a look at the full exception message. Your root component may be transient but the exception indicates one of its dependencies uses per web request lifestyle.

看看温莎的,可以帮助您查明问题。

Have a look at Windsor's diagnostics debugger view, that may help you pinpoint it.

这篇关于瞬态生活方式需要HttpContext吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 23:48