我收到以下错误:

Test method: BootStrapperTest.Can_Create_Alert_Management_Object threw exception:  Ninject.ActivationException:
Error activating IAlertManagement No matching bindings are available, and the type is not self-bindable.

Activation path:
1) Request for IAlertManagement

Suggestions:
1) Ensure that you have defined a binding for IAlertManagement.
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.

这是导致此异常的测试用例:
[TestInitialize]
public void Initialize()
{
    BootStrapper.RegisterTypes();
}

[TestMethod]
public void Can_Create_Alert_Management_Object()
{
    IAlertManagement alertManagementService = BootStrapper.Kernel.Get<IAlertManagement>();

    Assert.IsNotNull(alertManagementService);
}

//This is the code that gets called in [TestInitialize]
public static void RegisterTypes()
{
    if (!initialized)
    {
        Kernel.Bind(scanner => scanner.FromAssembliesMatching("MyCompany.MyProduct.*")
                                   .SelectAllClasses()
                                   .BindDefaultInterface());

        Kernel.Unbind(typeof(IWcfServiceClient<>));
        Kernel.Bind(typeof(IWcfServiceClient<>)).ToMethod(ctx =>
                    (ctx.Kernel.Get(typeof(WcfServiceClientProvider<>).MakeGenericType(ctx.GenericArguments)) as IProvider).Create(ctx));
    }

    initialized = true;
}

在我们的构建服务器上的单元测试之一中发生了上述错误,但是在我的开发机器上却没有发生。我还有7个其他测试几乎与此测试相同,即在构建服务器和我的开发机上通过,但这是唯一失败的测试。

IAlertManagement接口(interface)来自一个名为的dll核心,而具体类型来自另一个名为 AlertManagement 的dll。我在单元测试项目中同时包含了 Core dll和 AlertManagement dll作为项目引用。我有7或8个其他测试与此情况相同,但这是唯一失败的测试。

任何想法都将是感激的。

最佳答案

发生此错误是因为IAlertManagement没有与任何具体的类绑定(bind)。
尝试手动绑定(bind)IAlertManagement

public static void RegisterTypes()
{
    if (!initialized)
    {
        Kernel.Bind(scanner => scanner.FromAssembliesMatching("MyCompany.MyProduct.*")
                               .SelectAllClasses()
                               .BindDefaultInterface());


        //Try to add following line
        Kernel.Bind<IAlertManagement>().To<ConcreteImplementationOfIAlertManagement>();

        Kernel.Unbind(typeof(IWcfServiceClient<>));
        Kernel.Bind(typeof(IWcfServiceClient<>)).ToMethod(ctx => (ctx.Kernel.Get(typeof(WcfServiceClientProvider<>).MakeGenericType(ctx.GenericArguments)) as IProvider).Create(ctx));
    }

    initialized = true;
}

关于c# - Ninject ActivationException : Error activating IAlertManagement,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11864279/

10-12 12:37