'How to create child actors in Akka.NET with Dependency Injecton
I want to use akka for concurrent message processing. I am working with objects called Fixture. My design is to have a supervisor actor that gets all the messages and then routes them to another actor that handles all messages for the specific Fixture. To do this I have a dictionary of <FixtureID,IActorRef> in the supervisor actor. Naturally, if the supervisor receives a message for a Fixture that currently has no IActorRef, the supervisor needs to create a child actor and add it the dictionary. This is where I run into trouble. I am unable to create a child actor because it cannot resolve the dependencies.
This is how I register the supervising actor so I can pass it to the class that will send him the messages:
services.AddSingleton<IActorRef>(sp =>
{
var bootstrap = BootstrapSetup.Create();
var di = DependencyResolverSetup.Create(sp);
var actorSystemSetup = bootstrap.And(di);
var _actorSystem = ActorSystem.Create("AspNetDemo", actorSystemSetup);
var resolver = DependencyResolver.For(_actorSystem);
var props = resolver.Props<SupervisingActor>();
return _actorSystem.ActorOf(props, "supervisor");
});
This is how I create child actors in the supervisor:
Context.ActorOf(Context.DI().Props<FixtureActor>(), $"FixtureActor{message.Id}");
The supervisor actor is created successfully but when it tries to spawn a child actor it throws this error: 'The Dependency Resolver has not been configured yet'
My code is based on this: https://getakka.net/articles/actors/dependency-injection.html Note: I do not want to use another DI Container.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
