mirror of https://github.com/abpframework/abp.git
27 changed files with 349 additions and 48 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,16 @@ |
|||||
|
using System; |
||||
|
|
||||
|
namespace Volo.Abp.AspNetCore.Mvc.Client; |
||||
|
|
||||
|
public class AbpAspNetCoreMvcClientCacheOptions |
||||
|
{ |
||||
|
public TimeSpan TenantConfigurationCacheAbsoluteExpiration { get; set; } |
||||
|
|
||||
|
public TimeSpan ApplicationConfigurationDtoCacheAbsoluteExpiration { get; set; } |
||||
|
|
||||
|
public AbpAspNetCoreMvcClientCacheOptions() |
||||
|
{ |
||||
|
TenantConfigurationCacheAbsoluteExpiration = TimeSpan.FromMinutes(5); |
||||
|
ApplicationConfigurationDtoCacheAbsoluteExpiration = TimeSpan.FromSeconds(300); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,6 @@ |
|||||
|
namespace Volo.Abp; |
||||
|
|
||||
|
public class AbpHostEnvironment : IAbpHostEnvironment |
||||
|
{ |
||||
|
public string EnvironmentName { get; set; } |
||||
|
} |
||||
@ -0,0 +1,38 @@ |
|||||
|
using System; |
||||
|
using Microsoft.Extensions.Hosting; |
||||
|
|
||||
|
namespace Volo.Abp; |
||||
|
|
||||
|
public static class AbpHostEnvironmentExtensions |
||||
|
{ |
||||
|
public static bool IsDevelopment(this IAbpHostEnvironment hostEnvironment) |
||||
|
{ |
||||
|
Check.NotNull(hostEnvironment, nameof(hostEnvironment)); |
||||
|
|
||||
|
return hostEnvironment.IsEnvironment(Environments.Development); |
||||
|
} |
||||
|
|
||||
|
public static bool IsStaging(this IAbpHostEnvironment hostEnvironment) |
||||
|
{ |
||||
|
Check.NotNull(hostEnvironment, nameof(hostEnvironment)); |
||||
|
|
||||
|
return hostEnvironment.IsEnvironment(Environments.Staging); |
||||
|
} |
||||
|
|
||||
|
public static bool IsProduction(this IAbpHostEnvironment hostEnvironment) |
||||
|
{ |
||||
|
Check.NotNull(hostEnvironment, nameof(hostEnvironment)); |
||||
|
|
||||
|
return hostEnvironment.IsEnvironment(Environments.Production); |
||||
|
} |
||||
|
|
||||
|
public static bool IsEnvironment(this IAbpHostEnvironment hostEnvironment, string environmentName) |
||||
|
{ |
||||
|
Check.NotNull(hostEnvironment, nameof(hostEnvironment)); |
||||
|
|
||||
|
return string.Equals( |
||||
|
hostEnvironment.EnvironmentName, |
||||
|
environmentName, |
||||
|
StringComparison.OrdinalIgnoreCase); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,6 @@ |
|||||
|
namespace Volo.Abp; |
||||
|
|
||||
|
public interface IAbpHostEnvironment |
||||
|
{ |
||||
|
string EnvironmentName { get; set; } |
||||
|
} |
||||
@ -0,0 +1,65 @@ |
|||||
|
using System.Collections.Generic; |
||||
|
using System.Threading.Tasks; |
||||
|
using Microsoft.AspNetCore.Builder; |
||||
|
using Microsoft.Extensions.Configuration.Memory; |
||||
|
using Microsoft.Extensions.DependencyInjection; |
||||
|
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); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public class AbpHostEnvironment_Async_Initialize_Tests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public async Task Should_Set_Environment_From_AspNetCore() |
||||
|
{ |
||||
|
var builder = WebApplication.CreateBuilder(new WebApplicationOptions |
||||
|
{ |
||||
|
EnvironmentName = Environments.Staging |
||||
|
}); |
||||
|
builder.Host.UseAutofac(); |
||||
|
await builder.AddApplicationAsync<AbpAspNetCoreTestModule>(); |
||||
|
var app = builder.Build(); |
||||
|
await app.InitializeApplicationAsync(); |
||||
|
|
||||
|
var abpHostEnvironment = app.Services.GetRequiredService<IAbpHostEnvironment>(); |
||||
|
abpHostEnvironment.EnvironmentName.ShouldBe(Environments.Staging); |
||||
|
|
||||
|
builder = WebApplication.CreateBuilder(new WebApplicationOptions |
||||
|
{ |
||||
|
EnvironmentName = Environments.Staging |
||||
|
}); |
||||
|
builder.Host.UseAutofac(); |
||||
|
var abpApp = await AbpApplicationFactory.CreateAsync<AbpAspNetCoreTestModule>(builder.Services); |
||||
|
app = builder.Build(); |
||||
|
await app.InitializeApplicationAsync(); |
||||
|
|
||||
|
abpHostEnvironment = abpApp.Services.GetRequiredService<IAbpHostEnvironment>(); |
||||
|
abpHostEnvironment.EnvironmentName.ShouldBe(Environments.Staging); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue