2

我目前正在嘗試使用akka.net,但他們使用HOCON的配置與配置 我們的應用的app.json中通常使用的json語法不同。 有誰知道如何在當前的app.json配置中使用HOCON?Akka.net asp.net 5 Hocon的mvc 6配置

+0

您使用.NET的核心? – profesor79

+0

@ profesor79是的,我正在使用.net核心 – HBbaale

+0

在akka.net問這個問題gitter room https://gitter.im/akkadotnet/akka.net – profesor79

回答

1

你可以做的是把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(字符串名稱,配置配置)

不要忘記tp使文件「總是複製」或「如果更新」複製

0

我使用ConfigurationFactory.FromObject和一些具有我感興趣的屬性從appsettings讀取akka配置的類。

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