加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

.net-core – 如何在Asp.net核心中使用Akka.Net 1.3.0配置的Apps

发布时间:2020-12-16 09:33:04 所属栏目:asp.Net 来源:网络整理
导读:我一直在玩Visual Studio 2017中支持.NetStandard 1.6的新版Akka.Net.由于Akka.Net配置的特性,它使用HOCON格式进行配置.之前的版本在app.config或Web.config中嵌入了易于阅读的HOCON配置.另一种选择是使用接受字符串对象的ConfigurationFactory.ParseString
我一直在玩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:SqliteDbSample.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:SqliteDbSample.db3""
            table-name = snapshot_store
            auto-initialize = on

        }
}

}

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

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

解决方法

我将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"
      }
    }
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读