Skip to content

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

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

Closed
martincostello opened this issue Jun 27, 2021 · 1 comment · Fixed by #34794
Assignees
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc bug This issue describes a behavior which is not expected - a bug. feature-minimal-hosting investigate Priority:0 Work that we can't release without
Milestone

Comments

@martincostello
Copy link
Member

martincostello commented Jun 27, 2021

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
@BrennanConroy BrennanConroy added the bug This issue describes a behavior which is not expected - a bug. label Jun 28, 2021
@BrennanConroy BrennanConroy added this to the Next sprint planning milestone Jun 28, 2021
@ghost
Copy link

ghost commented Jun 28, 2021

Thanks for contacting us.

We're moving this issue to the Next sprint planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

martincostello added a commit to martincostello/aspnetcore that referenced this issue Jul 5, 2021
@rafikiassumani-msft rafikiassumani-msft added investigate Priority:0 Work that we can't release without labels Jul 20, 2021
@davidfowl davidfowl assigned davidfowl and unassigned halter73 Jul 28, 2021
@ghost ghost locked as resolved and limited conversation to collaborators Aug 27, 2021
@amcasey amcasey added area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed area-runtime labels Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc bug This issue describes a behavior which is not expected - a bug. feature-minimal-hosting investigate Priority:0 Work that we can't release without
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants