Service Fabric Actor 服务依赖注入和 Actor 事件
Service Fabric Actor Service Dependency Injection and Actor Events
当演员服务启动时,我想自动订阅任何事件 described in the documentation。手动订阅事件有效。然而,当服务被实例化时,有没有办法自动订阅参与者服务,就像在 OnActivateAsync() 中一样?
我试图做的是通过依赖注入解决这个问题,在 MyActor class 实例化时,它传入一个接口,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();
但是在它说“() => new MyActor(myInjectedInterface)”的那一行,它告诉我一个错误
Delegate 'Func' does not take 0
arguments
查看 Actor 上的构造函数 class 它具有以下内容
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 依赖项时收到的错误?
Delegate 'Func' does not take 0
arguments
奖金问题:
当我的无状态服务(调用客户端)调用时,如何解析要注入到 actor 服务中的接口实例的 IMyInjectedInterface?
IMyInjectedInterface myInjectedInterface = null;
//inject interface instance to the UserActor
ActorRuntime.RegisterActorAsync<MyActor>(
(context, actorType) => new ActorService(context, actorType,
(service, id) => new MyActor(myInjectedInterface, service, id)))
.GetAwaiter().GetResult();
创建 actor 实例的函数的签名是:
Func<ActorService, ActorId, ActorBase>
该框架提供了一个 ActorService
和 ActorId
的实例,您可以将其传递给 Actor 的构造函数并向下传递到基础构造函数。
奖金答案:
此处的用例与您的想法略有不同。这里的模式是一种通过接口分离具体实现的通用模式——这不是 client 应用程序修改运行时行为的方式。所以调用客户端不提供依赖项的具体实现(至少不通过构造函数注入)。依赖项在编译时注入。 IoC 容器通常会这样做,或者您可以手动提供一个。
当演员服务启动时,我想自动订阅任何事件 described in the documentation。手动订阅事件有效。然而,当服务被实例化时,有没有办法自动订阅参与者服务,就像在 OnActivateAsync() 中一样?
我试图做的是通过依赖注入解决这个问题,在 MyActor class 实例化时,它传入一个接口,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();
但是在它说“() => new MyActor(myInjectedInterface)”的那一行,它告诉我一个错误
Delegate 'Func' does not take 0 arguments
查看 Actor 上的构造函数 class 它具有以下内容
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 依赖项时收到的错误?
Delegate 'Func' does not take 0 arguments
奖金问题:
当我的无状态服务(调用客户端)调用时,如何解析要注入到 actor 服务中的接口实例的 IMyInjectedInterface?
IMyInjectedInterface myInjectedInterface = null;
//inject interface instance to the UserActor
ActorRuntime.RegisterActorAsync<MyActor>(
(context, actorType) => new ActorService(context, actorType,
(service, id) => new MyActor(myInjectedInterface, service, id)))
.GetAwaiter().GetResult();
创建 actor 实例的函数的签名是:
Func<ActorService, ActorId, ActorBase>
该框架提供了一个 ActorService
和 ActorId
的实例,您可以将其传递给 Actor 的构造函数并向下传递到基础构造函数。
奖金答案:
此处的用例与您的想法略有不同。这里的模式是一种通过接口分离具体实现的通用模式——这不是 client 应用程序修改运行时行为的方式。所以调用客户端不提供依赖项的具体实现(至少不通过构造函数注入)。依赖项在编译时注入。 IoC 容器通常会这样做,或者您可以手动提供一个。