Browse Source

Set `EnvironmentName` from `HostEnvironment`.

pull/14842/head
maliming 3 years ago
parent
commit
f89dc3ab00
No known key found for this signature in database GPG Key ID: 96224957E51C89E
  1. 19
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/Extensions/DependencyInjection/AbpWebAssemblyServiceCollectionExtensions.cs
  2. 10
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Microsoft/Extensions/DependencyInjection/EmptyWebAssemblyHostEnvironment.cs
  3. 6
      framework/src/Volo.Abp.AspNetCore.Components.WebAssembly/Volo/Abp/AspNetCore/Components/WebAssembly/AbpAspNetCoreComponentsWebAssemblyModule.cs
  4. 12
      framework/src/Volo.Abp.AspNetCore/Volo/Abp/AspNetCore/AbpAspNetCoreModule.cs
  5. 10
      framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs
  6. 31
      framework/test/Volo.Abp.AspNetCore.Tests/Volo/Abp/AspNetCore/AbpHostEnvironment_Tests.cs

19
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<WebAssemblyHostBuilder>();
}
public static IWebAssemblyHostEnvironment GetWebAssemblyHostEnvironment(this IServiceCollection services)
{
var webAssemblyHostEnvironment = services.GetSingletonInstanceOrNull<IWebAssemblyHostEnvironment>();
if (webAssemblyHostEnvironment == null)
{
return new EmptyWebAssemblyHostEnvironment()
{
Environment = Environments.Development
};
}
return webAssemblyHostEnvironment;
}
}

10
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; }
}

6
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<IAbpHostEnvironment>();
if (abpHostEnvironment.EnvironmentName.IsNullOrWhiteSpace())
{
abpHostEnvironment.EnvironmentName = context.Services.GetWebAssemblyHostEnvironment().Environment;
}
context.Services
.GetHostBuilder().Logging
.AddProvider(new AbpExceptionHandlingLoggerProvider(context.Services));

12
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<IAbpHostEnvironment>();
if (abpHostEnvironment.EnvironmentName.IsNullOrWhiteSpace())
{
abpHostEnvironment.EnvironmentName = context.Services.GetHostingEnvironment().EnvironmentName;
}
context.Services.AddAuthorization();
Configure<AbpAuditingOptions>(options =>

10
framework/src/Volo.Abp.Core/Volo/Abp/AbpApplicationBase.cs

@ -54,9 +54,7 @@ public abstract class AbpApplicationBase : IAbpApplication
services.AddSingleton<IModuleContainer>(this);
services.AddSingleton<IAbpHostEnvironment>(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<IAbpHostEnvironment>();
if (abpHostEnvironment.EnvironmentName.IsNullOrWhiteSpace())
{
abpHostEnvironment.EnvironmentName = Environments.Production;
}
}
public virtual async Task ShutdownAsync()

31
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<Startup>
{
protected override IHostBuilder CreateHostBuilder()
{
var builder = base.CreateHostBuilder();
builder.ConfigureHostConfiguration(x => x.Sources.Insert(0,
new MemoryConfigurationSource()
{
InitialData = new List<KeyValuePair<string, string>>
{
new(HostDefaults.EnvironmentKey, Environments.Staging),
}
}));
return builder;
}
[Fact]
public void Should_Set_Environment_From_IWebHostEnvironment()
{
var abpHostEnvironment = GetRequiredService<IAbpHostEnvironment>();
abpHostEnvironment.EnvironmentName.ShouldBe(Environments.Staging);
}
}
Loading…
Cancel
Save