Search code examples
c#akkaakka.netakka.net-persistenceakka-persistent-fsm

how to set connection string for akka.net persistence actor from C# code behind


I have used persistence actor which is configured to sql server plugin. Here is below my hocon configurations.

Here data source connection string is set to my localhost.

Could it be possible to set this connection string from C# code behind just before actor system initialize?

akka.persistence {
          journal {
            plugin = "akka.persistence.journal.sql-server"                
            sql-server {
                  class = "Akka.Persistence.SqlServer.Journal.SqlServerJournal, Akka.Persistence.SqlServer"
                  plugin-dispatcher = "akka.actor.default-dispatcher"

                  # connection string used for database access
                  connection-string = "Data Source=ES-NB-046\\MSSQLSERVER_2014;Initial Catalog=SwedolTest;User ID=sa;Password=aaaaaa@;"
                  # can alternativly specify: connection-string-name

                  # default SQL timeout
                  connection-timeout = 30s

                  # SQL server schema name
                  schema-name = dbo

                  # persistent journal table name
                  table-name = EventJournal

                  # initialize journal table automatically
                  auto-initialize = on

                  timestamp-provider = "Akka.Persistence.Sql.Common.Journal.DefaultTimestampProvider, Akka.Persistence.Sql.Common"
                  metadata-table-name = Metadata
            }
          } 

          snapshot-store {
            plugin = "akka.persistence.snapshot-store.sql-server"
              sql-server {
                class = "Akka.Persistence.SqlServer.Snapshot.SqlServerSnapshotStore, Akka.Persistence.SqlServer"
                plugin-dispatcher = "akka.actor.default-dispatcher"
                table-name = SnapshotStore
                schema-name = dbo
                auto-initialize = on
                connection-string = "Data Source=ES-NB-046\\MSSQLSERVER_2014;Initial Catalog=SwedolTest;User ID=sa;Password=aaaaaa@;"
             }
          }

      }

Solution

  • You could use Configuration Fallbacks:

    namespace AkkaTest
    {
        using Akka.Actor;
        using Akka.Configuration;
    
        class Program
        {
            static void Main(string[] args)
            {
                //Main config could be load different ways. This is placeholder
                var mainConfig = ConfigurationFactory.Default();
                var conString = GetConnectionString();
    
                var conStringConfig = ConfigurationFactory.ParseString(
                    $@"akka.persistence.journal.sqlite.connection-string        = ""{conString}""
                       akka.persistence.snapshot-store.sqlite.connection-string = ""{conString}""
                ");
    
                mainConfig = mainConfig.WithFallback(conStringConfig);
    
                var system = ActorSystem.Create("stackOverflow", mainConfig);
            }
    
            private static string GetConnectionString()
            {
                return "1";
            }
        }
    }