Akka.net .NET Core configuration

155 Views Asked by At

I'm trying to configure akka.net in an ASP.NET Core 7 Web API.

I added akka.net config into appsettings.json but the configuration still does not work. What am I doing wrong? How to configure akka.net 1.5.13 in .NET 7?

{
    "akka": {
        "stdout-loglevel": "DEBUG",
        "stdout-logger-class": "Akka.Event.StandardOutLogger",
        "log-config-on-start": "off",
        "loglevel": "DEBUG",
        "loggers": [ "Akka.Logger.NLog.NLogLogger,Akka.Logger.NLog" ],
    }
}
2

There are 2 best solutions below

2
On
builder.Services
    .AddAkka("ActorSystem", (builder, _) =>
    {
        builder.AddHoconFile("akka.hocon", HoconAddMode.Prepend)
        .WithActors((system, registry, resolver) =>
        {
          // init your actors here 
        }
    }

Your akka settings should be in the akka.hocon file.

Better way would be using Akka.Hosting since you're using WebApi.

https://github.com/akkadotnet/Akka.Hosting

0
On

I found a way to configure Akka.net, maybe it's not the best way, but it works.

  1. Put configuration into a separate file: akka.config. Here is example of the content of this file:

    akka {
        stdout-loglevel = DEBUG
        loglevel = DEBUG
        log-config-on-start = on
    }
    
  2. When you create the Actor system, you should read and parse this config and provide it to the Actor system:

    var config =
    ConfigurationFactory.ParseString(
        File.ReadAllText(Path.Combine(Environment.CurrentDirectory, "akka.config")));
    builder.Services.AddSingleton<ActorSystem>(_ => ActorSystem.Create("MySystem",config));
    

Useful links: code project