Ninject WithConstructorArgument:没有匹配的绑定是可用的,并且类型不能自行绑定 [英] Ninject WithConstructorArgument : No matching bindings are available, and the type is not self-bindable

查看:1569
本文介绍了Ninject WithConstructorArgument:没有匹配的绑定是可用的,并且类型不能自行绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我WithConstructorArgument的认识可能是错误的,因为以下不工作:



我有一个服务,让我们叫它为MyService,它的构造正在多个对象,和一个字符串参数调用testemail子。对于此字符串参数,添加以下Ninject绑定:

 字符串testemail子=test@example.com; 
kernel.Bind&所述; IMyService方式>()到<为MyService>()WithConstructorArgument(testemail子,testemail子)。



但是,在执行下面的代码行的时候,我得到一个异常:

  VAR为myService = kernel.Get<&为MyService GT;(); 

下面是例外,我得到:



<块引用>

错误激活字符串没有匹配的绑定是可用的,并且
型不是自我绑定。激活路径:结果
2)
依赖字符串注入式
为MyService结果
1)申请的MyService

$ B的构造函数参数testemail子
$ b

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




我在做什么错在这里?



更新



下面是为MyService构造:

  [Ninject.Inject] 
公共则将MyService(IMyRepository myRepository,IMyEventService myEventService,
IUnitOfWork的UnitOfWork,ILoggingService日志,
IEmailService emailService,IConfigurationManager配置,
HttpContextBase的HttpContext,串testemail子)
{
this.myRepository = myRepository;
this.myEventService = myEventService;
this.unitOfWork =的UnitOfWork;
this.log =登录;
this.emailService = emailService;
this.config =配置;
this.httpContext = HttpContext的;
this.testEmail = testemail子;
}



我有标准绑定的所有构造函数参数类型。只有'串'有没有约束力,HttpContextBase具有约束力的就是有点不同:

  kernel.Bind< HttpContextBase>( ).ToMethod(上下文=>新建HttpContextWrapper(新的HttpContext(新MyHttpRequest(,,,空,新的StringWriter())))); 

和MyHttpRequest定义如下:

 公共类MyHttpRequest:SimpleWorkerRequest的
{
公共字符串UserHostAddress;
公共字符串RawUrl;

公共MyHttpRequest(字符串appVirtualDir,串appPhysicalDir,字符串页面,查询字符串,TextWriter的输出)
:基地(appVirtualDir,appPhysicalDir,网页,查询,输出)
{
this.UserHostAddress =127.0.0.1;
this.RawUrl = NULL;
}
}


解决方案

通过声明:

  VAR为myService = kernel.Get<&为MyService GT;(); 

您正在尝试的决心为MyService 键,因为在为MyService 类型没有在内核中Ninject就会把它当作一个自我约束类型注册。



所以不会用你的 WithConstructorArgument 来解决testemail子,因为它会与绑定<的前提下使用; IMyService方式>()这就是为什么你得到异常



因此,如果您已经注册为MyService

 字符串testemail子=test@example.com; 
kernel.Bind&所述; IMyService方式>()到<为MyService>()
.WithConstructorArgument(testemail子,testemail子);



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

  VAR为myService = kernel.Get< IMyService>(); 


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 = "test@example.com";
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;
    }
}

解决方案

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>();

这篇关于Ninject WithConstructorArgument:没有匹配的绑定是可用的,并且类型不能自行绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆