Skip to content

Spruce up WebAssemblyHostEnvironment interface and use #20008

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ internal WebAssemblyHostBuilder(WebAssemblyJSRuntimeInvoker jsRuntimeInvoker)
InitializeDefaultServices();

var hostEnvironment = InitializeEnvironment(jsRuntimeInvoker);
HostEnvironment = hostEnvironment;

_createServiceProvider = () =>
{
return Services.BuildServiceProvider(validateScopes: hostEnvironment.Environment == "Development");
return Services.BuildServiceProvider(validateScopes: WebAssemblyHostEnvironmentExtensions.IsDevelopment(hostEnvironment));
};
}

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

/// <summary>
/// Gets information about the app's host environment.
/// </summary>
public IWebAssemblyHostEnvironment HostEnvironment { get; }

/// <summary>
/// Registers a <see cref="IServiceProviderFactory{TBuilder}" /> instance to be used to create the <see cref="IServiceProvider" />.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ public void Build_InProduction_ConfiguresWithServiceProviderWithScopeValidation(
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
{
public TestServiceThatTakesStringBuilder(StringBuilder builder) { }
Expand Down