diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/Extensions/DependencyInjection/AbpWebAssemblyServiceCollectionExtensions.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/Extensions/DependencyInjection/AbpWebAssemblyServiceCollectionExtensions.cs index bbcf1d5a83..bb9f0774bc 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/Extensions/DependencyInjection/AbpWebAssemblyServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/Extensions/DependencyInjection/AbpWebAssemblyServiceCollectionExtensions.cs @@ -1,16 +1,31 @@ using System.Diagnostics.CodeAnalysis; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; +using Microsoft.Extensions.Hosting; using Volo.Abp; namespace Microsoft.Extensions.DependencyInjection; public static class AbpWebAssemblyServiceCollectionExtensions { - public static WebAssemblyHostBuilder GetHostBuilder( - [NotNull] this IServiceCollection services) + public static WebAssemblyHostBuilder GetHostBuilder([NotNull] this IServiceCollection services) { Check.NotNull(services, nameof(services)); return services.GetSingletonInstance(); } + + public static IWebAssemblyHostEnvironment GetWebAssemblyHostEnvironment(this IServiceCollection services) + { + var webAssemblyHostEnvironment = services.GetSingletonInstanceOrNull(); + + if (webAssemblyHostEnvironment == null) + { + return new EmptyWebAssemblyHostEnvironment() + { + Environment = Environments.Development + }; + } + + return webAssemblyHostEnvironment; + } } diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/Extensions/DependencyInjection/EmptyWebAssemblyHostEnvironment.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/Extensions/DependencyInjection/EmptyWebAssemblyHostEnvironment.cs new file mode 100644 index 0000000000..6393948af7 --- /dev/null +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/Extensions/DependencyInjection/EmptyWebAssemblyHostEnvironment.cs @@ -0,0 +1,10 @@ +using Microsoft.AspNetCore.Components.WebAssembly.Hosting; + +namespace Microsoft.Extensions.DependencyInjection; + +public class EmptyWebAssemblyHostEnvironment : IWebAssemblyHostEnvironment +{ + public string Environment { get; set; } + + public string BaseAddress { get; set; } +} diff --git a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpAspNetCoreComponentsWebAssemblyModule.cs b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpAspNetCoreComponentsWebAssemblyModule.cs index 229831141e..2df5733a99 100644 --- a/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpAspNetCoreComponentsWebAssemblyModule.cs +++ b/framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpAspNetCoreComponentsWebAssemblyModule.cs @@ -35,6 +35,12 @@ public class AbpAspNetCoreComponentsWebAssemblyModule : AbpModule public override void ConfigureServices(ServiceConfigurationContext context) { + var abpHostEnvironment = context.Services.GetSingletonInstance(); + if (abpHostEnvironment.EnvironmentName.IsNullOrWhiteSpace()) + { + abpHostEnvironment.EnvironmentName = context.Services.GetWebAssemblyHostEnvironment().Environment; + } + context.Services .GetHostBuilder().Logging .AddProvider(new AbpExceptionHandlingLoggerProvider(context.Services)); diff --git a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs index 9f613122c8..8cef208bc5 100644 --- a/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs +++ b/framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs @@ -1,10 +1,8 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Hosting; +using System; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.RequestLocalization; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.Extensions.FileProviders; -using Microsoft.Extensions.Options; using Volo.Abp.AspNetCore.Auditing; using Volo.Abp.AspNetCore.VirtualFileSystem; using Volo.Abp.Auditing; @@ -33,6 +31,12 @@ public class AbpAspNetCoreModule : AbpModule { public override void ConfigureServices(ServiceConfigurationContext context) { + var abpHostEnvironment = context.Services.GetSingletonInstance(); + if (abpHostEnvironment.EnvironmentName.IsNullOrWhiteSpace()) + { + abpHostEnvironment.EnvironmentName = context.Services.GetHostingEnvironment().EnvironmentName; + } + context.Services.AddAuthorization(); Configure(options => diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs index f9fd7676a7..24ae3a6995 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs @@ -54,9 +54,7 @@ public abstract class AbpApplicationBase : IAbpApplication services.AddSingleton(this); services.AddSingleton(new AbpHostEnvironment() { - EnvironmentName = options.Environment.IsNullOrWhiteSpace() - ? Environments.Production - : options.Environment + EnvironmentName = options.Environment }); services.AddCoreServices(); @@ -68,6 +66,12 @@ public abstract class AbpApplicationBase : IAbpApplication { ConfigureServices(); } + + var abpHostEnvironment = services.GetSingletonInstance(); + if (abpHostEnvironment.EnvironmentName.IsNullOrWhiteSpace()) + { + abpHostEnvironment.EnvironmentName = Environments.Production; + } } public virtual async Task ShutdownAsync() diff --git a/framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpHostEnvironment_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpHostEnvironment_Tests.cs new file mode 100644 index 0000000000..42f8a43cc6 --- /dev/null +++ b/framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpHostEnvironment_Tests.cs @@ -0,0 +1,31 @@ +using System.Collections.Generic; +using Microsoft.Extensions.Configuration.Memory; +using Microsoft.Extensions.Hosting; +using Shouldly; +using Xunit; + +namespace Volo.Abp.AspNetCore; + +public class AbpHostEnvironment_Tests : AbpAspNetCoreTestBase +{ + protected override IHostBuilder CreateHostBuilder() + { + var builder = base.CreateHostBuilder(); + builder.ConfigureHostConfiguration(x => x.Sources.Insert(0, + new MemoryConfigurationSource() + { + InitialData = new List> + { + new(HostDefaults.EnvironmentKey, Environments.Staging), + } + })); + return builder; + } + + [Fact] + public void Should_Set_Environment_From_IWebHostEnvironment() + { + var abpHostEnvironment = GetRequiredService(); + abpHostEnvironment.EnvironmentName.ShouldBe(Environments.Staging); + } +}