如何在Asp.net核心中使用Akka.Net 1.3.0配置的Appsetting配置

时间:2017-08-18 13:31:53

标签: .net-core visual-studio-2017 akka.net .net-standard

我一直在玩Visual Studio 2017中支持.NetStandard 1.6的新版Akka.Net。由于Akka.Net配置的特殊性,它使用HOCON格式进行配置。之前的版本在app.config或Web.config中嵌入了易于阅读的HOCON配置。另一种选择是使用接受字符串对象的ConfigurationFactory.ParseString方法。但是从字符串解析HOCON对于小配置部分来说很方便。在我的情况下,我留下了这个ParseString配置,它甚至没有按预期工作。 我想出了这个:

 var configString = @"akka {
        log-config-on-start = on
        stdout-loglevel = INFO
        loglevel = DEBUG
        loggers= ""[Akka.Logger.Serilog.SerilogLogger, Akka.Logger.Serilog]""
        actor {

                    debug {
                        receive = on
                        autoreceive = on
                        lifecycle = on
                        event-stream = on
                        unhandled = on
                    }
              }

    akka.persistence {
        journal {
                    plugin = ""akka.persistence.journal.sqlite""

                    sqlite {
                                    class = ""Akka.Persistence.Sqlite.Journal.SqliteJournal, Akka.Persistence.Sqlite""
                                        plugin-dispatcher = ""akka.actor.default-dispatcher""
                                        connection-string = ""Data Source = F:\\SqliteDb\\Sample.db3""
                                      table-name = event_journal
                                    metadata-table-name = journal_metadata
                                    auto-initialize = on

                            }
                    }

    snapshot-store {
        plugin = ""akka.persistence.snapshot-store.sqlite""
        sqlite {
            class = ""[Akka.Persistence.Sqlite.Snapshot.SqliteSnapshotStore, Akka.Persistence.Sqlite]""
            connection-string = ""Data Source = F:\\SqliteDb\\Sample.db3""
            table-name = snapshot_store
            auto-initialize = on

        }
}

}

     ";
        var config = ConfigurationFactory.ParseString(configString);
         ActorSystem.Create("AkkaSystem", config);

没有按预期工作。 我们如何使用appsetting.json在Asp.Net核心中配置akka.net?或者有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

我将hocon转换为json并使用ConfigurationFactory.FromObject和一些具有我感兴趣的属性的类来从appsettings中读取akka-config。匿名对象模拟hocon根目录。

var config = ConfigurationFactory.FromObject(new { akka = configuration.GetSection("Akka").Get<AkkaConfig>() });

actorSystem = ActorSystem.Create("Stimpy", config);

请注意,我并不打算弄清楚如何从appsettings解析kebab-case属性。所以我刚刚重命名了连字符以外的属性。然后将JsonProperty属性设置为正确的名称,以便FromObject可以正确地反序列化它。

public class AkkaConfig
{
    [JsonProperty(PropertyName = "log-config-on-start")]
    public string logconfigonstart { get; set; }
    [JsonProperty(PropertyName = "stdout-loglevel")]
    public string stdoutloglevel { get; set; }
    public string loglevel { get; set; }
    public string[] loggers { get; set; }
    public ActorConfig actor { get; set; }

    public class ActorConfig
    {
        public DebugConfig debug { get; set; }
        public Dictionary<string, string> serializers { get; set; }
        [JsonProperty(PropertyName = "serialization-bindings")]
        public Dictionary<string, string> serializationbindings { get; set; }

        public class DebugConfig
        {
            public string receive { get; set; }
            public string autoreceive { get; set; }
            public string lifecycle { get; set; }
            [JsonProperty(PropertyName = "event-stream")]
            public string eventstream { get; set; }
            public string unhandled { get; set; }
        }
    }
}

appsettings.json:

{
  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Trace"
    }
  },
  "Akka": {
    "logconfigonstart":"on",
    "stdoutloglevel":"INFO",
    "loglevel": "DEBUG",
    "loggers": [ "Akka.Logger.NLog.NLogLogger, Akka.Logger.NLog" ],

    "actor": {
      "debug": {
        "receive": "on",
        "autoreceive": "on",
        "lifecycle": "on",
        "eventstream": "on",
        "unhandled": "on"
      },
      "serializers": {
        "hyperion": "Akka.Serialization.HyperionSerializer, Akka.Serialization.Hyperion"
      },
      "serializationbindings": {
        "System.Object": "hyperion"
      }
    }
  }
}