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

时间:2017-03-21 14:11:24

标签: c# akka akka.net akka.net-persistence akka-persistent-fsm

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

此处数据源连接字符串设置为我的localhost。

是否可以在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@;"
             }
          }

      }

1 个答案:

答案 0 :(得分:0)

您可以使用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";
        }
    }
}