如何使用 C# 将 AppSettings.json 转换为高级 Azure 设置?

How to convert AppSettings.json to Advance Azure Settings with C#?

您是否注意到 Azure 升级了 AppSettings Management?现在可以使用高级编辑选项一次更新多个 AppSettings,但格式与 AppSettings.json 不同。

我正在寻找一种快速解决方案,将我的 AppSettings 部分转换为 Azure Advance Edit 选项格式。你知道怎么做吗?

所以这个:

"Simulation": {
    "ApiUrl": "YourApiUrl",
    "ApiKey": "YouApiKey",
    "Groups": [
        {
            "Name": "YourGroup",
            "Latitude": 45.50884,
            "Longitude": -73.58781,
            "Radius": 500
        }
    ],
    "Planifications": [
        {
            "GroupName": "YourGroup",
            "At": "07:00",
            "Status": 10
        }
    ]
}

格式如下:

[
  {
    "Name": "Simulation:ApiUrl",
    "Value": "YourApiUrl",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:ApiKey",
    "Value": "YourApiKey",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Groups:0:Name",
    "Value": "YourGroup",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Groups:0:Latitude",
    "Value": "45.50884",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Groups:0:Longitude",
    "Value": "-73.58781",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Groups:0:Radius",
    "Value": "500",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Planifications:0:GroupName",
    "Value": "YourGroup",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Planifications:0:At",
    "Value": "07:00:00",
    "SlotSetting": false
  },
  {
    "Name": "Simulation:Planifications:0:Status",
    "Value": "10",
    "SlotSetting": false
  }
]

这是我将我的应用程序设置部分格式化为 Azure 高级编辑选项格式的快速解决方案:

我的分机:

    public static string ToJSON(this object obj)
    {
        return JToken.FromObject(obj).ToString();
    }

    public static IEnumerable<AzureSetting> ToAzureSettings(this object obj, string baseName = null, bool isEnumerable = false)
    {
        var t = obj.GetType();
        var result = new List<AzureSetting>();
        foreach (var propertyInfo in t.GetProperties())
        {
            var propValue = propertyInfo.GetValue(obj, null);
            if (propertyInfo.PropertyType.IsPrimitive
                || propertyInfo.PropertyType == typeof(string)
                || propertyInfo.PropertyType == typeof(TimeSpan)
                || propertyInfo.PropertyType == typeof(DateTime)
                || propertyInfo.PropertyType == typeof(DateTime?)
                || propertyInfo.PropertyType == typeof(DateTimeOffset)
                || propertyInfo.PropertyType == typeof(DateTimeOffset?)
            )
            {
                result.Add(
                    new AzureSetting()
                    {
                        Name = $"{baseName}:{propertyInfo.Name}",
                        Value = $"{propValue}"
                    }
                );
            }
            else if (typeof(IEnumerable).IsAssignableFrom(propertyInfo.PropertyType))
            {
                var enumerable = (IEnumerable)propValue;
                var i = 0;
                foreach (object child in enumerable)
                {
                    result.AddRange(child.ToAzureSettings($"{baseName}{propertyInfo.Name}:{i}", true));
                    i++;
                }
            }
            else
            {
                result.AddRange(propValue.ToAzureSettings($"{baseName}{propertyInfo.Name}:"));
            }
        }
        return result;
    }

public class AzureSetting
{
    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }
    [JsonProperty(PropertyName = "value")]
    public string Value { get; set; }
    [JsonProperty(PropertyName = "slotSetting")]
    public bool SlotSetting { get; set; } = false;
}

在program.cs

                    var simulationSettings = services.GetRequiredService<IOptions<SimulationSettings>>();
                    var azureJsonSettings = simulationSettings.Value.ToAzureSettings("Simulation").ToJSON();

                    using (StreamWriter writer = File.CreateText("bin\SimulationAzureSettings.txt"))
                    {
                        writer.WriteLine(azureJsonSettings);
                    }

希望对其他人有所帮助:)

如果您有任何问题,请告诉我。

这是快速解决方案,Great tool 将 .net 核心 appSetting.json 转换为 Azure appSetting.json。

有一个 dotnet 工具可以在 Nuget 上进行转换 dotnet-appsettings。需要 .NET 3.1 SDK。

dotnet tool install --global dotnet-appsettings
dotnet tool list --global
Usage: appsettings [appsettings.json]