如何在 Ninject 中设置依赖注入?
How to set up dependency injection in Ninject?
我正在尝试使用 Akka .NET 设置依赖注入。在关于该主题的 Pluralsight 课程之后,我想出了以下改编:
var container = new StandardKernel();
container.Bind<ITimeService>().To<LocalTimeService>();
container.Bind<TimeLordActor>().ToSelf();
using (var actorSystem = ActorSystem.Create("MyActorSystem"))
{
var resolver = new NinjectDependencyResolver(container, actorSystem);
var actor = actorSystem.ActorOf(Props.Create<TimeLordActor>(),
"TimeLordActor");
actor.Tell("Give me the time!");
Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();
}
TimeLordActor 的构造函数需要一个类型为 ITimeService
的参数。
但是,当 运行 时出现以下错误:
[ERROR][7/10/2016 5:39:42 PM][Thread 0012][akka://MyActorSystem/user/TimeLordActor] Error while creating actor instance of type AkkaNetDiExperimental.TimeLordActor with 0 args: ()
Cause: [akka://MyActorSystem/user/TimeLordActor#1586418697]: Akka.Actor.ActorInitializationException: Exception during creation ---> System.TypeLoadException: Error while creating actor instance of type AkkaNetDiExperimental.TimeLordActor with 0 args: () ---> System.MissingMethodException: Constructor on type 'AkkaNetDiExperimental.TimeLordActor' not found.
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at Akka.Actor.Props.ActivatorProducer.Produce()
at Akka.Actor.Props.NewActor()
--- End of inner exception stack trace ---
at Akka.Actor.Props.NewActor()
at Akka.Actor.ActorCell.CreateNewActorInstance()
at Akka.Actor.ActorCell.<>c__DisplayClass118_0.<NewActor>b__0()
at Akka.Actor.ActorCell.UseThreadContext(Action action)
at Akka.Actor.ActorCell.NewActor()
at Akka.Actor.ActorCell.Create(Exception failure)
--- End of inner exception stack trace ---
at Akka.Actor.ActorCell.Create(Exception failure)
at Akka.Actor.ActorCell.SysMsgInvokeAll(EarliestFirstSystemMessageList messages, Int32 currentState)
官方文档Akka .NET Dependency Injection建议在直接创建actor时,应该在ActorSystem实例上使用DI()扩展方法:
// Create the Props using the DI extension on your ActorSystem instance
var worker1Ref = system.ActorOf(system.DI().Props<TypedWorker>(), "Worker1");
var worker2Ref = system.ActorOf(system.DI().Props<TypedWorker>(), "Worker2");
但是我在actor系统上连这个扩展方法都找不到
有人可以解释一下如何使用 actor 系统本身的依赖注入创建一个简单的 actor 吗?
我想通了。您必须按照文档的建议使用 DI()
扩展方法。这是在 Akka.DI.Core
命名空间中。
using Akka.DI.Core;
然后记得更新 actor 创建以反映文档使用的方法:
var actor = actorSystem.ActorOf(actorSystem.DI().Props<TimeLordActor>(),
"TimeLordActor");
我正在尝试使用 Akka .NET 设置依赖注入。在关于该主题的 Pluralsight 课程之后,我想出了以下改编:
var container = new StandardKernel();
container.Bind<ITimeService>().To<LocalTimeService>();
container.Bind<TimeLordActor>().ToSelf();
using (var actorSystem = ActorSystem.Create("MyActorSystem"))
{
var resolver = new NinjectDependencyResolver(container, actorSystem);
var actor = actorSystem.ActorOf(Props.Create<TimeLordActor>(),
"TimeLordActor");
actor.Tell("Give me the time!");
Console.WriteLine("Press ENTER to exit...");
Console.ReadLine();
}
TimeLordActor 的构造函数需要一个类型为 ITimeService
的参数。
但是,当 运行 时出现以下错误:
[ERROR][7/10/2016 5:39:42 PM][Thread 0012][akka://MyActorSystem/user/TimeLordActor] Error while creating actor instance of type AkkaNetDiExperimental.TimeLordActor with 0 args: ()
Cause: [akka://MyActorSystem/user/TimeLordActor#1586418697]: Akka.Actor.ActorInitializationException: Exception during creation ---> System.TypeLoadException: Error while creating actor instance of type AkkaNetDiExperimental.TimeLordActor with 0 args: () ---> System.MissingMethodException: Constructor on type 'AkkaNetDiExperimental.TimeLordActor' not found.
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at Akka.Actor.Props.ActivatorProducer.Produce()
at Akka.Actor.Props.NewActor()
--- End of inner exception stack trace ---
at Akka.Actor.Props.NewActor()
at Akka.Actor.ActorCell.CreateNewActorInstance()
at Akka.Actor.ActorCell.<>c__DisplayClass118_0.<NewActor>b__0()
at Akka.Actor.ActorCell.UseThreadContext(Action action)
at Akka.Actor.ActorCell.NewActor()
at Akka.Actor.ActorCell.Create(Exception failure)
--- End of inner exception stack trace ---
at Akka.Actor.ActorCell.Create(Exception failure)
at Akka.Actor.ActorCell.SysMsgInvokeAll(EarliestFirstSystemMessageList messages, Int32 currentState)
官方文档Akka .NET Dependency Injection建议在直接创建actor时,应该在ActorSystem实例上使用DI()扩展方法:
// Create the Props using the DI extension on your ActorSystem instance
var worker1Ref = system.ActorOf(system.DI().Props<TypedWorker>(), "Worker1");
var worker2Ref = system.ActorOf(system.DI().Props<TypedWorker>(), "Worker2");
但是我在actor系统上连这个扩展方法都找不到
有人可以解释一下如何使用 actor 系统本身的依赖注入创建一个简单的 actor 吗?
我想通了。您必须按照文档的建议使用 DI()
扩展方法。这是在 Akka.DI.Core
命名空间中。
using Akka.DI.Core;
然后记得更新 actor 创建以反映文档使用的方法:
var actor = actorSystem.ActorOf(actorSystem.DI().Props<TimeLordActor>(),
"TimeLordActor");