繁体   English   中英

Ninject ActivationException:激活IAlertManagement时出错

[英]Ninject ActivationException: Error activating IAlertManagement

我收到以下错误:

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。 我的单元测试项目中包含Core dll和AlertManagement dll作为项目引用。 我有7或8个其他测试与此情况相同,但这是唯一失败的测试。

任何想法都将是感激的。

发生错误是因为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. 我要检查的第一件事是确保将包含IAlertManagement的实现的DLL复制到构建服务器上的正确目录中,以便测试可以看到它。

  2. 要尝试的另一件事是将内核加载代码移至ClassInitialize函数而不是TestInitialize ,只是看是否存在某种竞争条件或其他相关事物。 我已经看到由于对象以不同于通常发生的顺序或更快的顺序放置而导致的构建服务器上的随机错误(在我的情况下涉及Rx,TPL和未观察到的异常)。 在构建服务器上并行运行的测试可能要比在台式机上运行的测试更多。

  3. 或部分原因是服务器可能正在使用Server GC 我不知道是否强迫它使用Workstation GC是否会有所帮助,但可能值得尝试。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM