[英]Catel/Ninject: Ninject.ActivationException when resolving IUIVisualizerService
when i was trying to resolve the IUIVisualizerService a Ninject.ActivationException came up in Ninject.dll. 当我尝试解析IUIVisualizerService时,Ninject.dll中出现Ninject.ActivationException。
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. 编辑:调用GetService后,我得到一个空对象。 But the exception wasnt raised.
但是没有提出例外。 Only a message on the console output of Visual Studio can be seen:
只能在Visual Studio的控制台输出上看到一条消息:
An expcetion (first chance) of type "Ninject.ActivationException" was thrown in Ninject.dll. Ninject.dll中引发了“ Ninject.ActivationException”类型的期望(第一次机会)。
Edit 2: 编辑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: 在NinjectResolver中,您需要添加以下代码:
kernel.Bind<IViewLocator>().To<ViewLocator>();
kernel.Bind<IUIVisualizerService>().To<UIVisualizerService>().WithConstructorArgument("ViewLocator", GetService<IViewLocator>());
Resolving the IUIVisualizerService: 解决IUIVisualizerService:
TargetSelectorViewModel selector = new TargetSelectorViewModel();
var item = resolver.GetService<IUIVisualizerService>();
item.Register(typeof(TargetSelectorViewModel), typeof(TargetSelector));
item.ShowDialog(selector);
The UIVisualizerService requires dependency injection of the IViewLocator. UIVisualizerService需要IViewLocator的依赖项注入。 Make sure that Ninject can resolve that as well.
确保Ninject也可以解决该问题。
https://github.com/Catel/Catel/blob/develop/src/Catel.MVVM/Catel.MVVM.NET40/Services/UIVisualizerService.cs#L54 https://github.com/Catel/Catel/blob/develop/src/Catel.MVVM/Catel.MVVM.NET40/Services/UIVisualizerService.cs#L54
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.