Skip to content

Commit 447792f

Browse files
authored
Merge branch 'master' into master
2 parents dd74609 + 0d3bf4c commit 447792f

File tree

6 files changed

+330
-202
lines changed

6 files changed

+330
-202
lines changed

src/MediatR.Contracts/MediatR.Contracts.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<Description>Contracts package for requests, responses, and notifications</Description>
77
<Copyright>Copyright Jimmy Bogard</Copyright>
88
<TargetFramework>netstandard2.0</TargetFramework>
9-
<Nullable>enable</Nullable>
109
<Features>strict</Features>
1110
<PackageTags>mediator;request;response;queries;commands;notifications</PackageTags>
1211
<SignAssembly>true</SignAssembly>

src/MediatR/Entities/OpenBehavior.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
4+
namespace MediatR.Entities;
5+
/// <summary>
6+
/// Creates open behavior entity.
7+
/// </summary>
8+
public class OpenBehavior
9+
{
10+
public OpenBehavior(Type openBehaviorType, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
11+
{
12+
OpenBehaviorType = openBehaviorType;
13+
ServiceLifetime = serviceLifetime;
14+
}
15+
16+
public Type? OpenBehaviorType { get; }
17+
public ServiceLifetime ServiceLifetime { get; }
18+
19+
20+
}

src/MediatR/IPipelineBehavior.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace MediatR;
99
/// </summary>
1010
/// <typeparam name="TResponse">Response type</typeparam>
1111
/// <returns>Awaitable task returning a <typeparamref name="TResponse"/></returns>
12-
public delegate Task<TResponse> RequestHandlerDelegate<TResponse>();
12+
public delegate Task<TResponse> RequestHandlerDelegate<TResponse>(CancellationToken t = default);
1313

1414
/// <summary>
1515
/// Pipeline behavior to surround the inner handler.

src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Reflection;
55
using MediatR;
6+
using MediatR.Entities;
67
using MediatR.NotificationPublishers;
78
using MediatR.Pipeline;
89
using MediatR.Registration;
@@ -197,6 +198,37 @@ public MediatRServiceConfiguration AddOpenBehavior(Type openBehaviorType, Servic
197198
return this;
198199
}
199200

201+
/// <summary>
202+
/// Registers multiple open behavior types against the <see cref="IPipelineBehavior{TRequest,TResponse}"/> open generic interface type
203+
/// </summary>
204+
/// <param name="openBehaviorTypes">An open generic behavior type list includes multiple open generic behavior types.</param>
205+
/// <param name="serviceLifetime">Optional service lifetime, defaults to <see cref="ServiceLifetime.Transient"/>.</param>
206+
/// <returns>This</returns>
207+
public MediatRServiceConfiguration AddOpenBehaviors(IEnumerable<Type> openBehaviorTypes, ServiceLifetime serviceLifetime = ServiceLifetime.Transient)
208+
{
209+
foreach (var openBehaviorType in openBehaviorTypes)
210+
{
211+
AddOpenBehavior(openBehaviorType, serviceLifetime);
212+
}
213+
214+
return this;
215+
}
216+
217+
/// <summary>
218+
/// Registers open behaviors against the <see cref="IPipelineBehavior{TRequest,TResponse}"/> open generic interface type
219+
/// </summary>
220+
/// <param name="openBehaviors">An open generic behavior list includes multiple <see cref="OpenBehavior"/> open generic behaviors.</param>
221+
/// <returns>This</returns>
222+
public MediatRServiceConfiguration AddOpenBehaviors(IEnumerable<OpenBehavior> openBehaviors)
223+
{
224+
foreach (var openBehavior in openBehaviors)
225+
{
226+
AddOpenBehavior(openBehavior.OpenBehaviorType!, openBehavior.ServiceLifetime);
227+
}
228+
229+
return this;
230+
}
231+
200232
/// <summary>
201233
/// Register a closed stream behavior type
202234
/// </summary>

src/MediatR/Wrappers/RequestHandlerWrapper.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public class RequestHandlerWrapperImpl<TRequest, TResponse> : RequestHandlerWrap
3434
public override Task<TResponse> Handle(IRequest<TResponse> request, IServiceProvider serviceProvider,
3535
CancellationToken cancellationToken)
3636
{
37-
Task<TResponse> Handler() => serviceProvider.GetRequiredService<IRequestHandler<TRequest, TResponse>>()
38-
.Handle((TRequest) request, cancellationToken);
37+
Task<TResponse> Handler(CancellationToken t = default) => serviceProvider.GetRequiredService<IRequestHandler<TRequest, TResponse>>()
38+
.Handle((TRequest) request, t == default ? cancellationToken : t);
3939

4040
return serviceProvider
4141
.GetServices<IPipelineBehavior<TRequest, TResponse>>()
4242
.Reverse()
4343
.Aggregate((RequestHandlerDelegate<TResponse>) Handler,
44-
(next, pipeline) => () => pipeline.Handle((TRequest) request, next, cancellationToken))();
44+
(next, pipeline) => (t) => pipeline.Handle((TRequest) request, next, t == default ? cancellationToken : t))();
4545
}
4646
}
4747

@@ -55,10 +55,10 @@ public class RequestHandlerWrapperImpl<TRequest> : RequestHandlerWrapper
5555
public override Task<Unit> Handle(IRequest request, IServiceProvider serviceProvider,
5656
CancellationToken cancellationToken)
5757
{
58-
async Task<Unit> Handler()
58+
async Task<Unit> Handler(CancellationToken t = default)
5959
{
6060
await serviceProvider.GetRequiredService<IRequestHandler<TRequest>>()
61-
.Handle((TRequest) request, cancellationToken);
61+
.Handle((TRequest) request, t == default ? cancellationToken : t);
6262

6363
return Unit.Value;
6464
}
@@ -67,6 +67,6 @@ await serviceProvider.GetRequiredService<IRequestHandler<TRequest>>()
6767
.GetServices<IPipelineBehavior<TRequest, Unit>>()
6868
.Reverse()
6969
.Aggregate((RequestHandlerDelegate<Unit>) Handler,
70-
(next, pipeline) => () => pipeline.Handle((TRequest) request, next, cancellationToken))();
70+
(next, pipeline) => (t) => pipeline.Handle((TRequest) request, next, t == default ? cancellationToken : t))();
7171
}
7272
}

0 commit comments

Comments
 (0)