dotnet / dotnet/aspnetcore

How to validate DI container in minimal api?

Open
#45,047 4 comments 0 reactions 1 assignee Claimed by @davidfowl View on GitHub
area-minimal
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

Before I was having something like this:

```c#
public static class Program
{
private static async Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
await host.RunAsync();
}

public static IHostBuilder CreateHostBuilder(params string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureHostConfiguration(builder => { })
.ConfigureAppConfiguration((context, builder) => { })
.ConfigureLogging((context, logging) => { })
.ConfigureWebHostDefaults(builder => builder.UseStartup());
}
```

and then

```c#
[TestClass]
public class HostTests
{
[TestMethod]
public void Build_Should_Return_Host_With_Valid_Services()
{
var builder = Program.CreateHostBuilder()
.UseDefaultServiceProvider(options =>
{
options.ValidateScopes = true;
options.ValidateOnBuild = true;
});
var host = builder.Build();
host.Should().NotBeNull();
}
}
```

so it would fail in case of the DI container misconfiguration. Not always, but at least a basic smoke test.

What would be an alternative in the Minimal API and .NET 7?

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.