Upgrade MediatR to 12.5.0 and forward cancellation tokens through pipeline behaviors (#320)

This commit is contained in:
Aaron Po
2026-09-26 19:50:44 -04:00
committed by GitHub
parent 0ff42c3c70
commit e1b6cba1d4
7 changed files with 21 additions and 30 deletions

View File

@@ -15,7 +15,7 @@
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.11" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
<PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" /> <PackageReference Include="FluentValidation.AspNetCore" Version="11.3.0" />
<PackageReference Include="MediatR" Version="12.4.1" /> <PackageReference Include="MediatR" Version="12.5.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -9,7 +9,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="idunno.Password.Generator" Version="2.0.0" /> <PackageReference Include="idunno.Password.Generator" Version="2.0.0" />
<PackageReference Include="MediatR" Version="12.4.1" /> <PackageReference Include="MediatR" Version="12.5.0" />
<PackageReference Include="Microsoft.Data.SqlClient" Version="7.0.2" /> <PackageReference Include="Microsoft.Data.SqlClient" Version="7.0.2" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.11" /> <PackageReference Include="Microsoft.Data.Sqlite" Version="10.0.11" />
<PackageReference Include="SkiaSharp" Version="4.151.1" /> <PackageReference Include="SkiaSharp" Version="4.151.1" />

View File

@@ -26,7 +26,7 @@ public class UnhandledExceptionLoggingBehaviorTests
UnhandledExceptionLoggingBehavior<TestRequest, TestResponse> behavior = CreateBehavior(); UnhandledExceptionLoggingBehavior<TestRequest, TestResponse> behavior = CreateBehavior();
TestRequest request = new("aaron", "pa$$word123"); TestRequest request = new("aaron", "pa$$word123");
InvalidOperationException thrown = new("Something went wrong."); InvalidOperationException thrown = new("Something went wrong.");
RequestHandlerDelegate<TestResponse> next = () => throw thrown; Task<TestResponse> next(CancellationToken _ = default) => throw thrown;
Func<Task> act = async () => await behavior.Handle(request, next, CancellationToken.None); Func<Task> act = async () => await behavior.Handle(request, next, CancellationToken.None);
@@ -42,7 +42,7 @@ public class UnhandledExceptionLoggingBehaviorTests
UnhandledExceptionLoggingBehavior<TestRequest, TestResponse> behavior = CreateBehavior(); UnhandledExceptionLoggingBehavior<TestRequest, TestResponse> behavior = CreateBehavior();
TestRequest request = new("aaron", "pa$$word123"); TestRequest request = new("aaron", "pa$$word123");
ValidationException thrown = new([new ValidationFailure("Username", "is required")]); ValidationException thrown = new([new ValidationFailure("Username", "is required")]);
RequestHandlerDelegate<TestResponse> next = () => throw thrown; Task<TestResponse> next(CancellationToken _ = default) => throw thrown;
Func<Task> act = async () => await behavior.Handle(request, next, CancellationToken.None); Func<Task> act = async () => await behavior.Handle(request, next, CancellationToken.None);
@@ -56,7 +56,7 @@ public class UnhandledExceptionLoggingBehaviorTests
UnhandledExceptionLoggingBehavior<TestRequest, TestResponse> behavior = CreateBehavior(); UnhandledExceptionLoggingBehavior<TestRequest, TestResponse> behavior = CreateBehavior();
TestRequest request = new("aaron", "pa$$word123"); TestRequest request = new("aaron", "pa$$word123");
TestResponse response = new("ok"); TestResponse response = new("ok");
RequestHandlerDelegate<TestResponse> next = () => Task.FromResult(response); Task<TestResponse> next(CancellationToken _ = default) => Task.FromResult(response);
TestResponse result = await behavior.Handle(request, next, CancellationToken.None); TestResponse result = await behavior.Handle(request, next, CancellationToken.None);
@@ -69,7 +69,7 @@ public class UnhandledExceptionLoggingBehaviorTests
{ {
UnhandledExceptionLoggingBehavior<TestRequest, TestResponse> behavior = CreateBehavior(); UnhandledExceptionLoggingBehavior<TestRequest, TestResponse> behavior = CreateBehavior();
TestRequest request = new("aaron", "pa$$word123"); TestRequest request = new("aaron", "pa$$word123");
RequestHandlerDelegate<TestResponse> next = () => Task<TestResponse> next(CancellationToken _ = default) =>
throw new InvalidOperationException("Something went wrong."); throw new InvalidOperationException("Something went wrong.");
Func<Task> act = async () => await behavior.Handle(request, next, CancellationToken.None); Func<Task> act = async () => await behavior.Handle(request, next, CancellationToken.None);

View File

@@ -31,17 +31,13 @@ public class ValidationBehaviorTests
TestRequest request = new("aaron", "pa$$word123"); TestRequest request = new("aaron", "pa$$word123");
TestResponse response = new("ok"); TestResponse response = new("ok");
bool nextInvoked = false; bool nextInvoked = false;
RequestHandlerDelegate<TestResponse> next = () => Task<TestResponse> next(CancellationToken _ = default)
{ {
nextInvoked = true; nextInvoked = true;
return Task.FromResult(response); return Task.FromResult(response);
}; }
TestResponse result = await behavior.Handle( TestResponse result = await behavior.Handle(request, next, CancellationToken.None);
request,
next,
CancellationToken.None
);
nextInvoked.Should().BeTrue(); nextInvoked.Should().BeTrue();
result.Should().BeSameAs(response); result.Should().BeSameAs(response);
@@ -65,11 +61,11 @@ public class ValidationBehaviorTests
); );
TestRequest request = new("", "pa$$word123"); TestRequest request = new("", "pa$$word123");
bool nextInvoked = false; bool nextInvoked = false;
RequestHandlerDelegate<TestResponse> next = () => Task<TestResponse> next(CancellationToken _ = default)
{ {
nextInvoked = true; nextInvoked = true;
return Task.FromResult(new TestResponse("ok")); return Task.FromResult(new TestResponse("ok"));
}; }
Func<Task> act = async () => await behavior.Handle(request, next, CancellationToken.None); Func<Task> act = async () => await behavior.Handle(request, next, CancellationToken.None);
@@ -107,7 +103,7 @@ public class ValidationBehaviorTests
secondValidatorMock.Object secondValidatorMock.Object
); );
TestRequest request = new("aaron", "pa$$word123"); TestRequest request = new("aaron", "pa$$word123");
RequestHandlerDelegate<TestResponse> next = () => static Task<TestResponse> next(CancellationToken _ = default) =>
Task.FromResult(new TestResponse("ok")); Task.FromResult(new TestResponse("ok"));
await behavior.Handle(request, next, CancellationToken.None); await behavior.Handle(request, next, CancellationToken.None);
@@ -158,7 +154,7 @@ public class ValidationBehaviorTests
secondValidatorMock.Object secondValidatorMock.Object
); );
TestRequest request = new("", "pa$$word123"); TestRequest request = new("", "pa$$word123");
RequestHandlerDelegate<TestResponse> next = () => Task<TestResponse> next(CancellationToken _ = default) =>
Task.FromResult(new TestResponse("ok")); Task.FromResult(new TestResponse("ok"));
Func<Task> act = async () => await behavior.Handle(request, next, CancellationToken.None); Func<Task> act = async () => await behavior.Handle(request, next, CancellationToken.None);
@@ -171,17 +167,12 @@ public class ValidationBehaviorTests
[Fact] [Fact]
public async Task Handle_WhenNoValidatorsAreRegistered_InvokesNext() public async Task Handle_WhenNoValidatorsAreRegistered_InvokesNext()
{ {
ValidationBehavior<TestRequest, TestResponse> behavior = ValidationBehavior<TestRequest, TestResponse> behavior = CreateBehavior();
CreateBehavior();
TestRequest request = new("aaron", "pa$$word123"); TestRequest request = new("aaron", "pa$$word123");
TestResponse response = new("ok"); TestResponse response = new("ok");
RequestHandlerDelegate<TestResponse> next = () => Task.FromResult(response); Task<TestResponse> next(CancellationToken _ = default) => Task.FromResult(response);
TestResponse result = await behavior.Handle( TestResponse result = await behavior.Handle(request, next, CancellationToken.None);
request,
next,
CancellationToken.None
);
result.Should().BeSameAs(response); result.Should().BeSameAs(response);
} }

View File

@@ -67,7 +67,7 @@ public partial class UnhandledExceptionLoggingBehavior<TRequest, TResponse>
{ {
try try
{ {
return await next(); return await next(cancellationToken);
} }
catch (ValidationException) catch (ValidationException)
{ {

View File

@@ -21,7 +21,7 @@ public class ValidationBehavior<TRequest, TResponse>(IEnumerable<IValidator<TReq
) )
{ {
if (!validators.Any()) if (!validators.Any())
return await next(); return await next(cancellationToken);
ValidationContext<TRequest> context = new(request); ValidationContext<TRequest> context = new(request);
@@ -35,6 +35,6 @@ public class ValidationBehavior<TRequest, TResponse>(IEnumerable<IValidator<TReq
if (failures.Count > 0) if (failures.Count > 0)
throw new ValidationException(failures); throw new ValidationException(failures);
return await next(); return await next(cancellationToken);
} }
} }

View File

@@ -7,7 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MediatR" Version="12.4.1" /> <PackageReference Include="MediatR" Version="12.5.0" />
<PackageReference Include="FluentValidation" Version="11.3.0" /> <PackageReference Include="FluentValidation" Version="11.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.1" /> <PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="10.0.1" />
</ItemGroup> </ItemGroup>