当一个参与者服务出现时,我想以在文档中描述的形式自动订阅任何事件。手动订阅事件工作。但是,当服务被实例化时,是否有一种方法可以自动订阅参与者服务,比如在OnActivateAsync()中?
我想要做的是通过依赖注入来解决这个问题,在MyActor类的实例化中,它传入一个接口,OnActivateAsync调用这个接口来为客户机订阅事件。但是,我的依赖注入有问题。
使用Microsoft.ServiceFabric.Actors.2.2.207应该支持对参与者服务的依赖注入。现在,在实现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 (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?
发布于 2016-09-22 22:53:04
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();创建参与者实例的函数的签名是:
Func<ActorService, ActorId, ActorBase>该框架提供了ActorService和ActorId的一个实例,您可以将它们传递给Actor的构造函数,然后通过基本构造函数向下传递。
奖金答案:
这里的用例与您所想的略有不同。这里的模式是通过接口将具体实现解耦的通用模式--它不是客户机应用程序修改运行时行为的一种方式。因此,调用客户端没有提供依赖项的具体实现(至少不是通过构造函数注入)。依赖项在编译时注入。IoC容器通常会这样做,或者您可以手动提供一个容器。
https://stackoverflow.com/questions/39647952
复制相似问题