Spruce up WebAssemblyHostEnvironment interface and use (#20008)
This commit is contained in:
parent
c7d3c1f36a
commit
4b3608371f
|
|
@ -58,10 +58,11 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting
|
||||||
InitializeDefaultServices();
|
InitializeDefaultServices();
|
||||||
|
|
||||||
var hostEnvironment = InitializeEnvironment(jsRuntimeInvoker);
|
var hostEnvironment = InitializeEnvironment(jsRuntimeInvoker);
|
||||||
|
HostEnvironment = hostEnvironment;
|
||||||
|
|
||||||
_createServiceProvider = () =>
|
_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
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IServiceCollection Services { get; }
|
public IServiceCollection Services { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets information about the app's host environment.
|
||||||
|
/// </summary>
|
||||||
|
public IWebAssemblyHostEnvironment HostEnvironment { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Registers a <see cref="IServiceProviderFactory{TBuilder}" /> instance to be used to create the <see cref="IServiceProvider" />.
|
/// Registers a <see cref="IServiceProviderFactory{TBuilder}" /> instance to be used to create the <see cref="IServiceProvider" />.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,75 @@
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting
|
||||||
|
{
|
||||||
|
public static class WebAssemblyHostEnvironmentExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if the current hosting environment name is <see cref="EnvironmentName.Development"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostingEnvironment">An instance of <see cref="IWebAssemblyHostEnvironment"/>.</param>
|
||||||
|
/// <returns>True if the environment name is <see cref="EnvironmentName.Development"/>, otherwise false.</returns>
|
||||||
|
public static bool IsDevelopment(this IWebAssemblyHostEnvironment hostingEnvironment)
|
||||||
|
{
|
||||||
|
if (hostingEnvironment == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(hostingEnvironment));
|
||||||
|
}
|
||||||
|
|
||||||
|
return hostingEnvironment.IsEnvironment("Development");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if the current hosting environment name is <see cref="EnvironmentName.Staging"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostingEnvironment">An instance of <see cref="IWebAssemblyHostEnvironment"/>.</param>
|
||||||
|
/// <returns>True if the environment name is <see cref="EnvironmentName.Staging"/>, otherwise false.</returns>
|
||||||
|
public static bool IsStaging(this IWebAssemblyHostEnvironment hostingEnvironment)
|
||||||
|
{
|
||||||
|
if (hostingEnvironment == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(hostingEnvironment));
|
||||||
|
}
|
||||||
|
|
||||||
|
return hostingEnvironment.IsEnvironment("Staging");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks if the current hosting environment name is <see cref="EnvironmentName.Production"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostingEnvironment">An instance of <see cref="IWebAssemblyHostEnvironment"/>.</param>
|
||||||
|
/// <returns>True if the environment name is <see cref="EnvironmentName.Production"/>, otherwise false.</returns>
|
||||||
|
public static bool IsProduction(this IWebAssemblyHostEnvironment hostingEnvironment)
|
||||||
|
{
|
||||||
|
if (hostingEnvironment == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(hostingEnvironment));
|
||||||
|
}
|
||||||
|
|
||||||
|
return hostingEnvironment.IsEnvironment("Production");
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Compares the current hosting environment name against the specified value.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="hostingEnvironment">An instance of <see cref="IWebAssemblyHostEnvironment"/>.</param>
|
||||||
|
/// <param name="environmentName">Environment name to validate against.</param>
|
||||||
|
/// <returns>True if the specified name is the same as the current environment, otherwise false.</returns>
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -126,6 +126,20 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting
|
||||||
Assert.NotNull(host.Services.GetRequiredService<TestServiceThatTakesStringBuilder>());
|
Assert.NotNull(host.Services.GetRequiredService<TestServiceThatTakesStringBuilder>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Builder_InDevelopment_SetsHostEnvironmentProperty()
|
||||||
|
{
|
||||||
|
// Arrange
|
||||||
|
var builder = new WebAssemblyHostBuilder(new TestWebAssemblyJSRuntimeInvoker(environment: "Development"));
|
||||||
|
|
||||||
|
builder.Services.AddScoped<StringBuilder>();
|
||||||
|
builder.Services.AddSingleton<TestServiceThatTakesStringBuilder>();
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.NotNull(builder.HostEnvironment);
|
||||||
|
Assert.True(WebAssemblyHostEnvironmentExtensions.IsDevelopment(builder.HostEnvironment));
|
||||||
|
}
|
||||||
|
|
||||||
private class TestServiceThatTakesStringBuilder
|
private class TestServiceThatTakesStringBuilder
|
||||||
{
|
{
|
||||||
public TestServiceThatTakesStringBuilder(StringBuilder builder) { }
|
public TestServiceThatTakesStringBuilder(StringBuilder builder) { }
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue