Skip to content

Commit efb599c

Browse files
authored
Merge pull request #23 from fretje/parallel
Update readme with usage of `UseTaskWhenAll`
2 parents 264932e + a1cbb72 commit efb599c

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 7.0.1
2+
- Remove unnecessary check for _options.UseTaskWhenAll
3+
- README: Add 'Parallel notification handling' section
4+
15
# 7.0.0
26
- Removed Nuke
37
- Updated to .NET 8

MediatR.Courier/MediatRCourier.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ async Task HandleLocal(INotification n, CancellationToken c)
7272
}
7373
}
7474

75-
if (_options.UseTaskWhenAll && tasks.Count > 0)
75+
if (tasks.Count > 0)
7676
{
7777
await Task.WhenAll(tasks).ConfigureAwait(_options.CaptureThreadContext);
7878
}

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,26 @@ courier.SubscribeWeak<MyNotification>((notification, cancellation) => /*...*/);
165165

166166
You can configure how the Courier awaits the sent notifications. To change the behavior modify the `CaptureThreadContext` property on the `CourierOptions` class. When using dependency injection, you can change this behavior during runtime, because the `CourierOptions` is accessible through DI.
167167

168+
### Parallel notification handling
169+
170+
You can configure whether notification handlers should run sequentially or in parallel by using the `UseTaskWhenAll` property on the `CourierOptions` class:
171+
172+
```c#
173+
// Configure at registration time
174+
services.AddCourier(typeof(MyType).Assembly, options =>
175+
{
176+
options.UseTaskWhenAll = true; // Enable parallel execution of handlers
177+
});
178+
179+
// Or modify at runtime through dependency injection
180+
var options = serviceProvider.GetRequiredService<CourierOptions>();
181+
options.UseTaskWhenAll = true;
182+
```
183+
184+
When `UseTaskWhenAll` is set to `true`, asynchronous notification handlers are collected and awaited concurrently using `Task.WhenAll`.
185+
186+
When set to `false` (the default), handlers are awaited sequentially.
187+
168188
## Gotchas
169189

170190
* No ordering is guaranteed when calling the subscribed methods

0 commit comments

Comments
 (0)