Transient Lifestyle requires HttpContext?

北战南征 提交于 2019-12-25 09:16:57

问题


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());
    }

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

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

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

"HttpContext.Current is null. PerWebRequestLifestyle can only be used in ASP.Net"

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.

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


回答1:


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.



来源:https://stackoverflow.com/questions/41253039/transient-lifestyle-requires-httpcontext

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