diff --git a/RBLNews.Maui/MauiProgram.cs b/RBLNews.Maui/MauiProgram.cs index 391658f..c676649 100644 --- a/RBLNews.Maui/MauiProgram.cs +++ b/RBLNews.Maui/MauiProgram.cs @@ -1,7 +1,13 @@ using CommunityToolkit.Maui; using CommunityToolkit.Maui.Markup; + +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; + +using RBLNews.Maui.Services; +using RBLNews.Shared.Models; using RBLNews.Shared.Services; +using RBLNews.Shared.Services.Contracts; namespace RBLNews.Maui { @@ -22,7 +28,7 @@ namespace RBLNews.Maui builder.Services.AddMauiBlazorWebView(); builder.Services.AddBlazorBootstrap(); - + builder.Services.AddSingleton(); builder.Services.AddSingleton(); builder.Services.AddSingleton(); @@ -33,17 +39,13 @@ namespace RBLNews.Maui MauiApp app = builder.Build(); - using var serviceScope = app.Services.CreateScope(); - var services = serviceScope.ServiceProvider; + AppSettingsModel? appSettings = app.Configuration.GetSection("App").Get(); - var environmentVariablesService = services.GetRequiredService(); - string? backendApiUrl = environmentVariablesService.RblApiUrl; - - if (string.IsNullOrEmpty(backendApiUrl)) + if (string.IsNullOrEmpty(appSettings?.BackendApiUrl)) { - throw new Exception($"can't instantiate services, due to paramters are null. backendApiUrl: '{backendApiUrl}'"); + throw new Exception($"can't instantiate services, due to paramters are null. backendApiUrl: '{appSettings?.BackendApiUrl}'"); } - + return app; } } diff --git a/RBLNews.Maui/RBLNews.Maui.csproj b/RBLNews.Maui/RBLNews.Maui.csproj index bdcb172..9e9fc0d 100644 --- a/RBLNews.Maui/RBLNews.Maui.csproj +++ b/RBLNews.Maui/RBLNews.Maui.csproj @@ -90,6 +90,21 @@ + + + wwwroot\appsettings.Development.json + + + wwwroot\appsettings.json + + + wwwroot\appsettings.Production.json + + + wwwroot\appsettings.Test.json + + + diff --git a/RBLNews.Maui/Services/ConfigService.cs b/RBLNews.Maui/Services/ConfigService.cs new file mode 100644 index 0000000..09571c9 --- /dev/null +++ b/RBLNews.Maui/Services/ConfigService.cs @@ -0,0 +1,20 @@ +using Microsoft.Extensions.Configuration; + +using RBLNews.Shared.Models; +using RBLNews.Shared.Services.Contracts; + +namespace RBLNews.Maui.Services; + +public class ConfigService : IConfigService +{ + public AppSettingsModel? AppSettings { get; } + + public ConfigService(IConfiguration configuration) + { + this.AppSettings = configuration.GetSection("App").Get(); + if(this.AppSettings == null) + { + throw new NullReferenceException("App settings not found"); + } + } +} \ No newline at end of file diff --git a/RBLNews.Shared/Services/Contracts/IConfigService.cs b/RBLNews.Shared/Services/Contracts/IConfigService.cs index 1a07ef1..b92b96c 100644 --- a/RBLNews.Shared/Services/Contracts/IConfigService.cs +++ b/RBLNews.Shared/Services/Contracts/IConfigService.cs @@ -4,5 +4,5 @@ namespace RBLNews.Shared.Services.Contracts; public interface IConfigService { - AppSettingsModel AppSettings { get; } + AppSettingsModel? AppSettings { get; } } \ No newline at end of file diff --git a/RBLNews.Web/Services/ConfigService.cs b/RBLNews.Web/Services/ConfigService.cs index edd0d47..bff176c 100644 --- a/RBLNews.Web/Services/ConfigService.cs +++ b/RBLNews.Web/Services/ConfigService.cs @@ -5,7 +5,7 @@ namespace RBLNews.Web.Services; public class ConfigService : IConfigService { - public AppSettingsModel AppSettings { get; } + public AppSettingsModel? AppSettings { get; } public ConfigService(IConfiguration configuration) {