This MSDN article describes how HTTP Message Handlers can effectively be used in ASP.NET Web API to 'decorate' requests. Furthermore, the article shows the following code to register your custom handlers into the Web API pipeline:
config.MessageHandlers.Add(new MessageHandler1());
The problem I have with this approach is that this registers the MessageHandler1
effectively as a singleton. 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.
So my question is, what alternative ways do we have to register custom message handlers, in such way that they can be resolved from our IoC container on each request?