0

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?

SteveC
  • 15,808
  • 23
  • 102
  • 173
Emanuele Ciriachi
  • 2,216
  • 2
  • 26
  • 39

1 Answers1

3

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.

Krzysztof Kozmic
  • 27,267
  • 12
  • 73
  • 115
  • 1
    Yes, this was indeed the case. After struggling to get the right .pdb for Castle Core/Windsor and (managing to step into the code), this was the culprit. – Emanuele Ciriachi Dec 21 '16 at 13:32