Car*_*las 3 windows-services dependency-injection ninject akka.net
我正在尝试将 Windows 服务的一部分迁移到 AKKA.net 参与者模型中,但是当涉及到参与者的 DI(他们有一些依赖项,例如数据访问层等)时,我遇到了一些问题,因为我不完全了解如何在服务中连接 DependencyResolver。如果那是一个 Web 应用程序,那么它将是 HttpConfiguraiton 的 DependencyResolver 但是在这种情况下,我目前拥有标准内核来进行引导并获取顶级接口实现以启动 Windows 服务。
我会有两个问题:
我一直在这里阅读:http : //getakka.net/docs/Dependency%20injection#ninject
提前致谢!
我在 Windows 服务中使用 Akka.NET(使用 topshelf 和 autofac,但这种方法应该适用于任何服务运行器和 IoC 框架)。我在服务的启动方法中启动顶级actor,如下所示:
_scope.Resolve<ActorSystem>().ActorOf<MyTopLevelActor>().Tell(new StartMySystemMessage());
Run Code Online (Sandbox Code Playgroud)
从那里我创建儿童演员
var actorWithoutDependencies = Context.ActorOf<ChildActorType>();
Run Code Online (Sandbox Code Playgroud)
其中 actor 类型具有默认构造函数,或
var actorWithDependencies = Context.ActorOfDI<ChildActorType>();
Run Code Online (Sandbox Code Playgroud)
子actor类型具有依赖关系。
ActorOfDI 调用是我用来包装 akka.net 中的 ActorSystem.DI() 和 IActorContext.DI() 方法的扩展方法,如下所示:
public static IActorRef ActorOfDI<T>(this ActorSystem actorSystem, string name = null) where T : ActorBase
{
return actorSystem.ActorOf(actorSystem.DI().Props<T>(), name);
}
public static IActorRef ActorOfDI<T>(this IActorContext actorContext, string name = null) where T : ActorBase
{
return actorContext.ActorOf(actorContext.DI().Props<T>(), name);
}
Run Code Online (Sandbox Code Playgroud)
就演员的 IoC 配置而言,我正在包含演员的程序集中注册所有演员类型(使用 Autofac),如下所示:
containerBuilder.RegisterAssemblyTypes(typeof(SomeActorType).Assembly).Where(x => x.Name.EndsWith("Actor"));
Run Code Online (Sandbox Code Playgroud)
并注册演员系统本身我这样做:
containerBuilder.Register(c =>
{
var system = ActorSystem.Create("MyActorSystem");
// ReSharper disable once ObjectCreationAsStatement
new AutoFacDependencyResolver(lazyContainer.Value, system);
return system;
}).As<ActorSystem>().SingleInstance();
Run Code Online (Sandbox Code Playgroud)
其中 lazyContainer 是:
Lazy<IContainer> // this is an autofac type
Run Code Online (Sandbox Code Playgroud)
和构造函数委托调用
containerBuilder.Build() // this is an autofac call
Run Code Online (Sandbox Code Playgroud)
要从另一个依赖注入的类中获取actor系统,您可以将一个 ActorSystem 传递给该类的构造函数。我不确定是否在我的应用程序代码中获得对系统参与者的引用 - 我自己不需要这样做。
| 归档时间: |
|
| 查看次数: |
1100 次 |
| 最近记录: |