简体   繁体   English

如何从C#代码后面为akka.net持久性参与者设置连接字符串

[英]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. 我使用了配置为sql server插件的持久性参与者。 Here is below my hocon configurations. 这是我的hocon配置下面。

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

      }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM