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

2019-07-11 06:37发布

问题:

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:

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"



回答2:

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