gpt4 book ai didi

c# - Ninject WithConstructorArgument : No matching bindings are available, 并且该类型不可自绑定(bind)

转载 作者:IT王子 更新时间:2023-10-29 04:42:04 25 4
gpt4 key购买 nike

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

我有一个服务,我们称它为 MyService,其构造函数采用多个对象和一个名为 testEmail 的字符串参数。对于这个字符串参数,我添加了以下 Ninject 绑定(bind):

string testEmail = "test@example.com";
kernel.Bind<IMyService>().To<MyService>().WithConstructorArgument("testEmail", testEmail);

但是,当执行下面这行代码时,我得到了一个异常:

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

这是我得到的异常:

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.

我在这里做错了什么?

更新:

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

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

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

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

最佳答案

声明:

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

您正在尝试解决 MyService因为 MyService类型未在您的内核中注册 Ninject 会将其视为自绑定(bind)类型。

所以它不会使用你的 WithConstructorArgument解决 "testEmail"因为它只会与 Bind<IMyService>() 一起使用这就是你得到异常(exception)的原因。

因此,如果您已经注册了您的 MyService与:

string testEmail = "test@example.com";
kernel.Bind<IMyService>().To<MyService>()
.WithConstructorArgument("testEmail", testEmail);

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

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

关于c# - Ninject WithConstructorArgument : No matching bindings are available, 并且该类型不可自绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13466406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com