Akka.net asp.net 5 mvc 6 configuration for Hocon

假装没事ソ 提交于 2019-12-13 16:12:47

问题


I am currently trying to use akka.net but the configuration they use HOCON is a different syntax from the json syntax normally used in app.json when configuring our app. Does anyone know how to use HOCON with the current app.json configuration?


回答1:


I use ConfigurationFactory.FromObject and some classes that has the properties that I'm interested in to read the akka-config from appsettings.

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

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

Notice that I haven't bothered to figure out how to parse kebab-case properties from appsettings. So I have just renamed the properties excluding the hyphens. Then set the JsonProperty-attribute to the correct name so that FromObject can deserialize it correctly.

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:


What you can do is putting the HOCON in its own text file and then executing the following:

/// <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));
    }
}

Then pass that hocon object into the ActorSystem.Create(string name, Config config)

Dont forget tp make the file "Copy Always" or "Copy If Newer"



来源:https://stackoverflow.com/questions/36699889/akka-net-asp-net-5-mvc-6-configuration-for-hocon

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!