| | 1 | | using Microsoft.Extensions.Configuration; |
| | 2 | | using MRA.Infrastructure.Settings; |
| | 3 | | using Microsoft.Extensions.Configuration.AzureAppConfiguration; |
| | 4 | | using Microsoft.Extensions.Logging; |
| | 5 | | using MRA.Infrastructure.RemoteConfig; |
| | 6 | |
|
| | 7 | | namespace MRA.Infrastructure.Database.Providers; |
| | 8 | |
|
| | 9 | | public class AzureAppConfigurationDatabase : IRemoteConfigDatabase |
| | 10 | | { |
| | 11 | | private readonly AppSettings _appSettings; |
| | 12 | | private readonly IConfigurationRoot _configuration; |
| | 13 | | private readonly ILogger<AzureAppConfigurationDatabase> _logger; |
| | 14 | |
|
| 1 | 15 | | public AzureAppConfigurationDatabase( |
| 1 | 16 | | AppSettings appConfig, |
| 1 | 17 | | ILogger<AzureAppConfigurationDatabase> logger |
| 1 | 18 | | ) |
| | 19 | | { |
| 1 | 20 | | _logger = logger; |
| 1 | 21 | | _appSettings = appConfig; |
| | 22 | | try |
| | 23 | | { |
| 1 | 24 | | _configuration = new ConfigurationBuilder() |
| 1 | 25 | | .AddAzureAppConfiguration(options => |
| 1 | 26 | | { |
| 1 | 27 | | options.Connect(_appSettings.RemoteConfig.ConnectionString) |
| 1 | 28 | | .Select(KeyFilter.Any); |
| 1 | 29 | | }) |
| 1 | 30 | | .Build(); |
| 1 | 31 | | }catch(Exception ex) |
| | 32 | | { |
| 1 | 33 | | _logger.LogError(ex, "Error when initializing Azure App Configuration"); |
| 1 | 34 | | } |
| 1 | 35 | | } |
| | 36 | |
|
| | 37 | | public T GetValue<T>(RemoteConfigSetting<T> remoteConfig) |
| | 38 | | { |
| | 39 | | try |
| | 40 | | { |
| 0 | 41 | | return _configuration.GetValue(remoteConfig.Key.ToString(), remoteConfig.DefaultValue); |
| | 42 | | } |
| 0 | 43 | | catch(Exception ex) |
| | 44 | | { |
| 0 | 45 | | _logger.LogError(ex, "Error when getting Azure App Configuration with key '{Key}'. Returning default value i |
| 0 | 46 | | return remoteConfig.DefaultValue; |
| | 47 | | } |
| 0 | 48 | | } |
| | 49 | | } |