c#-Service Fabric Actor服务依赖注入和Actor事件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-Service Fabric Actor服务依赖注入和Actor事件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2438字,纯文字阅读大概需要4分钟。
内容图文
![c#-Service Fabric Actor服务依赖注入和Actor事件](/upload/InfoBanner/zyjiaocheng/684/ed5fb6ad8a98467fa5476e11ea6362f6.jpg)
当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容器可以做到这一点,或者您可以手动提供一个.
内容总结
以上是互联网集市为您收集整理的c#-Service Fabric Actor服务依赖注入和Actor事件全部内容,希望文章能够帮你解决c#-Service Fabric Actor服务依赖注入和Actor事件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。