Skip to content

WebApplicationFactory<T> does not default to Development environment with minimal hosting #33889

@martincostello

Description

@martincostello

Describe the bug

Compared to the use cases for IHostBuilder and IWebHostBuilder, WebApplicationFactory<T> does not set the default environment for the application under test to Development when the code path for minimal hosting is used.

hostBuilder?.UseEnvironment(Environments.Development);

return builder.UseEnvironment(Environments.Development);

var hostBuilder = CreateHostBuilder();
if (hostBuilder is not null)
{
ConfigureHostBuilder(hostBuilder);
return;
}
var builder = CreateWebHostBuilder();
if (builder is null)
{
var deferredHostBuilder = new DeferredHostBuilder();
// This helper call does the hard work to determine if we can fallback to diagnostic source events to get the host instance
var factory = HostFactoryResolver.ResolveHostFactory(
typeof(TEntryPoint).Assembly,
stopApplication: false,
configureHostBuilder: deferredHostBuilder.ConfigureHostBuilder,
entrypointCompleted: deferredHostBuilder.EntryPointCompleted);
if (factory is not null)
{
// If we have a valid factory it means the specified entry point's assembly can potentially resolve the IHost
// so we set the factory on the DeferredHostBuilder so we can invoke it on the call to IHostBuilder.Build.
deferredHostBuilder.SetHostFactory(factory);
ConfigureHostBuilder(deferredHostBuilder);
return;
}

Adding the missing line itself does not seem to work though, possibly related to #33876 - I've got some tests from debugging this locally I'll push up into a PR shortly see #33890.

To Reproduce

Inspect the IHostEnvironment.EnvironmentName property in, for example, the HelloWorld test.

[Fact]
public async Task HelloWorld()
{
// Arrange
var expected = "Hello World";
// Act
var content = await Client.GetStringAsync("http://localhost/");
// Assert
Assert.Equal(expected, content);
}

Further technical details

  • ASP.NET Core 6.0.0-preview.6.21323.4
  • .NET SDK 6.0.100-preview.6.21324.1
  • Visual Studio 2022 17.0.0 Preview 1.1

Metadata

Metadata

Assignees

Labels

Priority:0Work that we can't release withoutarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcbugThis issue describes a behavior which is not expected - a bug.feature-minimal-hostinginvestigate

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions