Skip to content

Commit 9359606

Browse files
committed
refactor: check if IEventBus is registered
1 parent 1b6bf64 commit 9359606

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
33
using Cnblogs.Architecture.Ddd.EventBus.Dapr;
44
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.Extensions.DependencyInjection;
56

67
// ReSharper disable once CheckNamespace
78
namespace Microsoft.AspNetCore.Routing;
@@ -62,6 +63,14 @@ public static IEndpointConventionBuilder Subscribe<TEvent>(
6263
where TEvent : IntegrationEvent
6364
{
6465
EnsureDaprSubscribeHandlerMapped(builder);
66+
67+
var serviceCheck = builder.ServiceProvider.GetRequiredService<IServiceProviderIsService>();
68+
if (!serviceCheck.IsService(typeof(IEventBus)))
69+
{
70+
throw new InvalidOperationException(
71+
$"{nameof(IEventBus)} has not been registered. Please using AddDaprEventBus to register.");
72+
}
73+
6574
var result = builder
6675
.MapPost(route, (TEvent receivedEvent, IEventBus eventBus) => eventBus.ReceiveAsync(receivedEvent))
6776
.WithTopic(DaprOptions.PubSubName, DaprUtils.GetDaprTopicName<TEvent>(appName));

test/Cnblogs.Architecture.IntegrationTestProject/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using Cnblogs.Architecture.Ddd.Cqrs.AspNetCore;
2-
using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection;
32
using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr;
4-
using Cnblogs.Architecture.Ddd.EventBus.Dapr;
53
using Cnblogs.Architecture.IntegrationTestProject.Application.Commands;
64
using Cnblogs.Architecture.IntegrationTestProject.Application.Queries;
75
using Cnblogs.Architecture.IntegrationTestProject.Payloads;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
22

3-
[assembly:AssemblyAppName("test")]
3+
[assembly: AssemblyAppName("test")]

test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18+
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr\Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr.csproj" />
1819
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.EntityFramework\Cnblogs.Architecture.Ddd.Cqrs.EntityFramework.csproj" />
1920
<ProjectReference Include="..\..\src\Cnblogs.Architecture.Ddd.Cqrs.MongoDb\Cnblogs.Architecture.Ddd.Cqrs.MongoDb.csproj" />
2021
<ProjectReference Include="..\Cnblogs.Architecture.TestIntegrationEvents\Cnblogs.Architecture.TestIntegrationEvents.csproj" />

test/Cnblogs.Architecture.UnitTests/EventBus/AssemblyAttributeTests.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using Cnblogs.Architecture.TestIntegrationEvents;
2-
32
using FluentAssertions;
4-
53
using Microsoft.AspNetCore.Builder;
64
using Microsoft.AspNetCore.Routing;
5+
using Microsoft.Extensions.DependencyInjection;
76

87
namespace Cnblogs.Architecture.UnitTests.EventBus;
98

@@ -14,6 +13,7 @@ public void SubscribeByAssemblyMeta_Success()
1413
{
1514
// Arrange
1615
var builder = WebApplication.CreateBuilder();
16+
builder.Services.AddDaprEventBus(nameof(AssemblyAttributeTests));
1717
var app = builder.Build();
1818

1919
// Act
@@ -22,4 +22,18 @@ public void SubscribeByAssemblyMeta_Success()
2222
// Assert
2323
act.Should().NotThrow();
2424
}
25+
26+
[Fact]
27+
public void SubscribeByAssemblyMeta_Throw()
28+
{
29+
// Arrange
30+
var builder = WebApplication.CreateBuilder();
31+
var app = builder.Build();
32+
33+
// Act
34+
var act = () => app.Subscribe<TestIntegrationEvent>();
35+
36+
// Assert
37+
act.Should().Throw<InvalidOperationException>();
38+
}
2539
}

0 commit comments

Comments
 (0)