Dependency resolvers? Hooking into IOC

Topics: Web Api
Sep 7, 2011 at 2:22 AM
Edited Sep 7, 2011 at 2:57 AM

Hi there,

I am using StructureMap as IOC container.
I would like my MessageHandlers and ErrorHandlers to be able to solve constructor dependencies automatically. These classes are instantiated by the web api framework. Is there a way I can register my own dependency resolver? (Similar as to what I can do in MVC:  IDependencyResolver



For example in the code below I want ILogger and IRequestAuthorizerService to be resolved.
Hope it's possible!
    public class AuthenticationMessageHandler : DelegatingChannel
        // services
        ILogger _logger;
        IRequestAuthorizerService _requestAuthorizer;

        public AuthenticationMessageHandler(HttpMessageChannel innerChannel, ILogger logger, IRequestAuthorizerService requestAuthorizer)
            : base(innerChannel)

*** EDIT ***
Oh no, I ran into IResourceFactory. When trying it out I get this YSOD. So it seems this is currently not possible...
Invalid type: 'AuthenticationMessageHandler'.
 It must inherit from base type 'DelegatingChannel', cannot be abstract, 
and must expose a public constructor taking a single argument of type 'HttpMessageChannel'.
Sep 7, 2011 at 4:11 AM
Edited Sep 7, 2011 at 4:12 AM

In Preview 5, you can do this as we provide a hook in HttpConfiguration to pass a lambda for creating error handlers and message handlers which you can wire to an IoC. If you grab our latest source (which is pre-Preview 5) you will see it is there. The new message handler doesn't require another handler to be passed in the ctor, so you can use the lambda to resolve from an IoC. If you check the new ContactManger_Advanced sample you will see I am using MEF to resolve the message handlers.  The custom error handler hook is not working correctly "yet" but we have a fix for it.

Sep 7, 2011 at 5:25 PM
Edited Sep 7, 2011 at 5:25 PM

Thanks for all the help!
Can you give some insight as to when Preview 5 will be available? Weeks, months?

Nov 11, 2011 at 2:04 AM

Hi Glenn, why would you subclass HttpConfiguration rather than WebApiConfiguration if you want to hook up an IoC container?