dotnet / dotnet/aspnetcore

DeferredHostBuilder starts the host during the build action

Open
#38,335 14 comments 0 reactions 0 assignees View on GitHub
area-hosting area-networking investigate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.