Skip to content

Resolving disposed service provider #26874

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 3 commits into from
Nov 9, 2021
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
3 changes: 2 additions & 1 deletion docs/core/compatibility/6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ If you're migrating an app to .NET 6, the breaking changes listed here might aff

| Title | Binary compatible | Source compatible | Introduced |
| - | - | - | - |
| [FileConfigurationProvider.Load throws InvalidDataException](extensions/6.0/filename-in-load-exception.md) | ✔️ | ❌ | RC 1 |
| [AddProvider checks for non-null provider](extensions/6.0/addprovider-null-check.md) | ✔️ | ❌ | RC 1 |
| [FileConfigurationProvider.Load throws InvalidDataException](extensions/6.0/filename-in-load-exception.md) | ✔️ | ❌ | RC 1 |
| [Resolving disposed ServiceProvider throws exception](extensions/6.0/service-provider-disposed.md) | ✔️ | ❌ | RC 1 |

## Globalization

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
title: "Breaking change: Resolving disposed ServiceProvider throws exception"
description: Learn about the .NET 6 breaking change in .NET extensions where resolving a disposed service provider throws an exception.
ms.date: 11/08/2021
---
# Resolving disposed ServiceProvider throws exception

When a service is resolved after the service provider has been disposed, the affected methods now throw an <xref:System.ObjectDisposedException> instead of causing a deadlock.

## Version introduced

6.0 RC 1

## Previous behavior

Previously, in the rare case that an application resolved a service after the service provider was disposed, it led to a deadlock.

## New behavior

Starting in .NET 6, an <xref:System.ObjectDisposedException> is thrown when a service is resolved after the service provider has been disposed, and there's no deadlock.

## Type of breaking change

This change can affect [source compatibility](../../categories.md#source-compatibility).

## Reason for change

This change was introduced to fix the deadlock scenario.

## Recommended action

Catch <xref:System.ObjectDisposedException> when calling any of the affected APIs.

## Affected APIs

- <xref:System.IServiceProvider.GetService(System.Type)?displayProperty=fullName>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the affected APIs - please double check.

- <xref:Microsoft.Extensions.DependencyInjection.ISupportRequiredService.GetRequiredService(System.Type)?displayProperty=fullName>
- <xref:Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(System.IServiceProvider,System.Type)?displayProperty=fullName>
- <xref:Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService%60%601(System.IServiceProvider)?displayProperty=fullName>
8 changes: 6 additions & 2 deletions docs/core/compatibility/toc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,12 @@ items:
href: /ef/core/what-is-new/ef-core-6.0/breaking-changes?toc=/dotnet/core/compatibility/toc.json&bc=/dotnet/breadcrumb/toc.json
- name: Extensions
items:
- name: FileConfigurationProvider.Load throws InvalidDataException
href: extensions/6.0/filename-in-load-exception.md
- name: AddProvider checks for non-null provider
href: extensions/6.0/addprovider-null-check.md
- name: FileConfigurationProvider.Load throws InvalidDataException
href: extensions/6.0/filename-in-load-exception.md
- name: Resolving disposed ServiceProvider throws exception
href: extensions/6.0/service-provider-disposed.md
- name: Globalization
items:
- name: Culture creation and case mapping in globalization-invariant mode
Expand Down Expand Up @@ -769,6 +771,8 @@ items:
href: extensions/6.0/addprovider-null-check.md
- name: FileConfigurationProvider.Load throws InvalidDataException
href: extensions/6.0/filename-in-load-exception.md
- name: Resolving disposed ServiceProvider throws exception
href: extensions/6.0/service-provider-disposed.md
- name: Globalization
items:
- name: .NET 6
Expand Down