Back to Aspnetcore

Breaking change: HostBuilder enables ValidateOnBuild/ValidateScopes in development environment

aspnetcore/breaking-changes/9/hostbuilder-validation.md

latest1.9 KB
Original Source

HostBuilder enables ValidateOnBuild/ValidateScopes in development environment

Previously, no validation was enabled by default. Now, in the development environment, xref:Microsoft.Extensions.DependencyInjection.ServiceProviderOptions.ValidateOnBuild and xref:Microsoft.Extensions.DependencyInjection.ServiceProviderOptions.ValidateScopes are enabled.

Version introduced

.NET 9 Preview 7

Previous behavior

xref:Microsoft.Extensions.DependencyInjection.ServiceProviderOptions.ValidateOnBuild and xref:Microsoft.Extensions.DependencyInjection.ServiceProviderOptions.ValidateScopes defaulted to false and were only enabled when they were explicitly set by calling xref:Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.UseDefaultServiceProvider%2A.

New behavior

In the development environment when options haven't been set with xref:Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.UseDefaultServiceProvider%2A, xref:Microsoft.Extensions.DependencyInjection.ServiceProviderOptions.ValidateOnBuild and xref:Microsoft.Extensions.DependencyInjection.ServiceProviderOptions.ValidateScopes are set to true.

Type of breaking change

This change is a behavioral change.

Reason for change

Validation helps to catch problems early in application startup rather than later (or not at all) when the application interacts with its service provider.

No action necessary if your application validates successfully. If you do see a validation error when testing in development, first try to fix it. If you can't fix it, you can disable validation by calling xref:Microsoft.AspNetCore.Hosting.WebHostBuilderExtensions.UseDefaultServiceProvider%2A.

Affected APIs