簡體   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