Skip to content

Commit 1e9b464

Browse files
The most significant changes include the renaming of a method to reflect its asynchronous nature, the addition of a try-catch block to handle exceptions during query execution, and the implementation of a new strategy to handle InvalidOperationException by attempting to retrieve results again with a CloseConnection command behavior.
1. The method `FirstOrDefault` has been renamed to `FirstOrDefaultAsync`, indicating that the method is now asynchronous. This change is important as it provides clarity about the method's behavior and usage. 2. A try-catch block has been added around the query execution code. This change is crucial as it allows the program to handle exceptions that may occur during the execution of the query, preventing the program from crashing and providing a way to manage errors. 3. In case of an `InvalidOperationException`, the code now tries to retrieve the results again using a `CommandBehavior` of `CloseConnection`. This change suggests that the original exception might be due to connection issues, and closing the connection after retrieving the results might solve the problem. 4. If any other type of exception is thrown, it is simply re-thrown. This change allows the exception to be handled further up the call stack, providing a way to manage unexpected errors. References to the code changes can be found at the end of each description.
1 parent f6c93ab commit 1e9b464

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

src/XperienceCommunity.AspNetCore.HealthChecks/Extensions/IObjectQueryExtensions.cs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,33 @@ public static async Task<List<TObject>> ToListAsync<TQuery, TObject>(this IObjec
5555
/// <param name="cancellationToken">The cancellation token.</param>
5656
/// <returns>The first element of the query, or a default value if the query is empty.</returns>
5757
/// <exception cref="ArgumentNullException">Thrown when the query is null.</exception>
58-
public static async Task<TObject?> FirstOrDefault<TQuery, TObject>(this IObjectQuery<TQuery, TObject> query, CancellationToken cancellationToken = default)
58+
public static async Task<TObject?> FirstOrDefaultAsync<TQuery, TObject>(this IObjectQuery<TQuery, TObject> query, CancellationToken cancellationToken = default)
5959
where TQuery : IObjectQuery<TQuery, TObject>
6060
where TObject : BaseInfo
6161
{
6262
ArgumentNullException.ThrowIfNull(query);
6363

6464
cancellationToken.ThrowIfCancellationRequested();
65+
try
66+
{
6567

66-
var results = await query
67-
.GetEnumerableTypedResultAsync(commandBehavior: CommandBehavior.SequentialAccess, true, cancellationToken: cancellationToken);
68+
var results = await query
69+
.GetEnumerableTypedResultAsync(commandBehavior: CommandBehavior.SequentialAccess, true, cancellationToken: cancellationToken);
6870

69-
return results?.FirstOrDefault() ?? default;
71+
return results?.FirstOrDefault() ?? default;
72+
}
73+
catch (InvalidOperationException)
74+
{
75+
var results = await query
76+
.GetEnumerableTypedResultAsync(commandBehavior: CommandBehavior.CloseConnection, true,
77+
cancellationToken: cancellationToken);
78+
79+
return results?.FirstOrDefault() ?? default;
80+
}
81+
catch (Exception)
82+
{
83+
throw;
84+
}
7085
}
7186

7287
public static WhereCondition WhereNotNullOrEmpty<TQuery, TObject>(this IObjectQuery<TQuery, TObject> query,

0 commit comments

Comments
 (0)