Dependency Injection for Handlers and Filters in ASP.NET Web API

后端 未结 2 815
温柔的废话
温柔的废话 2021-02-08 02:33

I am trying to wire up my Web Api project to use Castle Windsor for IoC

I have done that for my controllers by following this excellent article.

I am now trying

相关标签:
2条回答
  • 2021-02-08 02:51

    Since the MessageHandlers collection is global, it's effectively a list of singletons. This is fine when the handler itself has no state and no dependencies, but in a system that is based on the SOLID design principles, it's very likely that those handlers will have dependencies of their own and its very likely that some of those dependencies need a lifetime that is shorter than singleton.

    If that's the case, such message handler should not be created as singleton, since in general, a component should never have a lifetime that is longer than the lifetime of its dependencies.

    Web API however lacks any hooks that allow to resolve such handler on each request, but such mechanism is easily created by using a proxy class:

    public class DelegatingHandlerProxy<TDelegatingHandler> : DelegatingHandler
        where TDelegatingHandler : DelegatingHandler
    {
        private readonly WindsorContainer container;
    
        public DelegatingHandlerProxy(WindsorContainer container)
        {
            this.container = container;
        }
    
        protected override async Task<HttpResponseMessage> SendAsync(
            HttpRequestMessage request, CancellationToken cancellationToken)
        {
            // trigger the creation of the scope.
            request.GetDependencyScope();
    
            var handler = this.container.Resolve<TDelegatingHandler>();
    
            handler.InnerHandler = this.InnerHandler;
    
            var invoker = new HttpMessageInvoker(handler);
    
            var response = await invoker.SendAsync(request, cancellationToken);
    
            container.Release(handler);
    
            return response;
        }
    }
    

    This proxy can be used as follows:

    GlobalConfiguration.Configuration.MessageHandlers.Add(
        new DelegatingHandlerProxy<MyCustomHandler>(myContainer));
    

    The proxy is a singleton, but it will resolve the specified MyCustomHandler on each request.

    0 讨论(0)
  • 2021-02-08 03:05

    There seems to be no extension point as of today. There is a request for it though, at http://aspnetwebstack.codeplex.com/workitem/62

    0 讨论(0)
提交回复
热议问题