2016-09-22 50 views
1

当演员服务启动时,我想自动订阅described in the documentation中的任何事件。手动订阅活动作品。但是,当服务被实例化时,是否有自动订阅actor服务的方法,就像OnActivateAsync()一样?服务结构演员服务依赖注入和演员事件

我想要做的是通过依赖注入来解决这个问题,它在MyActor类的实例化中通过OnActivateAsync调用来为客户端订阅事件的接口。但是,我有依赖注入问题。

使用Microsoft.ServiceFabric.Actors.2.2.207应该支持到actor服务的依赖注入。现在,在实现Microsoft.ServiceFabric.Actors.Runtime.Actor时,将使用ActorService和ActorId参数创建默认构造函数。

我想添加我自己的构造函数,其中有一个额外的接口被传入。你如何为actor服务注册以添加依赖项?在默认的Program.cs主要提供这个

IMyInjectedInterface myInjectedInterface = null; 
    //inject interface instance to the UserActor 
       ActorRuntime.RegisterActorAsync<MyActor>(
        (context, actorType) => new ActorService(context, actorType,() => new MyActor(myInjectedInterface))).GetAwaiter().GetResult(); 

但是在那里说:行了“()=>新MyActor(myInjectedInterface)”它告诉我一个错误

委托“功能”不采取0 参数

望着在演员类的构造函数它具有以下

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依赖项时收到的错误?

委托“功能”不拿0 参数

奖励题:

如何解决IMyInjectedInterface的接口实例被注入到演员服务时打电话给我无状态服务(呼叫客户端)?

回答

2
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> 

框架提供的ActorServiceActorId一个实例,您可以通过底座的构造中传递给你的演员的构造和向下。

奖金答:

使用情况在这里是不是你在想什么的有点不同。这里的模式是通过接口分离具体实现的一般模式 - 这不是用于修改运行时行为的客户端的一种方式。所以调用客户端不提供依赖的具体实现(至少不通过构造器注入)。依赖项在编译时注入。一个IoC容器通常会这样做,或者您可以手动提供一个容器。

+0

我有一个在我的web-API上构建Autofac注册表的web-api。 Autofac注册接口和实现。是否有可能让共享项目上的接口为(actor服务接口)并让web-API使用autofac实现这些接口?我正在尝试这种方式,因为事件映射似乎是如何设置的。 –