C# Ninject WithConstructorArgument :没有匹配的绑定可用,并且类型不可自绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13466406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 08:40:36  来源:igfitidea点击:

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

c#exception-handlingdependency-injectionninjectninject-2

提问by Jean-Fran?ois Beauchamp

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

我对 WithConstructorArgument 的理解可能是错误的,因为以下不起作用:

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:

我有一个服务,我们称之为 MyService,它的构造函数接受多个对象,以及一个名为 testEmail 的字符串参数。对于这个字符串参数,我添加了以下 Ninject 绑定:

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.

激活字符串时出错 没有可用的匹配绑定,并且类型不可自绑定。激活路径:
2) 将依赖字符串注入 MyService 类型构造函数的参数 testEmail
1) 请求 MyService

建议:
1) 确保您已经定义了字符串的绑定。
2) 如果绑定是在模块中定义的,请确保该模块已加载到内核中。
3) 确保您没有意外创建多个内核。
4) 如果您使用构造函数参数,请确保参数名称与构造函数参数名称匹配。
5) 如果您使用自动模块加载,请确保搜索路径和过滤器正确。

What am I doing wrong here?

我在这里做错了什么?

UPDATE:

更新

Here is the MyService constructor:

这是 MyService 构造函数:

[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:

我有所有构造函数参数类型的标准绑定。只有 'string' 没有绑定,而 HttpContextBase 有一个有点不同的绑定:

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

and MyHttpRequest is defined as follows:

和 MyHttpRequest 定义如下:

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;
    }
}

采纳答案by nemesv

With the statement:

声明如下:

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

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

您正在尝试解析MyService,因为该MyService类型未在您的内核中注册,Ninject 会将其视为自绑定类型。

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

所以它不会使用你的WithConstructorArgument来解决,"testEmail"因为它只会用于Bind<IMyService>()你得到异常的原因。

So if you have registered your MyServicewith:

因此,如果您已注册MyService

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

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

那么你应该通过注册的接口(IMyService)来解决它:

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

回答by jrap

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.

虽然 nemesv 有正确的响应,但我遇到了同样的错误,我的解决方案是我的 /bin 中有一个流氓 DLL。我重构并删除/移动了一些仍然存在于我的旧 DLL 中的类。解决方案 - 删除旧的 DLL。