简体   繁体   English

使用Autofac注册IReliableStateManagerReplica

[英]Register IReliableStateManagerReplica using Autofac

I have Stateful Service fabric service which has the below constructor. 我有状态服务结构服务,具有以下构造函数。

public StatefuleService(StatefulServiceContext context, IReliableStateManagerReplica manager,
IActorProxyFactory actorProxyFactory = null)
            : base(context, manager)
        {
            ActorProxyFactory = actorProxyFactory ?? new ActorProxyFactory();
        }

I'm using Autofac to register the components. 我正在使用Autofac来注册组件。 How can I register IReliableStateManagerReplica using Autofac? 如何使用Autofac注册IReliableStateManagerReplica? I tried 我试过了

builder.RegisterType<ReliableStateManager>().As<IReliableStateManagerReplica>().SingleInstance;

but it gives the following exception: 但它给出了以下例外:

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Microsoft.ServiceFabric.Data.ReliableStateManager' can be invoked with the available services and parameters: Cannot resolve parameter 'System.Fabric.StatefulServiceContext serviceContext' of constructor 'Void .ctor(System.Fabric.StatefulServiceContext, Microsoft.ServiceFabric.Data.ReliableStateManagerConfiguration)'. 使用可用的服务和参数可以调用在'Microsoft.ServiceFabric.Data.ReliableStateManager'类型上找到'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'的构造函数:无法解析参数'System.Fabric.StatefulServiceContext serviceContext'构造函数'Void .ctor(System.Fabric.StatefulServiceContext,Microsoft.ServiceFabric.Data.ReliableStateManagerConfiguration)'。

The purpose of the constructor StatefulService(StatefulServiceContext, IReliableStateManagerReplica) is to initialize a new instance of the StatefulService class with non-default reliable state manager replica. 构造函数StatefulService(StatefulServiceContext, IReliableStateManagerReplica)是使用非默认的可靠状态管理器副本初始化StatefulService类的新实例。

This means, that you have implemented your own version of reliable state provider with a custom logic and does not want to use the default implemented by Service Fabric. 这意味着,您已使用自定义逻辑实现了自己的可靠状态提供程序版本,并且不希望使用Service Fabric实现的默认值。

If this is not the case, you should use the default constructor StatefulService(StatefulServiceContext) and if you need access to the StateManager, you can access it from ((StatefulServiceContext)context).StateManager 如果不是这种情况,则应使用默认构造函数StatefulService(StatefulServiceContext) ,如果需要访问StateManager,则可以从((StatefulServiceContext)context).StateManager访问它((StatefulServiceContext)context).StateManager

Another recommendation I give to you, you should not create StatefulService using the DI, because the StatefulServiceContext is created at runtime and multiple replicas\\partitions can reuse the same process in a shared host, that means you may have multiple StatefulServiceContext and the DI does not know which one to use. 我给你的另一个建议是,你不应该使用DI创建StatefulService,因为StatefulServiceContext是在运行时创建的,多个replicas \\ partitions可以在共享主机中重用相同的进程,这意味着你可能有多个StatefulServiceContext而DI不知道使用哪一个。

Please take a look on this other SO: Set up Dependency Injection on Service Fabric using default ASP.NET Core DI container 请查看其他SO: 使用默认的ASP.NET Core DI容器在Service Fabric上设置依赖注入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM