Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
157 commits
Select commit Hold shift + click to select a range
1266be2
Update packages
Jun 22, 2023
0d5e1ab
update README.md
Jun 22, 2023
d96d2fd
Remove 'LiteBus' prefix from module construct names
Jun 23, 2023
940b304
Update README.md
Jun 23, 2023
d50ba9a
Update README.md
Jun 23, 2023
077248d
Update Release-Notes.txt
Jun 23, 2023
0615876
remove IEvent constraint from event handlers
Jun 23, 2023
0f24800
Bump Microsoft.NET.Test.Sdk from 17.6.2 to 17.6.3
dependabot[bot] Jun 27, 2023
99f3cd1
Update LICENSE
Jun 27, 2023
24092ed
Merge pull request #38 from litenova/dependabot/nuget/Microsoft.NET.T…
Jun 27, 2023
c009b5c
Upgrade packages
Jul 11, 2023
64f2e4f
remove unused directives
Aug 2, 2023
93cc55c
Update Microsoft.NET.Test.Sdk package
Aug 17, 2023
660169a
Bump FluentAssertions from 6.11.0 to 6.12.0
dependabot[bot] Aug 23, 2023
28c7967
Merge pull request #43 from litenova/dependabot/nuget/FluentAssertion…
Aug 28, 2023
5d0aeca
Add execution context
Sep 26, 2023
f02e230
add a section for ExecutionContext in README.md
Sep 26, 2023
e52f072
Merge pull request #48 from litenova/introduce-execution-context
Sep 26, 2023
712b1d5
bump version to 0.16.0
Sep 26, 2023
d6557c6
add README.md to nuget packages
Sep 26, 2023
6700ed0
Update README.md
Sep 27, 2023
0ff2795
Add `Module` post-fix to module registration method names
Oct 20, 2023
f97493e
Add `Items` property to `IExecutionContext` to allow passing data bet…
Oct 20, 2023
bba2b9e
Remove AsyncEnumerable.cs utility class
Oct 29, 2023
63a82bc
Move the rethrow exception logic inside the RunAsyncErrorHandlers if …
Oct 29, 2023
1cd9860
Upgrade packages of LiteBus.UnitTests.csproj to latest
Oct 29, 2023
8e3a960
Update README.md
Oct 29, 2023
8ff3139
- All post handlers expose message result as the second parameter.
Nov 1, 2023
c978a0c
Merge test coverage results
Nov 2, 2023
fa09032
fix test coverage step in release.yml
Nov 3, 2023
7c6259c
Make execution of event handlers sync
Nov 6, 2023
fe63b91
preserve the stack trace when rethrowing exception in case there are …
Nov 22, 2023
4655c90
Add `EventMediationSettings` to IEventMediator to allow configuring e…
Nov 23, 2023
ca94d62
Merge pull request #59 from litenova/add-event-publish-settings
Nov 23, 2023
56f9498
Rename `FilterHandler` to `HandlerFilter` on `EventMediationSettings`
Nov 23, 2023
8967537
Upgrade to .NET 8
Nov 23, 2023
243a2fc
Add 'ThrowOnNoHandlers' to `EventMediationSettings`
Nov 25, 2023
956ce1c
Add a unit test to cover mediating an event with no handlers
Nov 26, 2023
3e22b19
Rename `Release-Notes.txt` to `Changelog.md`
Nov 26, 2023
d32a9e4
update github action workflows to upload test results to Codecov
Nov 26, 2023
39de92d
fix test report file names in release.yml
Nov 26, 2023
a1a122f
Update README.md
Nov 26, 2023
9cd5823
Push nuget packages to github packages using personal access token
Nov 27, 2023
3099d20
Merge remote-tracking branch 'origin/main'
Nov 27, 2023
e308f9a
Update README.md
Nov 27, 2023
15d3e33
Revert TargetFramework to .NET 7
Dec 5, 2023
a6d90c6
remove explicit version from LiteBus.Messaging.Extensions.MicrosoftDe…
Dec 5, 2023
31db551
Rename `AddMessaging` method to `AddMessageModule`
Dec 5, 2023
fe7a191
Remove redundant method in PostHandlerDescriptorBuilder.cs
Dec 6, 2023
2b0622c
refactor handler descriptors
Dec 8, 2023
d6b0660
Upgrade to Version 0.20.2
Dec 8, 2023
07279b1
Bump actions/setup-dotnet from 3 to 4
dependabot[bot] Dec 11, 2023
866d972
Merge pull request #61 from litenova/dependabot/github_actions/action…
Dec 11, 2023
1e6b1e2
Fixed Query, Event, and Command error handlers returning `object` ins…
Dec 18, 2023
61fc28c
Merge remote-tracking branch 'origin/main'
Dec 18, 2023
91f8a42
Introduce tag-based handler filtering through `HandlerTag` and `Handl…
Dec 23, 2023
bc5f3a3
Fix the missing `Exception` parameter in `IAsyncMessageErrorHandler[T…
Dec 23, 2023
5143f1d
Pump version to 0.23.0
Dec 23, 2023
8a66d02
Upgrade xunit and xunit.runner.visualstudio to latest versions
Jan 9, 2024
c1c20d4
Add `QueryMediatorExtensions`, `CommandMediatorExtensions`, and `Even…
Jan 11, 2024
d1f0d7d
Upgrade to .NET 8
Jan 30, 2024
c3b511f
Add `Tags` to `IExecutionContext`
Jan 30, 2024
c357115
Bump codecov/codecov-action from 3 to 4
dependabot[bot] Feb 5, 2024
9ddeda1
Merge pull request #62 from litenova/dependabot/github_actions/codeco…
Feb 5, 2024
fb89a05
Update release.yml
Feb 14, 2024
4218437
Add 'Abort' feature
Apr 16, 2024
b2f6693
Don't throw error if no handlers were found for plain event message t…
May 2, 2024
af7afd1
Add wiki submodule
Jul 8, 2024
ec77694
Bump codecov/codecov-action from 4 to 5
dependabot[bot] Nov 18, 2024
ba29ee7
Merge pull request #63 from litenova/dependabot/github_actions/codeco…
Jan 5, 2025
92fd9ca
Improve XML comments in the codebase
Apr 10, 2025
6e726f8
Enable `Nullable` for all projects
Apr 13, 2025
b45fef4
Add ICommandValidator
Apr 17, 2025
e0c17d1
Format and Cleanup
Apr 17, 2025
7d165a0
Bump coverlet.msbuild from 6.0.0 to 6.0.4
dependabot[bot] Apr 17, 2025
3f00a18
Bump xunit.runner.visualstudio from 2.5.6 to 3.0.2
dependabot[bot] Apr 17, 2025
3b49177
Bump Microsoft.NET.Test.Sdk from 17.8.0 to 17.13.0
dependabot[bot] Apr 17, 2025
6978fd7
Bump coverlet.collector from 6.0.0 to 6.0.4
dependabot[bot] Apr 17, 2025
bbde9f4
Update wiki
Apr 18, 2025
7b2cdae
Update README.md
Apr 18, 2025
310d6ac
Prepare for v1.0.0 release
Apr 18, 2025
06aeab5
Update GitHub Actions to use upload-artifact@v4
Apr 18, 2025
6bbdc64
Update Changelog.md
Apr 18, 2025
169b965
Merge pull request #72 from litenova/release/v1.0.0
Apr 18, 2025
2d8a399
Bump softprops/action-gh-release from 1 to 2
dependabot[bot] Apr 18, 2025
991ac02
Bump gittools/actions from 0.9.15 to 3.2.0
dependabot[bot] Apr 18, 2025
dd5c1e0
Bump codecov/codecov-action from 3 to 5
dependabot[bot] Apr 18, 2025
d6fbb73
Fix GitVersion configuration by adding preview branch regex
Apr 18, 2025
ee1cdad
Bump Microsoft.SourceLink.GitHub from 1.1.1 to 8.0.0
dependabot[bot] Apr 18, 2025
f122a70
Simplify GitVersion configuration
Apr 18, 2025
3922105
Merge pull request #76 from litenova/dependabot/nuget/Microsoft.Sourc…
Apr 19, 2025
c8a90a6
Merge pull request #75 from litenova/dependabot/github_actions/codeco…
Apr 19, 2025
378429a
Merge pull request #74 from litenova/dependabot/github_actions/gittoo…
Apr 19, 2025
b709cf4
Merge pull request #73 from litenova/dependabot/github_actions/softpr…
Apr 19, 2025
541973b
Merge pull request #70 from litenova/dependabot/nuget/coverlet.collec…
Apr 19, 2025
fdd3978
Merge pull request #69 from litenova/dependabot/nuget/Microsoft.NET.T…
Apr 19, 2025
70fdc20
Merge pull request #67 from litenova/dependabot/nuget/xunit.runner.vi…
Apr 19, 2025
4562484
Merge pull request #66 from litenova/dependabot/nuget/coverlet.msbuil…
Apr 19, 2025
4aac8bf
Update Packages
Apr 19, 2025
63c544a
Update versioning logic
Apr 19, 2025
0683564
Trigger release.yml only on tag creation
Apr 19, 2025
1db58d6
Add restore step back to release.yml
Apr 19, 2025
ca644cb
Add `Generate Release Notes` step to release.yml
Apr 19, 2025
3b73681
Update README.md
Apr 19, 2025
002a2fe
Update README.md
Apr 19, 2025
76597fe
Removed wiki submodule
Apr 21, 2025
be7fb39
Merge branch 'main' of https://github.com/litenova/LiteBus
Apr 21, 2025
5fee30b
Add IQueryValidator[TQuery].cs
Apr 24, 2025
8c201e7
Add IQueryValidator[TQuery].cs
Apr 24, 2025
c336c33
Merge remote-tracking branch 'origin/main'
Apr 24, 2025
858a6a6
Update README.md
Apr 25, 2025
58725c8
Add social-preview.png to assets
Apr 29, 2025
ac724a8
refactor: remove nullable annotations from mediator interfaces
Apr 29, 2025
270130f
Bump gittools/actions from 3.2.0 to 3.2.1
dependabot[bot] May 1, 2025
b7ae1cd
Merge pull request #78 from litenova/dependabot/github_actions/gittoo…
May 1, 2025
44bc686
Bump xunit.runner.visualstudio from 3.0.2 to 3.1.0
dependabot[bot] May 5, 2025
797976a
Merge pull request #79 from litenova/dependabot/nuget/xunit.runner.vi…
May 5, 2025
380c72c
Replace FluentAssertions with AwesomeAssertions
Jun 15, 2025
a55cf8d
Bump gittools/actions from 3.2.1 to 4.0.0
dependabot[bot] Jul 1, 2025
209c435
feat: Add .NET 9 support while maintaining .NET 8 backward compatibility
Jul 1, 2025
5de0b70
Merge pull request #86 from litenova/dependabot/github_actions/gittoo…
litenova Jul 3, 2025
5335eea
Add support for incremental registration
Jul 8, 2025
5b31d3e
Revert gittools version to 3.2.1
Jul 8, 2025
e596748
Allow message registration to handle all non-System types
Jul 8, 2025
6db1059
Bump AwesomeAssertions and 4 others
dependabot[bot] Jul 14, 2025
7761284
fix: filter out non-instantiable types during DI registration
Jul 18, 2025
e6f7e83
Fix Changelog.md wrong version
Jul 18, 2025
710642a
Remove Extra TryAddTransient
Jul 18, 2025
5bab791
Merge pull request #89 from litenova/dependabot/nuget/src/multi-7ea33…
litenova Jul 18, 2025
c2434ad
Sign Assemblies
Jul 21, 2025
e5169f2
Merge remote-tracking branch 'origin/main'
Jul 21, 2025
67b6a0a
Introduce runtime layer
Jul 24, 2025
5c1ef97
Introduce parallel execution and rename order to priority
Jul 31, 2025
9aec347
Initial inbox design
Aug 5, 2025
eda82c4
feat: Add contextual metadata support to mediation pipeline
Aug 5, 2025
6b35bf0
Merge branch 'main' into v4
Aug 7, 2025
1b5165c
Add AutoFac
Aug 13, 2025
e1ab5ac
Add LiteBus.Runtime.Abstractions Project
Sep 18, 2025
ae6b5c3
Adds test coverage for:
Sep 18, 2025
a737d65
Add v4 to Changelog.md
Sep 18, 2025
4587c4b
Merge pull request #104 from litenova/v4
litenova Sep 18, 2025
c7c61bb
Adjust Decode Strong Name Key in build-and-test.yml
Sep 18, 2025
a0033a5
Set IsTestProject to False in LiteBus.Testing.csproj
Sep 18, 2025
7c7311b
Add manual-test-and-coverage.yml & Simplify release.yml
Sep 18, 2025
4acb727
Update README.md
litenova Sep 26, 2025
c80baf1
Update README.md
litenova Sep 26, 2025
1ce8f1f
Bump actions/setup-dotnet from 4 to 5
dependabot[bot] Oct 1, 2025
e8ce930
Add IStreamQueryPostHandler and fix stream context loss
Oct 30, 2025
84e9a16
Update Nuget Packages
Oct 30, 2025
ca8b803
Bump actions/upload-artifact from 4 to 5
dependabot[bot] Nov 1, 2025
cf10231
Merge pull request #116 from litenova/dependabot/github_actions/actio…
litenova Nov 5, 2025
b3ec15b
Merge pull request #107 from litenova/dependabot/github_actions/actio…
litenova Nov 5, 2025
ad954a7
Add DotNet Core example
savariamir Nov 10, 2025
0fe6368
Merge pull request #118 from litenova/add-dotnet-code-examples
savariamir Nov 10, 2025
facad81
Release v4.2.0
Nov 11, 2025
0fac20e
Update README.md
litenova Nov 14, 2025
49b1d2b
Bump NSwag.AspNetCore from 14.6.2 to 14.6.3
dependabot[bot] Nov 24, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Enable Nullable for all projects
  • Loading branch information
a.shafie committed Apr 13, 2025
commit 6e726f887a0201e0d15d91b6b9bff2a72debe322
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## v.0.25.0
- Enable `Nullable` for all projects.

## v.0.24.4

- Improve XML comments in the codebase.
Expand Down
4 changes: 2 additions & 2 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
<LangVersion>latest</LangVersion>
<Authors>A. Shafie</Authors>
<PackageTags>mediator;cqrs</PackageTags>
<VersionPrefix>0.24.4</VersionPrefix>
<VersionPrefix>0.25.0</VersionPrefix>
<PackageIcon>icon.png</PackageIcon>
<Description>LiteBus is an easy-to-use and ambitious in-process mediator providing the foundation to implement Command Query Separation (CQS). It is implemented with minimal reflection and instead utilizes covariance and contravariance to provide its core functionality.</Description>
<PackageProjectUrl>https://github.com/litenova/LiteBus</PackageProjectUrl>
<RepositoryUrl>https://github.com/litenova/LiteBus</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>
<PackageReadmeFile>README.md</PackageReadmeFile>
<ManagePackageVersionsCentrally>true</ManagePackageVersionsCentrally>
</PropertyGroup>
Expand Down
2 changes: 0 additions & 2 deletions src/LiteBus.Commands.Abstractions/CommandMediationSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#nullable enable

using System.Collections.Generic;

namespace LiteBus.Commands.Abstractions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ public static Task SendAsync(this ICommandMediator commandMediator,
/// var result = await commandMediator.SendAsync(myCommand, cancellationToken);
/// </code>
/// </example>
public static Task<TCommandResult> SendAsync<TCommandResult>(this ICommandMediator commandMediator,
ICommand<TCommandResult> command,
CancellationToken cancellationToken = default)
public static Task<TCommandResult?> SendAsync<TCommandResult>(this ICommandMediator commandMediator,
ICommand<TCommandResult> command,
CancellationToken cancellationToken = default)
{
return commandMediator.SendAsync(command, null, cancellationToken);
}
Expand Down Expand Up @@ -73,7 +73,7 @@ public static Task SendAsync(this ICommandMediator commandMediator,
{
Filters =
{
Tags = new[] { tag }
Tags = [tag]
}
},
cancellationToken);
Expand All @@ -94,17 +94,17 @@ public static Task SendAsync(this ICommandMediator commandMediator,
/// var result = await commandMediator.SendAsync(myCommand, "UserAction", cancellationToken);
/// </code>
/// </example>
public static Task<TCommandResult> SendAsync<TCommandResult>(this ICommandMediator commandMediator,
ICommand<TCommandResult> command,
string tag,
CancellationToken cancellationToken = default)
public static Task<TCommandResult?> SendAsync<TCommandResult>(this ICommandMediator commandMediator,
ICommand<TCommandResult> command,
string tag,
CancellationToken cancellationToken = default)
{
return commandMediator.SendAsync(command,
new CommandMediationSettings
{
Filters =
{
Tags = new[] { tag }
Tags = [tag]
}
},
cancellationToken);
Expand Down
7 changes: 4 additions & 3 deletions src/LiteBus.Commands.Abstractions/ICommandMediator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#nullable enable
using System.Threading;
using System.Threading;
using System.Threading.Tasks;

namespace LiteBus.Commands.Abstractions;
Expand Down Expand Up @@ -46,5 +45,7 @@ public interface ICommandMediator
/// is executed, including pre-handlers, the main handler, post-handlers, and error handlers if exceptions occur.
/// The result produced by the handler is returned to the caller.
/// </remarks>
Task<TCommandResult> SendAsync<TCommandResult>(ICommand<TCommandResult> command, CommandMediationSettings? commandMediationSettings = null, CancellationToken cancellationToken = default);
Task<TCommandResult?> SendAsync<TCommandResult>(ICommand<TCommandResult> command,
CommandMediationSettings? commandMediationSettings = null,
CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ namespace LiteBus.Commands.Abstractions;
/// They have access to both the original command and its result, allowing for operations such as
/// result transformation, logging, caching, or other processing that depends on the command outcome.
/// </remarks>
public interface ICommandPostHandler<in TCommand, in TCommandResult> : IRegistrableCommandConstruct, IAsyncMessagePostHandler<TCommand, TCommandResult> where TCommand : ICommand<TCommandResult>;
public interface ICommandPostHandler<in TCommand, in TCommandResult> : IRegistrableCommandConstruct, IAsyncMessagePostHandler<TCommand, TCommandResult>
where TCommand : ICommand<TCommandResult>
where TCommandResult : notnull;
16 changes: 7 additions & 9 deletions src/LiteBus.Commands/CommandMediator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#nullable enable

using System.Threading;
using System.Threading;
using System.Threading.Tasks;
using LiteBus.Commands.Abstractions;
using LiteBus.Messaging.Abstractions;
Expand All @@ -18,7 +16,7 @@ public CommandMediator(IMessageMediator messageMediator)
}

public Task SendAsync(ICommand command,
CommandMediationSettings? commandMediationSettings = default,
CommandMediationSettings? commandMediationSettings = null,
CancellationToken cancellationToken = default)
{
commandMediationSettings ??= new CommandMediationSettings();
Expand All @@ -37,15 +35,15 @@ public Task SendAsync(ICommand command,
return _messageMediator.Mediate(command, options);
}

public Task<TCommandResult> SendAsync<TCommandResult>(ICommand<TCommandResult> command,
CommandMediationSettings? commandMediationSettings = null,
CancellationToken cancellationToken = default)
public Task<TCommandResult?> SendAsync<TCommandResult>(ICommand<TCommandResult> command,
CommandMediationSettings? commandMediationSettings = null,
CancellationToken cancellationToken = default)
{
commandMediationSettings ??= new CommandMediationSettings();
var mediationStrategy = new SingleAsyncHandlerMediationStrategy<ICommand<TCommandResult>, TCommandResult>();
var mediationStrategy = new SingleAsyncHandlerMediationStrategy<ICommand<TCommandResult>, TCommandResult?>();
var findStrategy = new ActualTypeOrFirstAssignableTypeMessageResolveStrategy();

var options = new MediateOptions<ICommand<TCommandResult>, Task<TCommandResult>>
var options = new MediateOptions<ICommand<TCommandResult>, Task<TCommandResult?>>
{
MessageResolveStrategy = findStrategy,
MessageMediationStrategy = mediationStrategy,
Expand Down
2 changes: 0 additions & 2 deletions src/LiteBus.Events.Abstractions/EventMediationSettings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#nullable enable

using System;
using System.Collections.Generic;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static Task PublishAsync(this IEventMediator eventMediator, IEvent @event
{
Filters =
{
Tags = new[] { tag }
Tags = [tag]
}
},
cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ namespace LiteBus.Events.Abstractions;
/// This allows for implementing specialized error handling strategies for different event types,
/// such as custom recovery logic, retry policies, or specific error reporting for critical events.
/// </remarks>
public interface IEventErrorHandler<in TEvent> : IAsyncMessageErrorHandler<TEvent, object>, IRegistrableEventConstruct;
public interface IEventErrorHandler<in TEvent> : IAsyncMessageErrorHandler<TEvent, object>, IRegistrableEventConstruct where TEvent : notnull;
2 changes: 1 addition & 1 deletion src/LiteBus.Events.Abstractions/IEventHandler[TEvent].cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ namespace LiteBus.Events.Abstractions;
/// execute asynchronously and can perform operations such as updating projections, notifying external
/// systems, or triggering additional workflows.
/// </remarks>
public interface IEventHandler<in TEvent> : IAsyncMessageHandler<TEvent>, IRegistrableEventConstruct;
public interface IEventHandler<in TEvent> : IAsyncMessageHandler<TEvent>, IRegistrableEventConstruct where TEvent : notnull;
3 changes: 1 addition & 2 deletions src/LiteBus.Events.Abstractions/IEventMediator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#nullable enable
using System.Threading;
using System.Threading;
using System.Threading.Tasks;

namespace LiteBus.Events.Abstractions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ namespace LiteBus.Events.Abstractions;
/// only to the specified event type. Multiple type-specific post-handlers can be registered for each event type
/// and will execute in sequence after the event handlers have processed the event.
/// </remarks>
public interface IEventPostHandler<in TEvent> : IAsyncMessagePostHandler<TEvent>, IRegistrableEventConstruct;
public interface IEventPostHandler<in TEvent> : IAsyncMessagePostHandler<TEvent>, IRegistrableEventConstruct where TEvent : notnull;
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ namespace LiteBus.Events.Abstractions;
/// that apply only to the specified event type. Multiple type-specific pre-handlers can be
/// registered for each event type and will execute in sequence before the actual event handlers.
/// </remarks>
public interface IEventPreHandler<in TEvent> : IAsyncMessagePreHandler<TEvent>, IRegistrableEventConstruct;
public interface IEventPreHandler<in TEvent> : IAsyncMessagePreHandler<TEvent>, IRegistrableEventConstruct where TEvent : notnull;
4 changes: 1 addition & 3 deletions src/LiteBus.Events/EventMediator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#nullable enable

using System.Threading;
using System.Threading;
using System.Threading.Tasks;
using LiteBus.Events.Abstractions;
using LiteBus.Events.MediationStrategies;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#nullable enable

using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ExceptionServices;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable
using System;
using System.Threading;
using System.Threading.Tasks;

namespace LiteBus.Messaging.Abstractions;

Expand Down Expand Up @@ -27,10 +28,99 @@ public static class AmbientExecutionContext
/// even across asynchronous operations.
///
/// Setting this property to null clears the ambient execution context.
///
/// Accessing this property when no context has been set will throw a <see cref="NoExecutionContextException"/>.
/// </remarks>
public static IExecutionContext? Current
/// <exception cref="NoExecutionContextException">Thrown when attempting to get the current context when none has been set.</exception>
public static IExecutionContext Current
{
get => ExecutionContextLocal.Value;
get => ExecutionContextLocal.Value ?? throw new NoExecutionContextException();
set => ExecutionContextLocal.Value = value;
}

/// <summary>
/// Determines whether a current execution context exists.
/// </summary>
/// <value>
/// <c>true</c> if a current execution context exists; otherwise, <c>false</c>.
/// </value>
public static bool HasCurrent => ExecutionContextLocal.Value != null;

/// <summary>
/// Gets the current execution context if it exists, or returns null if no context is set.
/// </summary>
/// <returns>The current execution context or null if no context has been set.</returns>
public static IExecutionContext? GetCurrentOrDefault() => ExecutionContextLocal.Value;

/// <summary>
/// Creates a new execution context scope that automatically restores the previous context when disposed.
/// </summary>
/// <param name="context">The execution context to use within the scope.</param>
/// <returns>A disposable scope that restores the previous context when disposed.</returns>
/// <example>
/// <code>
/// // Create a new execution context
/// var executionContext = new ExecutionContext(CancellationToken.None);
///
/// // Use the context within a scope
/// using (AmbientExecutionContext.CreateScope(executionContext))
/// {
/// // Code here has access to the execution context via AmbientExecutionContext.Current
/// var currentContext = AmbientExecutionContext.Current;
///
/// // Perform operations with the context
/// }
/// // Previous context (if any) is automatically restored here
/// </code>
/// </example>
public static ExecutionContextScope CreateScope(IExecutionContext context)
{
return new ExecutionContextScope(context);
}

/// <summary>
/// Represents a scope for an execution context that automatically restores the previous context when disposed.
/// </summary>
/// <remarks>
/// This class implements both <see cref="IDisposable"/> and <see cref="IAsyncDisposable"/> to support
/// both synchronous and asynchronous usage patterns. When the scope is disposed, the previous execution
/// context (if any) is restored.
/// </remarks>
public sealed class ExecutionContextScope : IDisposable, IAsyncDisposable
{
private readonly IExecutionContext? _previousContext;
private bool _disposed;

/// <summary>
/// Initializes a new instance of the <see cref="ExecutionContextScope"/> class.
/// </summary>
/// <param name="newContext">The new execution context to set as current during this scope.</param>
internal ExecutionContextScope(IExecutionContext newContext)
{
_previousContext = GetCurrentOrDefault();
ExecutionContextLocal.Value = newContext;
}

/// <summary>
/// Disposes the scope and restores the previous execution context.
/// </summary>
public void Dispose()
{
if (_disposed) return;

// This assignment is safe because ExecutionContextLocal.Value accepts nullable values
ExecutionContextLocal.Value = _previousContext;
_disposed = true;
}

/// <summary>
/// Asynchronously disposes the scope and restores the previous execution context.
/// </summary>
/// <returns>A <see cref="ValueTask"/> representing the asynchronous dispose operation.</returns>
public ValueTask DisposeAsync()
{
Dispose();
return ValueTask.CompletedTask;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable enable
using System.Collections.Generic;
using System.Threading;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static async Task RunAsyncPreHandlers(this IMessageDependencies messageDe
/// <param name="messageResult">The result of the message handling process, if any.</param>
/// <param name="exceptionDispatchInfo">The exception that triggered the error handler.</param>
/// <returns>A Task representing the asynchronous operation.</returns>
public static async Task RunAsyncErrorHandlers(this IMessageDependencies messageDependencies, object message, object messageResult, ExceptionDispatchInfo exceptionDispatchInfo)
public static async Task RunAsyncErrorHandlers(this IMessageDependencies messageDependencies, object message, object? messageResult, ExceptionDispatchInfo exceptionDispatchInfo)
{
if (messageDependencies.ErrorHandlers.Count + messageDependencies.IndirectErrorHandlers.Count == 0)
{
Expand All @@ -61,7 +61,7 @@ public static async Task RunAsyncErrorHandlers(this IMessageDependencies message
/// <param name="message">The message that has been handled.</param>
/// <param name="messageResult">The result produced by the message handling process.</param>
/// <returns>A Task representing the asynchronous operation.</returns>
public static async Task RunAsyncPostHandlers(this IMessageDependencies messageDependencies, object message, object messageResult)
public static async Task RunAsyncPostHandlers(this IMessageDependencies messageDependencies, object message, object? messageResult)
{
foreach (var postHandler in messageDependencies.PostHandlers)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#nullable enable
using System;
using System;
using System.Linq;

namespace LiteBus.Messaging.Abstractions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#nullable enable

using System;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -13,7 +11,7 @@ namespace LiteBus.Messaging.Abstractions;
/// </summary>
/// <typeparam name="TMessage">The type of the message that this error handler is applicable to.</typeparam>
/// <typeparam name="TMessageResult">The type of the result produced by the message processing.</typeparam>
public interface IAsyncMessageErrorHandler<in TMessage, in TMessageResult> : IMessageErrorHandler<TMessage, TMessageResult>
public interface IAsyncMessageErrorHandler<in TMessage, in TMessageResult> : IMessageErrorHandler<TMessage, TMessageResult> where TMessage : notnull
{
/// <summary>
/// Synchronously handles an error encountered in message processing by delegating to an asynchronous method.
Expand All @@ -24,7 +22,7 @@ public interface IAsyncMessageErrorHandler<in TMessage, in TMessageResult> : IMe
/// <returns>A placeholder object returned after handling the error.</returns>
object IMessageErrorHandler<TMessage, TMessageResult>.HandleError(TMessage message, Exception exception, TMessageResult? messageResult)
{
return HandleErrorAsync(message, messageResult, exception, AmbientExecutionContext.Current?.CancellationToken ?? throw new NoExecutionContextException());
return HandleErrorAsync(message, messageResult, exception, AmbientExecutionContext.Current.CancellationToken);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#nullable enable

using System;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -11,7 +9,7 @@ namespace LiteBus.Messaging.Abstractions;
/// This interface should be implemented to handle exceptions that occur during message processing.
/// </summary>
/// <typeparam name="TMessage">The type of the message that this error handler is applicable to.</typeparam>
public interface IAsyncMessageErrorHandler<in TMessage> : IMessageErrorHandler<TMessage, object>
public interface IAsyncMessageErrorHandler<in TMessage> : IMessageErrorHandler<TMessage, object> where TMessage : notnull
{
/// <summary>
/// Synchronously handles an error encountered in message processing by delegating to an asynchronous method.
Expand All @@ -22,7 +20,7 @@ public interface IAsyncMessageErrorHandler<in TMessage> : IMessageErrorHandler<T
/// <returns>A placeholder object returned after handling the error.</returns>
object IMessageErrorHandler<TMessage, object>.HandleError(TMessage message, Exception exception, object? messageResult)
{
return HandleErrorAsync(message, messageResult, exception, AmbientExecutionContext.Current?.CancellationToken ?? throw new NoExecutionContextException());
return HandleErrorAsync(message, messageResult, exception, AmbientExecutionContext.Current.CancellationToken);
}

/// <summary>
Expand Down
Loading