-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Host] Introduce IMessageScopeAccessor to wrap the MessageScope provi…
…der #222 Signed-off-by: Tomasz Maruszak <maruszaktomasz@gmail.com>
- Loading branch information
Showing
9 changed files
with
138 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace SlimMessageBus.Host.Consumer; | ||
|
||
/// <summary> | ||
/// Allows to get ahold of the <see cref="IServiceProvider"/> for the current message scope. | ||
/// </summary> | ||
public interface IMessageScopeAccessor | ||
{ | ||
/// <summary> | ||
/// If the running code is within a message scope of a consumer, this property will return the <see cref="IServiceProvider"/> for the current message scope. | ||
/// Otherwise it will return null. | ||
/// </summary> | ||
IServiceProvider Current { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace SlimMessageBus.Host.Consumer; | ||
|
||
internal sealed class MessageScopeAccessor : IMessageScopeAccessor | ||
{ | ||
public IServiceProvider Current => MessageScope.Current; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...ts/SlimMessageBus.Host.Integration.Test/MessageScopeAccessor/MessageScopeAccessorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
namespace SlimMessageBus.Host.Integration.Test.MessageScopeAccessor; | ||
|
||
using SlimMessageBus.Host; | ||
using SlimMessageBus.Host.Consumer; | ||
using SlimMessageBus.Host.Memory; | ||
|
||
/// <summary> | ||
/// This test verifies that the <see cref="IMessageScopeAccessor"/> correctly looks up the <see cref="IServiceProvider"/> for the current message scope. | ||
/// </summary> | ||
/// <param name="testOutputHelper"></param> | ||
[Trait("Category", "Integration")] | ||
public class MessageScopeAccessorTests(ITestOutputHelper testOutputHelper) | ||
: BaseIntegrationTest<MessageScopeAccessorTests>(testOutputHelper) | ||
{ | ||
protected override void SetupServices(ServiceCollection services, IConfigurationRoot configuration) | ||
{ | ||
services.AddSlimMessageBus(mbb => | ||
{ | ||
mbb.AddChildBus("Memory", builder => | ||
{ | ||
builder | ||
.WithProviderMemory() | ||
.AutoDeclareFrom(Assembly.GetExecutingAssembly(), t => t.Namespace.Contains("MessageScopeAccessor")) | ||
.PerMessageScopeEnabled(); | ||
}); | ||
mbb.AddServicesFromAssemblyContaining<TestMessageConsumer>(); | ||
}); | ||
services.AddScoped<TestValueHolder>(); | ||
} | ||
|
||
[Fact] | ||
public async Task Given_MemoryConsumer_When_MessageScopeAccessorCalledInsideConsumer_Then_LooksUpInTheMessageScope() | ||
{ | ||
// Arrange | ||
using var scope = ServiceProvider.CreateScope(); | ||
var bus = scope.ServiceProvider.GetRequiredService<IMessageBus>(); | ||
|
||
var value = Guid.NewGuid(); | ||
|
||
// Act | ||
await bus.Publish(new TestMessage(value)); | ||
|
||
// Assert | ||
var holder = scope.ServiceProvider.GetRequiredService<TestValueHolder>(); | ||
holder.ServiceProvider.Should().BeSameAs(holder.MessageScopeAccessorServiceProvider); | ||
} | ||
|
||
public record TestMessage(Guid Value); | ||
|
||
public class TestMessageConsumer(TestValueHolder holder, IServiceProvider serviceProvider, IMessageScopeAccessor messageScopeAccessor) : IRequestHandler<TestMessage> | ||
{ | ||
public Task OnHandle(TestMessage request) | ||
{ | ||
holder.ServiceProvider = serviceProvider; | ||
holder.MessageScopeAccessorServiceProvider = messageScopeAccessor.Current; | ||
return Task.CompletedTask; | ||
} | ||
} | ||
|
||
public class TestValueHolder | ||
{ | ||
public IServiceProvider ServiceProvider { get; set; } | ||
public IServiceProvider MessageScopeAccessorServiceProvider { get; set; } | ||
} | ||
|
||
} |