Ninject ActivationException: IAlertManagement activation error

I get the following error:

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. 

Here is an example that throws this exception:

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

The indicated error occurs in one of my unit tests on our build server, but not on my development machine. I have 7 other tests, almost identical to this, that pass on the build server and on my development machine, but this is the only test that fails.

The IAlertManagement interface comes from a dll named Core , and the specific type comes from another dll called AlertManagement . I have both a Core dll and AlertManagement included in my unit test project as project references. I have 7 or 8 other tests identical to this situation, but this is the only mistake.

Any ideas would be appreciated.

+8
c # ninject tfsbuild ninject-extensions
source share
3 answers

I decided to solve this by adding specific references to resolving types in my unit testing project. Adding project links is not enough. Here is how I do it:

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

The error arises because IAlertManagement not tied to any particular class. Try manually binding 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
source share
  • The first thing I would like to check is to make sure that the DLL containing the IAlertManagement implementation is copied to the appropriate directory on the build server so that the tests can see it.

    / li>
  • Another thing worth trying is to move the kernel boot code to the ClassInitialize function instead of TestInitialize to see if there is any race condition or other related condition. I saw random errors on the build servers due to the arrangement of objects in a different order or earlier than usual (including Rx, TPL, and inconspicuous exceptions in my case). It may be that more tests run in parallel on the build server than on the desktop.

  • Or it may be partially due to the fact that the server can use the GC Server . I don't know if using the GC workstation would help or not, but it might be worth a try.

0
source share

All Articles