Ninject ActivationException:激活IAlertManagement时出错

时间:2012-08-08 12:13:38

标签: c# ninject tfsbuild ninject-extensions

我收到以下错误:

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接口来自名为 Core 的dll,具体类型来自另一个名为 AlertManagement 的dll。我的单元测试项目中包含核心 dll和 AlertManagement dll作为项目引用。我有7或8个与这种情况相同的其他测试,但这是唯一一个失败的测试。

任何想法都会很有意义。

2 个答案:

答案 0 :(得分:1)

发生错误是因为IAlertManagement没有与任何具体类绑定。 尝试手动绑定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;
}

答案 1 :(得分:0)

我最后通过在单元测试项目中添加对解析类型的具体引用来解决这个问题。仅仅添加项目引用是不够的。这就是我这样做的方式:

[TestClass]
public class AssemblyInitialize
{
    /// <summary>
    /// Method which gets executed once per unit test session. The purpose of this method is to reference any loosely coupled
    /// assemblies so that they get included in the unit test session. If no code actually references a given assembly, even if its
    /// technically a project reference, it will not be copied to the test output folder.
    /// </summary>
    [AssemblyInitialize]
    public static void InitializeReferencedAssemblies(TestContext context)
    {
        List<Type> looselyCoupledTypes = new List<Type>
                                         {
                                             typeof(AlertManagement),
                                             typeof(Naming),
                                         };

        looselyCoupledTypes.ForEach(x => Console.WriteLine("Including loosely coupled assembly: {0}",
                                                           x.Assembly.FullName));
    }
}