mirror of https://github.com/abpframework/abp.git
6 changed files with 79 additions and 9 deletions
@ -1,16 +1,31 @@ |
|||||
using System.Diagnostics.CodeAnalysis; |
using System.Diagnostics.CodeAnalysis; |
||||
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; |
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; |
||||
|
using Microsoft.Extensions.Hosting; |
||||
using Volo.Abp; |
using Volo.Abp; |
||||
|
|
||||
namespace Microsoft.Extensions.DependencyInjection; |
namespace Microsoft.Extensions.DependencyInjection; |
||||
|
|
||||
public static class AbpWebAssemblyServiceCollectionExtensions |
public static class AbpWebAssemblyServiceCollectionExtensions |
||||
{ |
{ |
||||
public static WebAssemblyHostBuilder GetHostBuilder( |
public static WebAssemblyHostBuilder GetHostBuilder([NotNull] this IServiceCollection services) |
||||
[NotNull] this IServiceCollection services) |
|
||||
{ |
{ |
||||
Check.NotNull(services, nameof(services)); |
Check.NotNull(services, nameof(services)); |
||||
|
|
||||
return services.GetSingletonInstance<WebAssemblyHostBuilder>(); |
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; |
||||
|
} |
||||
} |
} |
||||
|
|||||
@ -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; } |
||||
|
} |
||||
@ -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…
Reference in new issue