c# - ASP.NET MVC 和 Ninject 2.0 绑定(bind)问题

标签 c# asp.net asp.net-mvc ninject ninject.web.mvc

我有一个名为 MyMailSender 的类,它绑定(bind)到名为 EmailController 的 asp.net Controller 。

例如

public EmailController(IMailSender sender)
    {
        //MyMailSender
        this.sender = sender;
    }

这个MyMailSender类还依赖于一个名为MessageSender的类。

现在,我的流程通过执行以下操作无需注入(inject)即可运行。

public class MyMailSender : IMailSender
{
    private IMessageSender messageSender;

    public MyMailSender()
    {
        messageSender = new SmtpMessageSender("mail.address.com", 25);
    }
}

如何使用 Ninject 将此依赖类绑定(bind)到 MyMailSender 类?

MyMailSender 初始绑定(bind)是通过 Controller 工厂绑定(bind)完成的

public override void Load()
        {
                 Bind<IMailSender>()
                .To<MyMailSender>();            
        }

我尝试绑定(bind)到 Controller 工厂,例如:

  Bind<IMessageSender>()
       .To<SmtpMessageSender>().WithConstructorArgument("mail.address.com", 25);

但我怀疑我不应该在此处绑定(bind)这种类型的绑定(bind)。我有什么选择?

我的错误消息是:

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.

最佳答案

这看起来不错。 WithConstructorArgument 适用于这些类型的初始化(只要这些参数在 aaplication/thread 的生命周期内不改变)。

我会像这样更改 MyMailSender 以使用构造函数注入(inject):

public class MyMailSender : IMailSender
{
    private IMessageSender _messageSender;

    public MyMailSender(IMessageSender messageSender)
    {
        _messageSender = messageSender;
    }
}

然后在你的onLoad中:

public override void Load()
{                     
  Bind<IMessageSender>()
       .To<SmtpMessageSender>()
       .WithConstructorArgument("hostname", "mail.address.com")
       .WithConstructorArgument("port", 25);
  Bind<IMailSender>()
       .To<MyMailSender>();     
}

作为一般提示,每当您在类里面看到东西时,请将其视为一种气味。使用任何 DI 框架时,new 应该很少出现在您的代码中(例如在工厂等中)

另一个建议是从某些配置/设置文件加载地址和端口,而不是硬编码。

关于c# - ASP.NET MVC 和 Ninject 2.0 绑定(bind)问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6797871/

相关文章:

c# - 如何更改 MVC 4 Web 应用程序中的默认 View

c# - 运行托管代码的非托管线程

c# - 什么时候应该使用 Moq 的 .As 方法?

asp.net - 浏览到 .../trace.axd 显示服务器信息 - PCI show stopper

c# - cshtml C#已经存在一个与此连接相关联的打开的DataReader,必须先关闭它

C# 在代码中从复制的 SQL 值创建字节数组?

asp.net - 我可以使用 Windows 窗体 GUI 创建 ASP.NET Core Web 服务吗?

c# - 将证书导入到 Azure key 保管库 : Operation returned an invalid status code 'Conflict'

c# - 无法在 MVC 应用程序中获取项目目录路径

asp.net-mvc - ASP.NET MVC 安全模型/数据库 - 我有哪些选择?