Ninject WithConstructorArgument : No matching bindings are available, and the type is not self-bindable

Jean-François Beauchamp picture Jean-François Beauchamp · Nov 20, 2012 · Viewed 43.9k times · Source

My understanding of WithConstructorArgument is probably erroneous, because the following is not working:

I have a service, lets call it MyService, whose constructor is taking multiple objects, and a string parameter called testEmail. For this string parameter, I added the following Ninject binding:

string testEmail = "[email protected]";
kernel.Bind<IMyService>().To<MyService>().WithConstructorArgument("testEmail", testEmail);

However, when executing the following line of code, I get an exception:

var myService = kernel.Get<MyService>();

Here is the exception I get:

Error activating string No matching bindings are available, and the type is not self-bindable. Activation path:
2) Injection of dependency string into parameter testEmail of constructor of type MyService
1) Request for MyService

Suggestions:
1) Ensure that you have defined a binding for string.
2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
3) Ensure you have not accidentally created more than one kernel.
4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
5) If you are using automatic module loading, ensure the search path and filters are correct.

What am I doing wrong here?

UPDATE:

Here is the MyService constructor:

[Ninject.Inject]
public MyService(IMyRepository myRepository, IMyEventService myEventService, 
                 IUnitOfWork unitOfWork, ILoggingService log,
         IEmailService emailService, IConfigurationManager config,
         HttpContextBase httpContext, string testEmail)
{
    this.myRepository = myRepository;
    this.myEventService = myEventService;
    this.unitOfWork = unitOfWork;
    this.log = log;
    this.emailService = emailService;
    this.config = config;
    this.httpContext = httpContext;
    this.testEmail = testEmail;
}

I have standard bindings for all the constructor parameter types. Only 'string' has no binding, and HttpContextBase has a binding that is a bit different:

kernel.Bind<HttpContextBase>().ToMethod(context => new HttpContextWrapper(new HttpContext(new MyHttpRequest("", "", "", null, new StringWriter()))));

and MyHttpRequest is defined as follows:

public class MyHttpRequest : SimpleWorkerRequest
{
    public string UserHostAddress;
    public string RawUrl;

    public MyHttpRequest(string appVirtualDir, string appPhysicalDir, string page, string query, TextWriter output)
    : base(appVirtualDir, appPhysicalDir, page, query, output)
    {
        this.UserHostAddress = "127.0.0.1";
        this.RawUrl = null;
    }
}

Answer

nemesv picture nemesv · Nov 20, 2012

With the statement:

var myService = kernel.Get<MyService>();

You are trying the resolve MyService and because the MyService type is not registered in your kernel Ninject will treat it as a self bound type.

So it won't use your WithConstructorArgument to resolve the "testEmail" because it will be only used with Bind<IMyService>() that is why you're getting the exception.

So if you have registered your MyService with:

string testEmail = "[email protected]";
kernel.Bind<IMyService>().To<MyService>()
      .WithConstructorArgument("testEmail", testEmail);

Then you should resolve it through the registered interface (IMyService):

var myService = kernel.Get<IMyService>();