Skip to content

DeferredHostBuilder starts the host during the build action #38335

Open
@Blackclaws

Description

@Blackclaws

Describe the bug

This issue is about a very hidden behaviour when DeferredHostBuilder is being used.

I came across this issue when fixing an issue with databases being seeded twice during tests:

https://github.com/ardalis/CleanArchitecture/pull/277/files

The reason is that in contrast to other IHostBuilder implementations DeferredHostBuilder actually does start the Host when calling:

IHostBuilder builder;
builder.Build();

Since this is also done on a separate thread this leads to race conditions in code that previously didn't have them. The issue comes up when switching to the new template for aspnetcore servers that don't use a Startup class

To Reproduce

  1. Create an aspnetcore 6.0 project without a Startup class
  2. Create a CustomWebApplicationFactory extending WebApplicationFactory
  3. Override CreateHost(IHostBuilder builder)
  4. Call builder.Build() then perform other things with the host's services
  5. Notice that the code in Program.cs is running in parallel and you might be getting race conditions now

Exceptions (if any)

Further technical details

  • ASP.NET Core version: 6.0.100
  • The IDE (VS / VS Code/ VS4Mac) you're running on, and its version: dotnet-cli 6.0.100

Metadata

Metadata

Assignees

Labels

area-hostingIncludes Hostingarea-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsinvestigate

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions