如何从后面的 C# 代码为 akka.net persistence actor 设置连接字符串

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

我使用了配置为 sql 服务器插件的持久性 actor。下面是我的 hocon 配置。

此处数据源连接字符串设置为我的本地主机。

是否可以在 actor 系统初始化之前从后面的 C# 代码设置此连接字符串?

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@;"
             }
          }

      }

你可以使用 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("Whosebug", mainConfig);
        }

        private static string GetConnectionString()
        {
            return "1";
        }
    }
}