c#-Service Fabric Actor服务依赖注入和Actor事件
作者:互联网
当actor服务启动时,我想自动将任何事件订阅为described in the documentation.手动订阅事件是可行的.但是,当实例化服务时,是否可以像OnActivateAsync()中那样自动订阅参与者服务?
我试图做的是通过依赖注入解决此问题,该依赖注入在MyActor类的实例化时将其传递给OnActivateAsync调用以为客户端订阅事件的接口.但是我在依赖注入方面遇到了问题.
应该使用Microsoft.ServiceFabric.Actors.2.2.207支持对Actor服务的依赖注入.现在,在实现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)”的行上,它告诉我一个错误
Delegate ‘Func’ does not take 0
arguments
在Actor类上查看构造函数,它具有以下内容
我的演员
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
奖励问题:
当我的无状态服务(调用客户端)调用接口接口时,如何解析IMyInjectedInterface以便将接口实例注入到actor服务中?
解决方法:
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的构造函数,然后向下传递给基本构造函数.
奖励答案:
这里的用例与您所想的有些不同.这里的模式是一种通用模式,通过接口将具体的实现分离开来-这不是客户端应用程序修改运行时行为的方式.因此,调用方客户端不提供依赖项的具体实现(至少不是通过构造函数注入).依赖项在编译时注入.通常,一个IoC容器可以做到这一点,或者您可以手动提供一个.
标签:dependency-injection,azure-service-fabric,c,service-fabric-actor 来源: https://codeday.me/bug/20191026/1938227.html