From 2fa221b7aec6fd5ae8faf23d924d9839779b4ad0 Mon Sep 17 00:00:00 2001 From: Yaser Moradi Date: Mon, 8 Jul 2024 19:41:46 +0200 Subject: [PATCH] fix --- .../Layout/AppErrorBoundary.razor.cs | 2 +- .../Components/Layout/Footer.razor.cs | 2 +- .../IConfigurationBuilderExtensions.cs | 17 ++------ .../IServiceCollectionExtensions.cs | 4 +- .../Services/AppRenderMode.cs | 2 +- .../Services/AuthenticationManager.cs | 2 +- .../Services/ExceptionHandlerBase.cs | 2 +- ...ebug.json => appsettings.Development.json} | 0 ...lease.json => appsettings.Production.json} | 0 .../Components/Pages/AboutPage.razor | 12 ++++++ .../Components/Pages/AboutPage.razor.cs | 4 ++ .../Boilerplate.Client.Maui/MainPage.xaml.cs | 2 +- .../MauiProgram.Services.cs | 4 +- .../Boilerplate.Client.Maui/MauiProgram.cs | 6 +-- .../Services/MauiLocalHttpServer.cs | 2 +- .../Client/Boilerplate.Client.Web/Program.cs | 2 + .../Components/Pages/AboutPage.razor | 8 ++++ .../Components/Pages/AboutPage.razor.cs | 3 ++ .../MainWindow.xaml.cs | 2 +- .../Program.Services.cs | 4 +- .../Services/WindowsLocalHttpServer.cs | 2 +- .../Bit.Boilerplate/src/Directory.Build.props | 5 +++ .../Server/Boilerplate.Server.Api/Program.cs | 6 ++- .../Services/AppSecureJwtDataFormat.cs | 2 +- .../Program.Services.cs | 1 - .../Server/Boilerplate.Server.Web/Program.cs | 6 ++- .../IConfigurationBuilderExtensions.cs | 17 ++------ .../Extensions/IConfigurationExtensions.cs | 2 +- .../src/Shared/Services/AppEnvironment.cs | 42 +++++++++++++++++++ .../src/Shared/Services/BuildConfiguration.cs | 24 ----------- ...ebug.json => appsettings.Development.json} | 0 ...lease.json => appsettings.Production.json} | 0 32 files changed, 110 insertions(+), 77 deletions(-) rename src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/{appsettings.Debug.json => appsettings.Development.json} (100%) rename src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/{appsettings.Release.json => appsettings.Production.json} (100%) create mode 100644 src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Services/AppEnvironment.cs delete mode 100644 src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Services/BuildConfiguration.cs rename src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/{appsettings.Debug.json => appsettings.Development.json} (100%) rename src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/{appsettings.Release.json => appsettings.Production.json} (100%) diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/AppErrorBoundary.razor.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/AppErrorBoundary.razor.cs index 330159b874..229818c01c 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/AppErrorBoundary.razor.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/AppErrorBoundary.razor.cs @@ -15,7 +15,7 @@ public partial class AppErrorBoundary protected override void OnInitialized() { - showException = BuildConfiguration.IsDebug(); + showException = AppEnvironment.IsDevelopment(); } protected override async Task OnErrorAsync(Exception exception) diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/Footer.razor.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/Footer.razor.cs index 01d81fd6e4..7bb9be3a2c 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/Footer.razor.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Components/Layout/Footer.razor.cs @@ -42,7 +42,7 @@ await cookie.Set(new() Name = ".AspNetCore.Culture", Value = Uri.EscapeDataString($"c={SelectedCulture}|uic={SelectedCulture}"), MaxAge = 30 * 24 * 3600, - Secure = BuildConfiguration.IsRelease() + Secure = AppEnvironment.IsDevelopment() is false }); } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Extensions/IConfigurationBuilderExtensions.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Extensions/IConfigurationBuilderExtensions.cs index a596759eba..0d6d4254f2 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Extensions/IConfigurationBuilderExtensions.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Extensions/IConfigurationBuilderExtensions.cs @@ -11,21 +11,10 @@ public static void AddClientConfigurations(this IConfigurationBuilder builder) var assembly = Assembly.Load("Boilerplate.Client.Core"); builder.AddJsonStream(assembly.GetManifestResourceStream("Boilerplate.Client.Core.appsettings.json")!); - if (BuildConfiguration.IsDebug()) + var settings = assembly.GetManifestResourceStream($"Boilerplate.Client.Core.appsettings.{AppEnvironment.Name}.json"); + if (settings is not null) { - var settings = assembly.GetManifestResourceStream("Boilerplate.Client.Core.appsettings.Debug.json"); - if (settings is not null) - { - builder.AddJsonStream(settings); - } - } - else - { - var settings = assembly.GetManifestResourceStream("Boilerplate.Client.Core.appsettings.Release.json"); - if (settings is not null) - { - builder.AddJsonStream(settings); - } + builder.AddJsonStream(settings); } } } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Extensions/IServiceCollectionExtensions.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Extensions/IServiceCollectionExtensions.cs index da674839eb..0c2d352d14 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Extensions/IServiceCollectionExtensions.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Extensions/IServiceCollectionExtensions.cs @@ -56,8 +56,8 @@ public static IServiceCollection AddClientCoreProjectServices(this IServiceColle // .UseModel(OfflineDbContextModel.Instance) .UseSqlite($"Data Source={dbPath}"); - options.EnableSensitiveDataLogging(BuildConfiguration.IsDebug()) - .EnableDetailedErrors(BuildConfiguration.IsDebug()); + options.EnableSensitiveDataLogging(AppEnvironment.IsDevelopment()) + .EnableDetailedErrors(AppEnvironment.IsDevelopment()); }); //#endif diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/AppRenderMode.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/AppRenderMode.cs index 84a0fd2ece..70d9f333bb 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/AppRenderMode.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/AppRenderMode.cs @@ -13,7 +13,7 @@ public static class AppRenderMode public static IComponentRenderMode NoPrerenderBlazorWebAssembly => new InteractiveWebAssemblyRenderMode(prerender: false); public static IComponentRenderMode Current => - BuildConfiguration.IsDebug() + AppEnvironment.IsDevelopment() ? BlazorServer // For better development experience. : Auto; // For better production experience. diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/AuthenticationManager.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/AuthenticationManager.cs index e87f3c353a..44b8944f96 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/AuthenticationManager.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/AuthenticationManager.cs @@ -114,7 +114,7 @@ await cookie.Set(new() Value = response.AccessToken, MaxAge = rememberMe is true ? response.ExpiresIn : null, // to create a session cookie SameSite = SameSite.Strict, - Secure = BuildConfiguration.IsRelease() + Secure = AppEnvironment.IsDevelopment() is false }); } } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/ExceptionHandlerBase.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/ExceptionHandlerBase.cs index b254951654..b02f063a36 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/ExceptionHandlerBase.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/ExceptionHandlerBase.cs @@ -24,7 +24,7 @@ public void Handle(Exception exp, IDictionary? parameters = nul protected virtual void Handle(Exception exception, Dictionary parameters) { - var isDebug = BuildConfiguration.IsDebug(); + var isDebug = AppEnvironment.IsDevelopment(); string exceptionMessage = (exception as KnownException)?.Message ?? (isDebug ? exception.ToString() : Localizer[nameof(AppStrings.UnknownException)]); diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/appsettings.Debug.json b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/appsettings.Development.json similarity index 100% rename from src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/appsettings.Debug.json rename to src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/appsettings.Development.json diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/appsettings.Release.json b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/appsettings.Production.json similarity index 100% rename from src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/appsettings.Release.json rename to src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/appsettings.Production.json diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Components/Pages/AboutPage.razor b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Components/Pages/AboutPage.razor index ab82bba82b..be6b776eb6 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Components/Pages/AboutPage.razor +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Components/Pages/AboutPage.razor @@ -22,6 +22,18 @@ App Version: @appVersion + + OS: + @os + + + OEM: + @oem + + + Environment: + @AppEnvironment.Name + Process Id: @processId diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Components/Pages/AboutPage.razor.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Components/Pages/AboutPage.razor.cs index 197ac42c4d..1587f509a9 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Components/Pages/AboutPage.razor.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Components/Pages/AboutPage.razor.cs @@ -7,6 +7,8 @@ public partial class AboutPage private string appName = default!; private string appVersion = default!; private string processId = default!; + private string os = default!; + private string oem = default!; protected async override Task OnInitAsync() { @@ -20,6 +22,8 @@ protected async override Task OnInitAsync() appVersion = AppInfo.Version.ToString(); #endif processId = Environment.ProcessId.ToString(); + os = $"{DeviceInfo.Current.Platform} {DeviceInfo.Current.VersionString}"; + oem = DeviceInfo.Current.Manufacturer; await base.OnInitAsync(); } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MainPage.xaml.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MainPage.xaml.cs index 2ed20ee349..cc9f5b5426 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MainPage.xaml.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MainPage.xaml.cs @@ -76,7 +76,7 @@ private async Task CheckForUpdates() } } } - catch (InvalidOperationException) when ((OperatingSystem.IsIOS() || OperatingSystem.IsMacCatalyst() || OperatingSystem.IsMacOS()) && BuildConfiguration.IsDebug()) { } + catch (InvalidOperationException) when ((OperatingSystem.IsIOS() || OperatingSystem.IsMacCatalyst() || OperatingSystem.IsMacOS()) && AppEnvironment.IsDevelopment()) { } catch (FileNotFoundException) { } } } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.Services.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.Services.cs index 4d499aed06..f6496eee75 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.Services.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.Services.cs @@ -24,7 +24,7 @@ public static void ConfigureServices(this MauiAppBuilder builder) services.AddMauiBlazorWebView(); - if (BuildConfiguration.IsDebug()) + if (AppEnvironment.IsDevelopment()) { services.AddBlazorWebViewDeveloperTools(); } @@ -43,7 +43,7 @@ public static void ConfigureServices(this MauiAppBuilder builder) builder.Logging.AddConfiguration(configuration.GetSection("Logging")); - if (BuildConfiguration.IsDebug()) + if (AppEnvironment.IsDevelopment()) { builder.Logging.AddDebug(); } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.cs index 525754446c..7402f9b144 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/MauiProgram.cs @@ -95,7 +95,7 @@ private static void SetupBlazorWebView() webView.DefaultBackgroundColor = Microsoft.UI.Colors.Black; } - if (BuildConfiguration.IsRelease()) + if (AppEnvironment.IsDevelopment() is false) { webView.EnsureCoreWebView2Async() .AsTask() @@ -118,7 +118,7 @@ private static void SetupBlazorWebView() webView.ScrollView.Bounces = false; webView.Opaque = false; - if (BuildConfiguration.IsDebug()) + if (AppEnvironment.IsDevelopment()) { if ((DeviceInfo.Current.Platform == DevicePlatform.MacCatalyst && DeviceInfo.Current.Version >= new Version(13, 3)) || (DeviceInfo.Current.Platform == DevicePlatform.iOS && DeviceInfo.Current.Version >= new Version(16, 4))) @@ -143,7 +143,7 @@ private static void SetupBlazorWebView() settings.JavaScriptCanOpenWindowsAutomatically = settings.DomStorageEnabled = true; - if (BuildConfiguration.IsDebug()) + if (AppEnvironment.IsDevelopment()) { settings.MixedContentMode = Android.Webkit.MixedContentHandling.AlwaysAllow; } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Services/MauiLocalHttpServer.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Services/MauiLocalHttpServer.cs index e9281060db..16e54a1222 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Services/MauiLocalHttpServer.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Maui/Services/MauiLocalHttpServer.cs @@ -30,7 +30,7 @@ private async Task StartImplementation() { ApplicationName = "LocalHttpServer", ContentRootPath = Directory.GetCurrentDirectory(), - EnvironmentName = BuildConfiguration.IsDebug() ? Environments.Development : Environments.Production, + EnvironmentName = AppEnvironment.Name, WebRootPath = Path.Combine(AppContext.BaseDirectory, "wwwroot") }); diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/Program.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/Program.cs index d66f9089fb..7b960fde0d 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/Program.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Web/Program.cs @@ -15,6 +15,8 @@ public static async Task Main(string[] args) { var builder = WebAssemblyHostBuilder.CreateDefault(args); + AppEnvironment.Name = builder.HostEnvironment.Environment; + #if BlazorWebAssemblyStandalone builder.RootComponents.Add("#app-container"); builder.RootComponents.Add("head::after"); diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Components/Pages/AboutPage.razor b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Components/Pages/AboutPage.razor index ab82bba82b..c25dbf845c 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Components/Pages/AboutPage.razor +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Components/Pages/AboutPage.razor @@ -22,6 +22,14 @@ App Version: @appVersion + + OS: + @os + + + Environment: + @AppEnvironment.Name + Process Id: @processId diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Components/Pages/AboutPage.razor.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Components/Pages/AboutPage.razor.cs index 77cb5e13ff..8dc779bdb2 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Components/Pages/AboutPage.razor.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Components/Pages/AboutPage.razor.cs @@ -1,4 +1,5 @@ using System.Reflection; +using System.Runtime.InteropServices; namespace Boilerplate.Client.Windows.Components.Pages; @@ -6,6 +7,7 @@ public partial class AboutPage { private string appName = default!; private string appVersion = default!; + private string os = default!; private string processId = default!; protected async override Task OnInitAsync() @@ -13,6 +15,7 @@ protected async override Task OnInitAsync() var asm = typeof(AboutPage).Assembly; appName = asm.GetCustomAttribute()!.Title; appVersion = asm.GetName().Version!.ToString(); + os = RuntimeInformation.OSDescription; processId = Environment.ProcessId.ToString(); await base.OnInitAsync(); diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/MainWindow.xaml.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/MainWindow.xaml.cs index aac2f11fe0..70144eda42 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/MainWindow.xaml.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/MainWindow.xaml.cs @@ -24,7 +24,7 @@ public MainWindow() { await BlazorWebView.WebView.EnsureCoreWebView2Async(); var settings = BlazorWebView.WebView.CoreWebView2.Settings; - if (BuildConfiguration.IsRelease()) + if (AppEnvironment.IsDevelopment() is false) { settings.IsZoomControlEnabled = false; settings.AreBrowserAcceleratorKeysEnabled = false; diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Program.Services.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Program.Services.cs index 799102ab04..a244443274 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Program.Services.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Program.Services.cs @@ -28,7 +28,7 @@ public static void ConfigureServices(this IServiceCollection services) }); services.AddWpfBlazorWebView(); - if (BuildConfiguration.IsDebug()) + if (AppEnvironment.IsDevelopment()) { services.AddBlazorWebViewDeveloperTools(); } @@ -43,7 +43,7 @@ public static void ConfigureServices(this IServiceCollection services) loggingBuilder.AddConfiguration(configuration.GetSection("Logging")); loggingBuilder.AddEventLog(); loggingBuilder.AddEventSourceLogger(); - if (BuildConfiguration.IsDebug()) + if (AppEnvironment.IsDevelopment()) { loggingBuilder.AddDebug(); } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Services/WindowsLocalHttpServer.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Services/WindowsLocalHttpServer.cs index 121989c24b..3a93ee653f 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Services/WindowsLocalHttpServer.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Windows/Services/WindowsLocalHttpServer.cs @@ -29,7 +29,7 @@ private async Task StartImplementation() { ApplicationName = "LocalHttpServer", ContentRootPath = Directory.GetCurrentDirectory(), - EnvironmentName = BuildConfiguration.IsDebug() ? Environments.Development : Environments.Production, + EnvironmentName = AppEnvironment.Name, WebRootPath = Path.Combine(AppContext.BaseDirectory, "wwwroot") }); diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Directory.Build.props b/src/Templates/Boilerplate/Bit.Boilerplate/src/Directory.Build.props index 23c5bd46a0..c1a2129bf9 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Directory.Build.props +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Directory.Build.props @@ -21,6 +21,10 @@ true + + Development + Production + 14.0 14.0 24.0 @@ -34,6 +38,7 @@ $(DefineConstants);MultilingualEnabled $(DefineConstants);BlazorWebAssemblyStandalone $(DefineConstants);PwaEnabled + $(DefineConstants);$(Environment) diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Program.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Program.cs index 29d728696c..ff8f863e25 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Program.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Program.cs @@ -8,10 +8,12 @@ public static async Task Main(string[] args) { var builder = WebApplication.CreateBuilder(args); + AppEnvironment.Name = builder.Environment.EnvironmentName; + builder.Configuration.AddSharedConfigurations(); // The following line (using the * in the URL), allows the emulators and mobile devices to access the app using the host IP address. - if (BuildConfiguration.IsDebug() && OperatingSystem.IsWindows()) + if (AppEnvironment.IsDevelopment() && OperatingSystem.IsWindows()) { builder.WebHost.UseUrls("http://localhost:5031", "http://*:5031"); } @@ -21,7 +23,7 @@ public static async Task Main(string[] args) var app = builder.Build(); - if (BuildConfiguration.IsDebug()) + if (AppEnvironment.IsDevelopment()) { await using var scope = app.Services.CreateAsyncScope(); var dbContext = scope.ServiceProvider.GetRequiredService(); diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/AppSecureJwtDataFormat.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/AppSecureJwtDataFormat.cs index 2e649bc5b3..379d150127 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/AppSecureJwtDataFormat.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Api/Services/AppSecureJwtDataFormat.cs @@ -32,7 +32,7 @@ public class AppSecureJwtDataFormat(AppSettings appSettings, TokenValidationPara } catch (Exception ex) { - if (BuildConfiguration.IsDebug()) + if (AppEnvironment.IsDevelopment()) { Console.WriteLine(ex); // since we do not have access to any logger at this point! } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Services.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Services.cs index cd64c7dd49..10551894fc 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Services.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.Services.cs @@ -14,7 +14,6 @@ private static void ConfigureServices(this WebApplicationBuilder builder) var services = builder.Services; var configuration = builder.Configuration; - var env = builder.Environment; AddBlazor(builder); diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.cs index 7983bc286c..088bc9f8d8 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Server/Boilerplate.Server.Web/Program.cs @@ -14,10 +14,12 @@ public static async Task Main(string[] args) ContentRootPath = AppContext.BaseDirectory }); + AppEnvironment.Name = builder.Environment.EnvironmentName; + builder.Configuration.AddClientConfigurations(); // The following line (using the * in the URL), allows the emulators and mobile devices to access the app using the host IP address. - if (BuildConfiguration.IsDebug() && OperatingSystem.IsWindows()) + if (AppEnvironment.IsDevelopment() && OperatingSystem.IsWindows()) { builder.WebHost.UseUrls("http://localhost:5030", "http://*:5030"); } @@ -26,7 +28,7 @@ public static async Task Main(string[] args) var app = builder.Build(); - if (BuildConfiguration.IsDebug()) + if (AppEnvironment.IsDevelopment()) { await using var scope = app.Services.CreateAsyncScope(); var dbContext = scope.ServiceProvider.GetRequiredService(); diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Extensions/IConfigurationBuilderExtensions.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Extensions/IConfigurationBuilderExtensions.cs index 9392f801f2..cfe3508811 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Extensions/IConfigurationBuilderExtensions.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Extensions/IConfigurationBuilderExtensions.cs @@ -9,21 +9,10 @@ public static void AddSharedConfigurations(this IConfigurationBuilder builder) var assembly = Assembly.Load("Boilerplate.Shared"); builder.AddJsonStream(assembly.GetManifestResourceStream("Boilerplate.Shared.appsettings.json")!); - if (BuildConfiguration.IsDebug()) + var settings = assembly.GetManifestResourceStream($"Boilerplate.Shared.appsettings.{AppEnvironment.Name}.json"); + if (settings is not null) { - var settings = assembly.GetManifestResourceStream("Boilerplate.Shared.appsettings.Debug.json"); - if (settings is not null) - { - builder.AddJsonStream(settings); - } - } - else - { - var settings = assembly.GetManifestResourceStream("Boilerplate.Shared.appsettings.Release.json"); - if (settings is not null) - { - builder.AddJsonStream(settings); - } + builder.AddJsonStream(settings); } } } diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Extensions/IConfigurationExtensions.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Extensions/IConfigurationExtensions.cs index 34d31f99fe..180c9a52e3 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Extensions/IConfigurationExtensions.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Extensions/IConfigurationExtensions.cs @@ -6,7 +6,7 @@ public static string GetServerAddress(this IConfiguration configuration) { var serverAddress = configuration.GetValue("ServerAddress", defaultValue: "/")!; - if (BuildConfiguration.IsDebug() && + if (AppEnvironment.IsDevelopment() && serverAddress.Contains("localhost", StringComparison.InvariantCultureIgnoreCase) && OperatingSystem.IsAndroid()) { diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Services/AppEnvironment.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Services/AppEnvironment.cs new file mode 100644 index 0000000000..42db4dfd09 --- /dev/null +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Services/AppEnvironment.cs @@ -0,0 +1,42 @@ +//-:cnd:noEmit +namespace Boilerplate.Shared.Services; + +/// +/// Unlike ASP.NET Core, which allows environment configuration via environment variables, +/// Android, iOS, Windows, and macOS do not support the exact same concept. +/// To maintain consistency, we introduced . +/// The environment name is synchronized with ASP.NET Core environment's name in the API, +/// Blazor Server, and Blazor WebAssembly (WASM). +/// Additionally, in Blazor Hybrid, it stays in sync with the build configuration (Debug, Release). +/// +public static class AppEnvironment +{ + public static string Name { get; set; } = +#if Development // dotnet publish -c Debug + "Development"; +#elif Staging // dotnet publish -c Release -p:Environment=Staging + "Staging"; +#else // dotnet publish -c Release + "Production"; +#endif + + public static bool IsDevelopment() + { + return Name == "Development"; + } + + public static bool IsProduction() + { + return Name == "Production"; + } + + public static bool IsStaging() + { + return Name == "Staging"; + } + + public static bool IsEnvironment(string name) + { + return Name == name; + } +} diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Services/BuildConfiguration.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Services/BuildConfiguration.cs deleted file mode 100644 index 26b0d55a63..0000000000 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Services/BuildConfiguration.cs +++ /dev/null @@ -1,24 +0,0 @@ -//-:cnd:noEmit -namespace Boilerplate.Shared.Services; - -public static class BuildConfiguration -{ - public static bool IsDebug() - { -#if DEBUG - return true; -#else - return false; -#endif - } - - - public static bool IsRelease() - { -#if DEBUG - return false; -#else - return true; -#endif - } -} diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/appsettings.Debug.json b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/appsettings.Development.json similarity index 100% rename from src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/appsettings.Debug.json rename to src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/appsettings.Development.json diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/appsettings.Release.json b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/appsettings.Production.json similarity index 100% rename from src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/appsettings.Release.json rename to src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/appsettings.Production.json