WorkaroundSqlClientIssue26 1.0.0

dotnet add package WorkaroundSqlClientIssue26 --version 1.0.0
                    
NuGet\Install-Package WorkaroundSqlClientIssue26 -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WorkaroundSqlClientIssue26" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WorkaroundSqlClientIssue26" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="WorkaroundSqlClientIssue26" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WorkaroundSqlClientIssue26 --version 1.0.0
                    
#r "nuget: WorkaroundSqlClientIssue26, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package WorkaroundSqlClientIssue26@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=WorkaroundSqlClientIssue26&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=WorkaroundSqlClientIssue26&version=1.0.0
                    
Install as a Cake Tool

Work around Microsoft SqlClient issue #26 for EF Core

NuGet Continuous Integration Coverage

Microsoft's SqlClient has a long standing issue (#26): Cancelling an async SqlClient operation throws SqlException, not TaskCanceledException

It is problematic because whenever an SQL command is canceled, an InvalidOperationException, a SqlException or a TaskCanceledException will be randomly thrown . This is also a showstopper when using Blazor virtualization where an OperationCanceledException is expected to signal cancelation.

This project provides a workaround that leverages EF Core execution strategies. When using this workaround, querying the db context will consistently throw an OperationCanceledException upon cancelation.

try
{
  await dbContext.Tracks.CountAsync(cancellationToken);
}
catch (OperationCanceledException ex) when (ex.CancellationToken == cancellationToken)
{
  // Alwas caught with the workaround applied. Uncaught with SqlClient default behavior.
}

Getting started

Add the WorkaroundSqlClientIssue26 NuGet package to your project using the NuGet Package Manager or run the following command:

dotnet add package WorkaroundSqlClientIssue26

Install the workaround when configuring the db context options.

var options = new DbContextOptionsBuilder<MyDbContext>()
  .UseSqlServer(connectionString, sql => sql.WorkAroundSqlClientIssue26())
  .Options;

This workaround is also compatible with a custom execution strategy such as retry on failure.

var options = new DbContextOptionsBuilder<MyDbContext>()
  .UseSqlServer(connectionString, s => s.EnableRetryOnFailure().WorkAroundSqlClientIssue26())
  .Options;

Frequently Asked Questions

  • What are the supported EF Core versions?

    • EF Core 8, 9 and 10 are supported. Future versions should work too.
  • Why is this package required? Why doesn't EF Core take care of throwing a proper OperationCanceledException?

    • EF Core distinguishes cancelation from failure, but only for logging purposes. A maintainer of EF Core said this:

      EF makes no attempt to correct SqlClient's behavior here by throwing a different type of exception; it just distinguishes between failure and cancellation.

  • What happens if the original issue gets fixed?

    • If SqlClient issue #26 is fixed, this workaround will turn into a no-op.
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 103 4/27/2026