59 lines
2.5 KiB
C#
59 lines
2.5 KiB
C#
using Microsoft.Extensions.Configuration;
|
|
|
|
namespace TarifasANEEL.Services
|
|
{
|
|
public class ConfigurationService
|
|
{
|
|
public static string ApiDadosAbertosUrl { get; private set; } = "";
|
|
public static string ResourcesDbPath { get; private set; } = "";
|
|
public static string MainDbPath { get; private set; } = "";
|
|
public static string ResourcesTableName { get; private set; } = "";
|
|
public static string DataTableName { get; private set; } = "";
|
|
public static int TimerDurationToClose { get; private set; }
|
|
public static string ResourcesDbConnectionString { get; private set; } = "";
|
|
public static string MainDbConnectionString { get; private set; } = "";
|
|
|
|
public static void LoadConfiguration()
|
|
{
|
|
var config = new ConfigurationBuilder()
|
|
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
|
.Build();
|
|
|
|
ApiDadosAbertosUrl = config["sApiDadosAbertosUrl"]!;
|
|
ResourcesDbPath = config["sResourcesDbPath"]!;
|
|
MainDbPath = config["sDbPath"]!;
|
|
ResourcesTableName = config["sResourcesTableName"]!;
|
|
DataTableName = config["sDataTableName"]!;
|
|
TimerDurationToClose = int.Parse(config["sTimerDurationToClose"]!);
|
|
|
|
ResourcesDbConnectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={ResourcesDbPath};Persist Security Info=False;";
|
|
MainDbConnectionString = $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={MainDbPath};Jet OLEDB:Database Password=gds21";
|
|
|
|
ValidateConfiguration();
|
|
}
|
|
|
|
private static void ValidateConfiguration()
|
|
{
|
|
var requiredSettings = new Dictionary<string, string>
|
|
{
|
|
{ nameof(ApiDadosAbertosUrl), ApiDadosAbertosUrl },
|
|
{ nameof(ResourcesDbPath), ResourcesDbPath },
|
|
{ nameof(MainDbPath), MainDbPath },
|
|
{ nameof(ResourcesTableName), ResourcesTableName },
|
|
{ nameof(DataTableName), DataTableName }
|
|
};
|
|
|
|
var missingSettings = requiredSettings
|
|
.Where(kvp => string.IsNullOrEmpty(kvp.Value))
|
|
.Select(kvp => kvp.Key)
|
|
.ToList();
|
|
|
|
if (missingSettings.Any())
|
|
{
|
|
throw new InvalidOperationException(
|
|
$"Missing required configuration values: {string.Join(", ", missingSettings)}");
|
|
}
|
|
}
|
|
}
|
|
}
|