15

Closed

Web API / Enable Dependency Injection for HttpMessageHandler types.

description

It would be good if I could pass a type of HttpMessageHandler instead of an instance. That way, the DependencyResolver could automatically resolve the type including any of it's dependencies.

Right now, in order to supply dependencies I have to reference the container itself, or pass a delegate (which will call into the container). However, AFAICT the HttpMessageHandler types are initialized once, in application start so it looks like I will have to manually control the lifetime of the dependencies.

Since the handler is initialized once, if I pass a singleton for the dependency that means it won't get GC'ed at all. On the other hand, I can manually resolve an instance and then release it but I am not happy with this approach as it couples the handler with lifetime management of it's dependencies.
Closed Mar 5, 2013 at 12:35 AM by HongmeiG
Given the message handler is passed as instance and being initialized once for the entire service. I think it is easier to have user code inject the dependency themselves first and register the instance with http configuration. This allow people to write custom message handler that has constructor but do not need dependency injection as well. The current model is more flexible.

comments

HongmeiG wrote Apr 25, 2012 at 12:36 AM

This is a great feature. However, we are trying to lock down our current release. We can revisit this feature in our future release.

baxevanis wrote Apr 25, 2012 at 1:47 AM

OK!

andrewwebb wrote May 16, 2012 at 7:49 AM

Very good suggestion. I really need this one. Bit of an oversight that this wasn't done in the first place.

I had assumed that I would get a new instance of my delegating handler each time (with correct DI), but the fact that this wasn't happening caused a nasty and hard-to-find bug.

I've got a workaround in place for now (service location within my single-instance delegating handler using the Service Resolver), but it's not as elegant as the proper DI-based solution.

Looking forward to this oversight being corrected.

tugberk wrote Aug 16, 2012 at 9:15 AM

This is one of the feature I wish it would be there.

eatdoku wrote Nov 2, 2012 at 8:03 AM

tool me a while to find this issue, delegatinghandler only gets initialized once. Currently i am constructing my dependencies manually.

tugberk wrote Nov 2, 2012 at 9:13 AM

@eatdoku yes. I initially voted for this feature but now I am not sure whether it will be better to construct a brand-new instance of an HttpMessageHandler for every single request.

I mean we can get access to DependencyScope inside an HttpMessageHandler. This is not a blocking issue.

eatdoku wrote Nov 20, 2012 at 10:24 PM

yeah, i am using dependencyscope in httpmessagehandler as well, it's definitly not a blocking issue for me nethier.

but it'll be alot nicer to have it injected from the constructor, much easier to maintain the code and not have a lot of webapi specific code.

abatishchev wrote Oct 20 at 7:16 PM

Any update on this very demanded feature?