Skip to content

Instantly share code, notes, and snippets.

@thomaslazar
Last active January 21, 2020 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaslazar/144e4aabfdc6f18dfbd93ca309555597 to your computer and use it in GitHub Desktop.
Save thomaslazar/144e4aabfdc6f18dfbd93ca309555597 to your computer and use it in GitHub Desktop.
SimpleInjector and Akka.Net example failing during container.Verify()
using System;
using System.Threading;
using Akka.Actor;
using Akka.DI.Core;
using Akka.DI.SimpleInjector;
using SimpleInjector;
namespace SimpleInjectorAkkaNetExample
{
public interface IMyDependency
{
void SayHello(string parent);
}
public class MyDependency : IMyDependency
{
public void SayHello(string parent)
{
Console.WriteLine($"Hello from MyDependency! My parent is {parent}.");
}
}
public class SomeMessage { }
public class MyActor : ReceiveActor
{
private readonly IMyDependency _myDependency;
public MyActor(IMyDependency myDependency)
{
_myDependency = myDependency;
Receive<SomeMessage>(msg =>
{
Console.WriteLine($"Received {msg.GetType()} message.");
Console.WriteLine($"Hello from {Self.Path}!");
myDependency.SayHello(Self.Path.ToString());
});
}
}
internal class Program
{
private static void Main(string[] args)
{
var container = new Container();
container.Register<IMyDependency, MyDependency>();
container.Register<MyActor>();
try
{
container.Verify(); // <- this fails due to the container instantiating actor classes which it shouldn't
}
catch (Exception e)
{
Console.WriteLine(e);
Console.WriteLine();
}
using (var actorSystem = ActorSystem.Create("MyActorSystem"))
{
var resolver = new SimpleInjectorDependencyResolver(container, actorSystem);
var myFirstActorRef = actorSystem.ActorOf(actorSystem.DI().Props<MyActor>(), "myFirstActor");
var mySecondActorRef = actorSystem.ActorOf(actorSystem.DI().Props<MyActor>(), "mySecondActor");
myFirstActorRef.Tell(new SomeMessage());
Thread.Sleep(1000);
mySecondActorRef.Tell(new SomeMessage());
Console.ReadKey();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment