4

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. How can I register IReliableStateManagerReplica using Autofac? 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)'.

Shahar Shokrani
  • 7,598
  • 9
  • 48
  • 91
kowsalyav
  • 41
  • 5

1 Answers1

1

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.

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.

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

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.

Please take a look on this other SO: Set up Dependency Injection on Service Fabric using default ASP.NET Core DI container

Diego Mendes
  • 10,631
  • 2
  • 32
  • 36
  • 1
    Hi, appreciate your recommendation. But if I want to use my custom constructor instead of the default one, how do I register IReliableStateManagerReplica using Autofac? Can it be done? – kowsalyav Feb 13 '19 at 14:20