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

时光总嘲笑我的痴心妄想 提交于 2019-12-03 09:51:56

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 = "test@example.com";
kernel.Bind<IMyService>().To<MyService>()
      .WithConstructorArgument("testEmail", testEmail);

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

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

While nemesv has the correct response, I ran into the same error and the solution for me was a rogue DLL in my /bin. I had refactored and removed/moved some classes which were still present in my old DLL. Solution- remove the old DLL.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!