【问题标题】:Akka.net asp.net 5 mvc 6 configuration for Hocon用于 Hocon 的 Akka.net asp.net 5 mvc 6 配置
【发布时间】:2019-07-19 13:03:04
【问题描述】:

我目前正在尝试使用 akka.net,但他们使用 HOCON 的配置与配置时 app.json 中通常使用的 json 语法不同 我们的应用程序。 有谁知道如何在当前 app.json 配置中使用 HOCON?

【问题讨论】:

  • 你在使用.net core吗?
  • @profesor79 是的,我正在使用 .net core
  • 在akka.net gitter roomgitter.im/akkadotnet/akka.net提出这个问题
  • @profesor79 我一直在看聊天墙,没有回复问题
  • 所以 - 添加这个作为答案 - 将帮助其他人

标签: configuration asp.net-core-mvc asp.net-core-1.0 akka.net


【解决方案1】:

我使用 ConfigurationFactory.FromObject 和一些具有我感兴趣的属性的类从 appsettings 中读取 akka-config。

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

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

请注意,我没有费心去弄清楚如何从 appsettings 中解析 kebab-case 属性。所以我刚刚重命名了不包括连字符的属性。然后将 JsonProperty-attribute 设置为正确的名称,以便 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"
    }
  },
  "Hosting": {
    "Url": "http://*:1890"
  },

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

【讨论】:

    【解决方案2】:

    你可以做的是将HOCON放在它自己的文本文件中,然后执行以下操作:

    /// <summary>
    ///     Used to load HOCON definitions from a dedicated HOCON file
    /// </summary>
    public static class HoconLoader
    {
        /// <summary>
        ///     Parses a HOCON <see cref="Config" /> object from an underlying file
        /// </summary>
        /// <param name="path">The path to the HOCON file.</param>
        /// <returns>A parsed <see cref="Config" /> object.</returns>
        public static Config FromFile(string path)
        {
            return ConfigurationFactory.ParseString(File.ReadAllText(path));
        }
    }
    

    然后将该 hocon 对象传递给 ActorSystem.Create(string name, Config config)

    不要忘记将文件设置为“始终复制”或“如果更新则复制”

    【讨论】:

    • OP 询问是否使用 json 文件进行配置(特别是 appsettings.json),而不是 HOCON。
    猜你喜欢
    • 1970-01-01
    • 2016-03-28
    • 2023-03-05
    • 2016-03-24
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    相关资源
    最近更新 更多