小编Jus*_*eff的帖子

Service Fabric Actor 服务依赖项注入和 Actor 事件

当参与者服务启动时,我想自动订阅文档中描述的任何事件。手动订阅事件是有效的。但是,当实例化服务时,是否有一种方法可以自动订阅参与者服务,就像在 OnActivateAsync() 中一样?

我试图做的是通过依赖注入解决这个问题,在实例化 MyActor 类时,它传入一个接口,OnActivateAsync 调用该接口来为客户端订阅事件。但是我在依赖注入方面遇到了问题。

使用 Microsoft.ServiceFabric.Actors.2.2.207 应该支持对 Actor 服务的依赖项注入。现在,在实现 Microsoft.ServiceFabric.Actors.Runtime.Actor 时,会创建一个带有 ActorService 和 ActorId 参数的默认构造函数。

我想添加我自己的构造函数,该构造函数具有传入的附加接口。如何编写参与者服务的注册以添加依赖项?在默认的 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();
Run Code Online (Sandbox Code Playgroud)

但是,在显示“() => 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 …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection azure-service-fabric service-fabric-actor

4
推荐指数
1
解决办法
2678
查看次数