2
我有一類稱爲MyMailSender結合稱爲EmailController的asp.net控制器。ASP.NET MVC和Ninject 2.0綁定問題
E.g.
public EmailController(IMailSender sender)
{
//MyMailSender
this.sender = sender;
}
這MyMailSender
類也依賴於一個名爲MessageSender
類。
現在我的過程中通過執行以下作品,未經注射。
public class MyMailSender : IMailSender
{
private IMessageSender messageSender;
public MyMailSender()
{
messageSender = new SmtpMessageSender("mail.address.com", 25);
}
}
如何使用Ninject將此依賴類綁定到MyMailSender
類?
MyMailSender
最初的結合是通過控制器工廠結合
public override void Load()
{
Bind<IMailSender>()
.To<MyMailSender>();
}
我試圖綁定到控制器工廠像做:
Bind<IMessageSender>()
.To<SmtpMessageSender>().WithConstructorArgument("mail.address.com", 25);
,但我懷疑,我不應該綁定在這裏對這種類型的結合。我有什麼選擇?
我的錯誤信息是:
Error activating string
No matching bindings are available, and the type is not self-bindable.
Activation path:
4) Injection of dependency string into parameter hostname of constructor of type SmtpMessageSender
3) Injection of dependency IMessageSender into parameter messageSender of constructor of type MyMailSender
2) Injection of dependency IMailSender into parameter sender of constructor of type EmailController
1) Request for EmailController
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.
我試圖MyMailSender使用構造器注入從乞討,並得到一個錯誤--->錯誤激活串 沒有匹配的綁定是可用的,並且類型不是自可綁定。 激活路徑:依賴字符串的 4)注射入式SmtpMessageSender – Keith
對不起的構造函數的參數主機名,看我的更新。您需要指定帕拉姆名稱和值使用'WithConstructorArgument()'時。我假設了param的名字,但你明白了。 – Mrchief
謝謝!那樣做了! – Keith