0

when i was trying to resolve the IUIVisualizerService a Ninject.ActivationException came up in Ninject.dll.

Can someone help me pls?

This code section calls the problem:

NinjectDependencyResolver resolver = new NinjectDependencyResolver();
            var item = resolver.GetService<IUIVisualizerService>();

Code for Resolver:

using System;
    using System.Collections.Generic;
    using Catel.Services;
    using Ninject;
    using NLog;

    namespace MS_Modell.Infrastructure
    {
        internal class NinjectDependencyResolver
        {
            private IKernel kernel;
            private Logger log = LogManager.GetCurrentClassLogger();

            public NinjectDependencyResolver()
            {
                try
                {
                    kernel = new StandardKernel();
                    kernel.Bind<IUIVisualizerService>().To<UIVisualizerService>();
                }
                catch (Exception ex)
                {
                    log.Fatal("NinjectDependencyResolver(): " + ex);
                    throw;
                }
            }

            public T GetService<T>()
            {
                try
                {
                    return kernel.TryGet<T>();
                }
                catch (Exception ex)
                {
                    log.Fatal("GetService<T>(): " + ex.Message);
                    throw;
                }
            }       
        }
    }

Edit: I got a null object after GetService is called. But the exception wasnt raised. Only a message on the console output of Visual Studio can be seen:

An expcetion (first chance) of type "Ninject.ActivationException" was thrown in Ninject.dll.

Edit 2:

Thx guys for the fast answers. Here is the concrete solution for someone, who runs into the same problem:

In NinjectResolver you need to add this code:

    kernel.Bind<IViewLocator>().To<ViewLocator>();
    kernel.Bind<IUIVisualizerService>().To<UIVisualizerService>().WithConstructorArgument("ViewLocator", GetService<IViewLocator>());

Resolving the IUIVisualizerService:

TargetSelectorViewModel selector = new TargetSelectorViewModel();
                var item = resolver.GetService<IUIVisualizerService>();
                item.Register(typeof(TargetSelectorViewModel), typeof(TargetSelector));
                item.ShowDialog(selector);
Benjamin Martin
  • 576
  • 1
  • 8
  • 27
  • Please provide the message and stacktrace of the `ActivationException`. – BatteryBackupUnit Mar 06 '14 at 11:19
  • Hi, this exception was not thrown by the Ninject itself but i saw this message on the output of the console in vs: an exception (first chance) of type "Ninject.ActivationException" was thrown in Ninject.dll. "WpfApplication5.vshost.exe" (Verwaltet (v4.0.30319)): "C:\windows\Microsoft.Net\assembly\GAC_MSIL\PresentationFramework.Aero\v4.0_4.0.0.0__31bf3856ad364e35\PresentationFramework.Aero.dll" geladen – Benjamin Martin Mar 06 '14 at 11:50
  • See @GeertvanHorrik 's answer, that's what the content of the `ActivationException` would tell you. So you just have to get a hold of the exception and it's message. Print it to the log, debug it,.. what ever. – BatteryBackupUnit Mar 06 '14 at 12:08

1 Answers1

0

The UIVisualizerService requires dependency injection of the IViewLocator. Make sure that Ninject can resolve that as well.

https://github.com/Catel/Catel/blob/develop/src/Catel.MVVM/Catel.MVVM.NET40/Services/UIVisualizerService.cs#L54

Geert van Horrik
  • 5,689
  • 1
  • 18
  • 32