Actor Producer Extension backed by the Microsoft Dependency Injection Dependency Injection Container for the Akka.NET framework.
Akka.DI.Microsoft is an ActorSystem extension for the Akka.NET framework that provides an alternative to the basic capabilities of Props when you have Actors with multiple dependencies.
The best way to understand how to use it is by example. If you are already considering this extension then we will assume that you know how how to use the Microsoft Dependency Injection container.
Start by creating your container and registering your actors and dependencies.
// Setup Microsoft Dependency Injection
var services = new ServiceCollection();
services.AddScoped<IWorkerService, WorkerService>();
services.AddScoped<ITypedWorker, TypedWorker>();
Next you have to create your ActorSystem
and inject that system reference along with the container reference into a new instance of the MicrosoftDependencyResolver
.
// Create the ActorSystem
using (var system = ActorSystem.Create("MySystem"))
{
// Create the dependency resolver
var resolver = new MicrosoftDependencyResolver(scopeFactory, system);
// we'll fill in the rest in the following steps
}
To register the actors with the system use method Akka.Actor.Props Create<TActor>()
of the IDependencyResolver
interface implemented by the MicrosoftDependencyResolver
.
// Register the actors with the system
system.ActorOf(resolver.Create<TypedWorker>(), "Worker1");
system.ActorOf(resolver.Create<TypedWorker>(), "Worker2");
// OR
system.DI().Props<TypedWorker>("Worker1");
system.DI().Props<TypedWorker>("Worker2");
Finally create your router, message and send the message to the router.
// Create the router
var router = system.ActorOf(Props.Empty.WithRouter(new ConsistentHashingGroup(config)));
// Create the message to send
var message = new TypedActorMessage
{
Id = 1,
Name = Guid.NewGuid().ToString()
};
// Send the message to the router
router.Tell(message);
The resulting code should look similar to the the following:
// Setup Microsoft Dependency Injection
var services = new ServiceCollection();
services.AddScoped<IWorkerService, WorkerService>();
services.AddScoped<ITypedWorker, TypedWorker>();
// Create the ActorSystem
using (var system = ActorSystem.Create("MySystem"))
{
// Create the dependency resolver
var resolver = new MicrosoftDependencyResolver(container, system);
// Register the actors with the system
system.ActorOf(resolver.Create<TypedWorker>(), "Worker1");
system.ActorOf(resolver.Create<TypedWorker>(), "Worker2");
// OR
var props1 = system.DI().Props<TypedWorker>("Worker1");
var props2 = system.DI().Props<TypedWorker>("Worker2");
// Create the router
var router = system.ActorOf(Props.Empty.WithRouter(new ConsistentHashingGroup(config)));
// Create the message to send
var message = new TypedActorMessage
{
Id = 1,
Name = Guid.NewGuid().ToString()
};
// Send the message to the router
router.Tell(message);
}