diff --git a/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs b/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs index 7af78d2035..1c4dc0036a 100644 --- a/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs +++ b/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs @@ -58,10 +58,11 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting InitializeDefaultServices(); var hostEnvironment = InitializeEnvironment(jsRuntimeInvoker); + HostEnvironment = hostEnvironment; _createServiceProvider = () => { - return Services.BuildServiceProvider(validateScopes: hostEnvironment.Environment == "Development"); + return Services.BuildServiceProvider(validateScopes: WebAssemblyHostEnvironmentExtensions.IsDevelopment(hostEnvironment)); }; } @@ -111,6 +112,11 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting /// public IServiceCollection Services { get; } + /// + /// Gets information about the app's host environment. + /// + public IWebAssemblyHostEnvironment HostEnvironment { get; } + /// /// Registers a instance to be used to create the . /// diff --git a/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostEnvironmentExtensions.cs b/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostEnvironmentExtensions.cs new file mode 100644 index 0000000000..257a55ef87 --- /dev/null +++ b/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostEnvironmentExtensions.cs @@ -0,0 +1,75 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting +{ + public static class WebAssemblyHostEnvironmentExtensions + { + /// + /// Checks if the current hosting environment name is . + /// + /// An instance of . + /// True if the environment name is , otherwise false. + public static bool IsDevelopment(this IWebAssemblyHostEnvironment hostingEnvironment) + { + if (hostingEnvironment == null) + { + throw new ArgumentNullException(nameof(hostingEnvironment)); + } + + return hostingEnvironment.IsEnvironment("Development"); + } + + /// + /// Checks if the current hosting environment name is . + /// + /// An instance of . + /// True if the environment name is , otherwise false. + public static bool IsStaging(this IWebAssemblyHostEnvironment hostingEnvironment) + { + if (hostingEnvironment == null) + { + throw new ArgumentNullException(nameof(hostingEnvironment)); + } + + return hostingEnvironment.IsEnvironment("Staging"); + } + + /// + /// Checks if the current hosting environment name is . + /// + /// An instance of . + /// True if the environment name is , otherwise false. + public static bool IsProduction(this IWebAssemblyHostEnvironment hostingEnvironment) + { + if (hostingEnvironment == null) + { + throw new ArgumentNullException(nameof(hostingEnvironment)); + } + + return hostingEnvironment.IsEnvironment("Production"); + } + + /// + /// Compares the current hosting environment name against the specified value. + /// + /// An instance of . + /// Environment name to validate against. + /// True if the specified name is the same as the current environment, otherwise false. + public static bool IsEnvironment( + this IWebAssemblyHostEnvironment hostingEnvironment, + string environmentName) + { + if (hostingEnvironment == null) + { + throw new ArgumentNullException(nameof(hostingEnvironment)); + } + + return string.Equals( + hostingEnvironment.Environment, + environmentName, + StringComparison.OrdinalIgnoreCase); + } + } +} diff --git a/src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyHostBuilderTest.cs b/src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyHostBuilderTest.cs index b003edafa7..7edc9c23e6 100644 --- a/src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyHostBuilderTest.cs +++ b/src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyHostBuilderTest.cs @@ -126,6 +126,20 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting Assert.NotNull(host.Services.GetRequiredService()); } + [Fact] + public void Builder_InDevelopment_SetsHostEnvironmentProperty() + { + // Arrange + var builder = new WebAssemblyHostBuilder(new TestWebAssemblyJSRuntimeInvoker(environment: "Development")); + + builder.Services.AddScoped(); + builder.Services.AddSingleton(); + + // Assert + Assert.NotNull(builder.HostEnvironment); + Assert.True(WebAssemblyHostEnvironmentExtensions.IsDevelopment(builder.HostEnvironment)); + } + private class TestServiceThatTakesStringBuilder { public TestServiceThatTakesStringBuilder(StringBuilder builder) { }