Skip to content

HostBuilder validation enabled #42042

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/core/compatibility/9.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ If you're migrating an app to .NET 9, the breaking changes listed here might aff
| Title | Type of change | Introduced version |
|------------------------------------------------------------------------------------------|---------------------|--------------------|
| [DefaultKeyResolution.ShouldGenerateNewKey has altered meaning](aspnet-core/9.0/key-resolution.md) | Behavioral change | Preview 3 |
| [HostBuilder enables ValidateOnBuild/ValidateScopes in development environment](aspnet-core/9.0/hostbuilder-validation.md) | Behavioral change | Preview 7 |

## Core .NET libraries

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
title: "Breaking change: HostBuilder enables ValidateOnBuild/ValidateScopes in development environment"
description: Learn about the breaking change in .NET 9 where HostBuilder now enables ValidateOnBuild and ValidateScopes in the development environment.
ms.date: 08/05/2024
---
# HostBuilder enables ValidateOnBuild/ValidateScopes in development environment

Previously, no validation was enabled by default. Now, in the [development environment](/aspnet/core/fundamentals/environments), <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](/aspnet/core/fundamentals/environments) 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](../../categories.md#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.

## Recommended action

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

- <xref:Microsoft.Extensions.Hosting.HostBuilder>
- <xref:Microsoft.Extensions.Hosting.HostBuilder.Build>
4 changes: 4 additions & 0 deletions docs/core/compatibility/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ items:
items:
- name: DefaultKeyResolution.ShouldGenerateNewKey has altered meaning
href: aspnet-core/9.0/key-resolution.md
- name: HostBuilder enables ValidateOnBuild/ValidateScopes in development environment
href: aspnet-core/9.0/hostbuilder-validation.md
- name: Core .NET libraries
items:
- name: Adding a ZipArchiveEntry sets header general-purpose bit flags
Expand Down Expand Up @@ -958,6 +960,8 @@ items:
items:
- name: DefaultKeyResolution.ShouldGenerateNewKey has altered meaning
href: aspnet-core/9.0/key-resolution.md
- name: HostBuilder enables ValidateOnBuild/ValidateScopes in development environment
href: aspnet-core/9.0/hostbuilder-validation.md
- name: .NET 8
items:
- name: ConcurrencyLimiterMiddleware is obsolete
Expand Down
Loading