Search code examples
c#.netakkaakka.nethocon

Assigning config values with HOCON and C#


I have a setup for an Akka ActorSystem like so:

akka {  
  actor{
    provider = ""Akka.Remote.RemoteActorRefProvider, Akka.Remote""
      deployment {
        /remoteecho {
           remote = ""akka.tcp://Target@{0}""
      }
    }
  }
}

What I want to do it substitute the {0} with another value. The only way I have found to do this is with Typesafe for Java, but I'm using C#.


Solution

  • Just like any other config format, HOCON is designed for static configuration. However you can attach multiple HOCON config strings as fallbacks, that means if searched config key won't be found in top level configuration, reader will use next fallback configuration in order to find a correct key-value. This is a recursive operation.

    If you want to configure those values dynamically, you have two options.

    1. If you need to resolve that address once, i.e. at the beginning of the program, you may initialize it directly in your actor system ActorSystem.Create(name, ConfigurationFactory.ParseString(hoconString)). If you must initialize you actor system first, you may use actorSystem.Settings.InjectTopLevelFallback(ConfigurationFactory.ParseString(hoconString)) to update your config. Remember that these changes will apply only after configuration has been applied. Moreover some of the Akka.NET plugins may cache config settings and may not react to these changes. So be cautious with that.
    2. Other way is to define deployment address explicitly when creating an actor. this could be done by using Props class, which is used to define all building schema for an actor. Example: actorSystem.ActorOf(Props.Create(() => new MyActor()).WithDeploy(new Deploy(new RemoteScope(Address.Parse(remoteAddress))))).