Skip to content

Commit eb0cf49

Browse files
committed
fix(CanConnectAsync): throw OperationCanceledException in case of cancellation
1 parent 7a87abf commit eb0cf49

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/EFCore.Relational/Storage/RelationalDatabaseCreator.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,8 @@ public virtual bool CanConnect()
373373
/// </summary>
374374
/// <remarks>
375375
/// <para>
376-
/// Any exceptions thrown when attempting to connect are caught and not propagated to the application.
376+
/// Any exceptions thrown when attempting to connect are caught and not propagated to the application,
377+
/// except for the ones indicating cancellation.
377378
/// </para>
378379
/// <para>
379380
/// The configured connection string is used to create the connection in the normal way, so all
@@ -386,13 +387,17 @@ public virtual bool CanConnect()
386387
/// </remarks>
387388
/// <param name="cancellationToken">A <see cref="CancellationToken" /> to observe while waiting for the task to complete.</param>
388389
/// <returns><see langword="true" /> if the database is available; <see langword="false" /> otherwise.</returns>
389-
/// <exception cref="OperationCanceledException">If the <see cref="CancellationToken" /> is canceled.</exception>
390+
/// <exception cref="Exception">Various exceptions might be thrown if the <see cref="CancellationToken" /> is canceled.</exception>
390391
public virtual async Task<bool> CanConnectAsync(CancellationToken cancellationToken = default)
391392
{
392393
try
393394
{
394395
return await ExistsAsync(cancellationToken).ConfigureAwait(false);
395396
}
397+
catch (Exception exception) when (Dependencies.ExceptionDetector.IsCancellation(exception, cancellationToken))
398+
{
399+
throw;
400+
}
396401
catch
397402
{
398403
return false;

src/EFCore.Relational/Storage/RelationalDatabaseCreatorDependencies.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public RelationalDatabaseCreatorDependencies(
5454
IExecutionStrategy executionStrategy,
5555
ICurrentDbContext currentContext,
5656
IDbContextOptions contextOptions,
57-
IRelationalCommandDiagnosticsLogger commandLogger)
57+
IRelationalCommandDiagnosticsLogger commandLogger,
58+
IExceptionDetector exceptionDetector)
5859
{
5960
Connection = connection;
6061
ModelDiffer = modelDiffer;
@@ -65,6 +66,7 @@ public RelationalDatabaseCreatorDependencies(
6566
CurrentContext = currentContext;
6667
ContextOptions = contextOptions;
6768
CommandLogger = commandLogger;
69+
ExceptionDetector = exceptionDetector;
6870
}
6971

7072
/// <summary>
@@ -111,4 +113,9 @@ public RelationalDatabaseCreatorDependencies(
111113
/// Contains the <see cref="DbContext" /> currently in use.
112114
/// </summary>
113115
public ICurrentDbContext CurrentContext { get; init; }
116+
117+
/// <summary>
118+
/// Gets the exception detector.
119+
/// </summary>
120+
public IExceptionDetector ExceptionDetector { get; init; }
114121
}

0 commit comments

Comments
 (0)