【发布时间】:2017-01-31 13:22:43
【问题描述】:
当演员服务启动时,我想以described in the documentation 自动订阅任何事件。手动订阅事件有效。但是,当服务被实例化时,是否有办法自动订阅参与者服务,例如在 OnActivateAsync() 中?
我试图做的是通过依赖注入来解决这个问题,即在 MyActor 类的实例化时,它传入一个接口,OnActivateAsync 调用该接口为客户端订阅事件。但是我遇到了依赖注入的问题。
使用 Microsoft.ServiceFabric.Actors.2.2.207 应该支持对参与者服务的依赖注入。现在,在实现 Microsoft.ServiceFabric.Actors.Runtime.Actor 时,会使用 ActorService 和 ActorId 参数创建一个默认构造函数。
我想添加我自己的构造函数,它有一个额外的接口被传入。你如何编写actor服务的注册以添加依赖项?在默认的 Program.cs Main 中它提供了这个
IMyInjectedInterface myInjectedInterface = null;
// inject interface instance to the UserActor
ActorRuntime.RegisterActorAsync<MyActor>(
(context, actorType) => new ActorService(context, actorType, () => new MyActor(myInjectedInterface))).GetAwaiter().GetResult();
但是在显示“() => new MyActor(myInjectedInterface)”的那一行它告诉我一个错误
委托 'Func' 不取 0 论据
查看 Actor 类的构造函数,它有以下内容
MyActor.Cs
internal class MyActor : Microsoft.ServiceFabric.Actors.Runtime.Actor, IMyActor
{
private ActorService _actorService;
private ActorId _actorId;
private IMyInjectedInterface _myInjectedInterface;
public SystemUserActor(IMyInjectedInterface myInjectedInterface, ActorService actorService = null, ActorId actorId = null) : base(actorService, actorId)
{
_actorService = actorService;
_actorId = actorId;
_myInjectedInterface = myInjectedInterface;
}
}
1) 如何解决我在尝试解决 Actor 依赖项时收到的错误?
委托 'Func' 不取 0 论据
额外问题:
当我的无状态服务(调用客户端)调用接口实例时,如何解析 IMyInjectedInterface 以注入到参与者服务中?
【问题讨论】:
标签: c# dependency-injection azure-service-fabric service-fabric-actor