Skip to content

Commit 4b36083

Browse files
authored
Spruce up WebAssemblyHostEnvironment interface and use (#20008)
1 parent c7d3c1f commit 4b36083

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ internal WebAssemblyHostBuilder(WebAssemblyJSRuntimeInvoker jsRuntimeInvoker)
5858
InitializeDefaultServices();
5959

6060
var hostEnvironment = InitializeEnvironment(jsRuntimeInvoker);
61+
HostEnvironment = hostEnvironment;
6162

6263
_createServiceProvider = () =>
6364
{
64-
return Services.BuildServiceProvider(validateScopes: hostEnvironment.Environment == "Development");
65+
return Services.BuildServiceProvider(validateScopes: WebAssemblyHostEnvironmentExtensions.IsDevelopment(hostEnvironment));
6566
};
6667
}
6768

@@ -111,6 +112,11 @@ private WebAssemblyHostEnvironment InitializeEnvironment(WebAssemblyJSRuntimeInv
111112
/// </summary>
112113
public IServiceCollection Services { get; }
113114

115+
/// <summary>
116+
/// Gets information about the app's host environment.
117+
/// </summary>
118+
public IWebAssemblyHostEnvironment HostEnvironment { get; }
119+
114120
/// <summary>
115121
/// Registers a <see cref="IServiceProviderFactory{TBuilder}" /> instance to be used to create the <see cref="IServiceProvider" />.
116122
/// </summary>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting
6+
{
7+
public static class WebAssemblyHostEnvironmentExtensions
8+
{
9+
/// <summary>
10+
/// Checks if the current hosting environment name is <see cref="EnvironmentName.Development"/>.
11+
/// </summary>
12+
/// <param name="hostingEnvironment">An instance of <see cref="IWebAssemblyHostEnvironment"/>.</param>
13+
/// <returns>True if the environment name is <see cref="EnvironmentName.Development"/>, otherwise false.</returns>
14+
public static bool IsDevelopment(this IWebAssemblyHostEnvironment hostingEnvironment)
15+
{
16+
if (hostingEnvironment == null)
17+
{
18+
throw new ArgumentNullException(nameof(hostingEnvironment));
19+
}
20+
21+
return hostingEnvironment.IsEnvironment("Development");
22+
}
23+
24+
/// <summary>
25+
/// Checks if the current hosting environment name is <see cref="EnvironmentName.Staging"/>.
26+
/// </summary>
27+
/// <param name="hostingEnvironment">An instance of <see cref="IWebAssemblyHostEnvironment"/>.</param>
28+
/// <returns>True if the environment name is <see cref="EnvironmentName.Staging"/>, otherwise false.</returns>
29+
public static bool IsStaging(this IWebAssemblyHostEnvironment hostingEnvironment)
30+
{
31+
if (hostingEnvironment == null)
32+
{
33+
throw new ArgumentNullException(nameof(hostingEnvironment));
34+
}
35+
36+
return hostingEnvironment.IsEnvironment("Staging");
37+
}
38+
39+
/// <summary>
40+
/// Checks if the current hosting environment name is <see cref="EnvironmentName.Production"/>.
41+
/// </summary>
42+
/// <param name="hostingEnvironment">An instance of <see cref="IWebAssemblyHostEnvironment"/>.</param>
43+
/// <returns>True if the environment name is <see cref="EnvironmentName.Production"/>, otherwise false.</returns>
44+
public static bool IsProduction(this IWebAssemblyHostEnvironment hostingEnvironment)
45+
{
46+
if (hostingEnvironment == null)
47+
{
48+
throw new ArgumentNullException(nameof(hostingEnvironment));
49+
}
50+
51+
return hostingEnvironment.IsEnvironment("Production");
52+
}
53+
54+
/// <summary>
55+
/// Compares the current hosting environment name against the specified value.
56+
/// </summary>
57+
/// <param name="hostingEnvironment">An instance of <see cref="IWebAssemblyHostEnvironment"/>.</param>
58+
/// <param name="environmentName">Environment name to validate against.</param>
59+
/// <returns>True if the specified name is the same as the current environment, otherwise false.</returns>
60+
public static bool IsEnvironment(
61+
this IWebAssemblyHostEnvironment hostingEnvironment,
62+
string environmentName)
63+
{
64+
if (hostingEnvironment == null)
65+
{
66+
throw new ArgumentNullException(nameof(hostingEnvironment));
67+
}
68+
69+
return string.Equals(
70+
hostingEnvironment.Environment,
71+
environmentName,
72+
StringComparison.OrdinalIgnoreCase);
73+
}
74+
}
75+
}

src/Components/WebAssembly/WebAssembly/test/Hosting/WebAssemblyHostBuilderTest.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,20 @@ public void Build_InProduction_ConfiguresWithServiceProviderWithScopeValidation(
126126
Assert.NotNull(host.Services.GetRequiredService<TestServiceThatTakesStringBuilder>());
127127
}
128128

129+
[Fact]
130+
public void Builder_InDevelopment_SetsHostEnvironmentProperty()
131+
{
132+
// Arrange
133+
var builder = new WebAssemblyHostBuilder(new TestWebAssemblyJSRuntimeInvoker(environment: "Development"));
134+
135+
builder.Services.AddScoped<StringBuilder>();
136+
builder.Services.AddSingleton<TestServiceThatTakesStringBuilder>();
137+
138+
// Assert
139+
Assert.NotNull(builder.HostEnvironment);
140+
Assert.True(WebAssemblyHostEnvironmentExtensions.IsDevelopment(builder.HostEnvironment));
141+
}
142+
129143
private class TestServiceThatTakesStringBuilder
130144
{
131145
public TestServiceThatTakesStringBuilder(StringBuilder builder) { }

0 commit comments

Comments
 (0)