I am trying to use Ninject to resolve my dependency in my DelegatingHandler in asp.net web api.
I am registering the custom delegating handler in global.asax as follows:
GlobalConfiguration.Configuration.MessageHandlers.Add(
DependencyResolver.Current.GetService<AuthorizationHeaderHandler>());
I am wondering what’s the lifetime of MessageHandlers? does it even make sense to use IoC container to inject the dependencies in delegating handler?
Delegating handlers are initialized only once, in application start.
Think of them as in-process intermediaries rather than part of a single request/response lifecycle.
If you want this to be changed – there is a work item open to change this at Codeplex – http://aspnetwebstack.codeplex.com/workitem/62 you can vote for the feature and it might be brought into Web API vNext.
For the time being, the best bet to inject anything into the handler is manually resolving dependency, so using (unfortunately) Service Location.