dotnet / dotnet/AspNetCore.Docs

Provide examples/samples on how to customize Host in ASP NET Core 6 Minimal APIs

Open
#26,175 6 comments 5 reactions 0 assignees View on GitHub
product-question re-Aditya
Dominant language
C#
Stars
13.1k
Forks
24.6k
Avg merge
1d 3h
Merged PRs (30d)
97

Description

**Help us make content visible**

- Keywords I tried: "minimal testing custom host", "minimal testing override host"
- I expected these to contain information on the issue:
- https://docs.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-6.0#customize-webapplicationfactory
- https://docs.microsoft.com/en-us/aspnet/core/migration/50-to-60-samples?view=aspnetcore-6.0#test-with-webapplicationfactory-or-testserver

**Describe the new topic**

- Being able to customize the Host configuration in integration tests is useful to avoid calling services that shouldn't run at all during testing. This was easily achievable using the standard Startup model, overriding the `CreateHostBuilder` from the `WebApplicationFactory`. I tried many many things using the "Minimal APIs" approach and couldn't figure it out.

A full example of what was possible using the Startup model, would be something like this:

Program.cs:
```csharp
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); })
// This is a service that downloads Configuration from the internet, to be used
// as a source for `IConfiguration`, just like any `appsettings.json`.
// I don't want this running during testing
.AddConfigServer();
}
```

As you can imagine, the `AddConfigServer` calls an external web server to download the configuration I want to use in my app startup, but I definitely don't want my integration tests calling this external web server for several reasons: don't want to depend on external services, don't want to hammer my config server with testing requests, don't want my server customizations to be exposed to my tests, etc.

Using the `Startup` model, I could easily change this behavior with this:

```csharp
public class MyWebApplicationFactory : WebApplicationFactory
{
protected override IHostBuilder CreateHostBuilder() =>
Host.CreateDefaultBuilder()
// No AddConfigServer for my integration tests
.ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup(); });
}

//... other customizations
}
```

However, trying this same approach throws an exception on my integration test startup:
```
System.InvalidOperationException : No application configured. Please specify an application via IWebHostBuilder.UseStartup, IWebHostBuilder.Configure, or specifying the startup assembly via StartupAssemblyKey in the web host configuration.
```

Since I don't have a `Startup` class, I couldn't figure out how to properly do this. In fact, since this is common in all my web projects, I abandoned Minimal APIs altogether for the moment, until I figure out how to properly achieve this without a `Startup` class.

- I expect this to be in "ASP.NET Core > Test, debug, and troubleshoot > Integration tests in ASP.NET Core".
- Suggestion: How to override Host configuration in ASP NET Core 6 Minimal APIs hosting model.

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.