0

I have a factory method called MessageHandlerFactory

public class MessageHandlerFactory : IMessageHandlerFactory<IMessage>
{
    public IMessageHandler<IMessage> GetMessageHandler()
    {
        return new MessageHandler_1();
    }
}    

... and the following message Handler

public class MessageHandler_1 : IMessageHandler<Message>
{
    //Do Something…
}

public class Message : IMessage
{
    public String Name {get; set;}
}

MessageHandlerFactory needs to be assigned to all occurrences of IMessageHandlerFactory in my project so that calls to IMessageHandlerFactory.GetMessageHandler() will return an instance of MessageHandler_1. (In my actual code, I have multiple implementations of IMessageHandler and IMessageHandlerFactory.GetMessageHandler would return an appropriate implementation based on some conditions...). Here is how I do my Ninject binding

Bind<IMessageHandlerFactory<IMessage>>().To<MessageHandlerFactory>();
Bind<IMessageHandler<Message>>().To<MessageHandler_1>();

But when I try accessing the MessageHandlerFactory.GetMessageHandler, I get the following exception

Ninject.ActivationException: Error activating IMessageHandler{IMessage} No matching bindings are available, and the type is not self-bindable. Activation path: 1) Request for IMessageHandler{IMessage}

Wondering how I could fix it? I have been playing with this for quite sometime without any success. Any help would be greatly appreciated!!

infinity
  • 1,900
  • 4
  • 29
  • 48
  • your actual implementation must be different from what you provided here, since the code you provided here does not, *ever*, resolve `IMessageHandler`. There is no `IResolutionRoot.Get<..>`. So please provide your *actual* code so we may help. You might also want to look into ninject.extensions.factory - it eliminates the need for implementing factories which do nothing more than `return IResolutionRoot.Get<...>()` – BatteryBackupUnit May 04 '14 at 08:44

1 Answers1

0

Your exception message implies that you are attempting to resolve an "IMessageHandler< IMessage >", and there is no matching binding. The closest you have is a binding for "IMessageHandler< Message >" (note the missing "I"). I'm guessing that's where you're running into a problem.

Mike Manard
  • 1,020
  • 9
  • 13