Construtor/Setter Injection using IoC in HttpHandler, is it possible?

后端 未结 2 738
一个人的身影
一个人的身影 2021-01-18 14:53

I\'ve ran into a rather hairy problem. There is probably a simple solution to this but I can\'t find it!

I have a custom HttpHandler that I want to process a request

相关标签:
2条回答
  • 2021-01-18 15:33

    What you could do is have the HttpHandler call out to another object that actually handles the request. so in your HttpHandler's ProcessRequest method you would do something like this:

    public void ProcessRequest(HttpContext context)
    {
     var myHandlerObject = container.Resolve<HandlerObject>();
     myHandlerObject.ProcessRequest(context or some state/info that is required)
    }
    
    0 讨论(0)
  • 2021-01-18 15:37

    Not possible, at least not directly. IHttpHandler objects are instantiated by the ASP.NET runtime and it doesn't allow Windsor to get involved in its creation. You can either:

    • Pull dependencies, by using the container as a service locator.
    • Set up a base handler that creates, injects and delegates to your own handlers (see how Spring does it)
    • Use the container as a service locator for another service that handles the entire request (as saret explained)
    0 讨论(0)
提交回复
热议问题