Skip to content

Commit b81706c

Browse files
authored
Merge pull request #7 from cnblogs/check-if-ieventbus-is-registered
refactor: check if IEventBus is registered
2 parents 1b6bf64 + 5270c2b commit b81706c

File tree

6 files changed

+47
-10
lines changed

6 files changed

+47
-10
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,7 @@ public class DaprOptions
1818
/// <summary>
1919
/// 是否调用过 <c>app.MapSubscribeHandler()</c>
2020
/// </summary>
21-
internal static bool IsDaprSubscribeHandlerMapped { get; set; }
21+
internal bool IsDaprSubscribeHandlerMapped { get; set; }
22+
23+
internal bool IsEventBusRegistered { get; set; }
2224
}

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
33
using Cnblogs.Architecture.Ddd.EventBus.Dapr;
44
using Microsoft.AspNetCore.Builder;
5+
using Microsoft.Extensions.DependencyInjection;
6+
using Microsoft.Extensions.Options;
57

68
// ReSharper disable once CheckNamespace
79
namespace Microsoft.AspNetCore.Routing;
@@ -61,7 +63,10 @@ public static IEndpointConventionBuilder Subscribe<TEvent>(
6163
string appName)
6264
where TEvent : IntegrationEvent
6365
{
64-
EnsureDaprSubscribeHandlerMapped(builder);
66+
var daprOptions = builder.ServiceProvider.GetRequiredService<IOptions<DaprOptions>>().Value;
67+
EnsureDaprSubscribeHandlerMapped(builder, daprOptions);
68+
EnsureEventBusRegistered(builder, daprOptions);
69+
6570
var result = builder
6671
.MapPost(route, (TEvent receivedEvent, IEventBus eventBus) => eventBus.ReceiveAsync(receivedEvent))
6772
.WithTopic(DaprOptions.PubSubName, DaprUtils.GetDaprTopicName<TEvent>(appName));
@@ -95,9 +100,26 @@ public static void Subscribe(this IEndpointRouteBuilder builder, params Assembly
95100
}
96101
}
97102

98-
private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder builder)
103+
private static void EnsureEventBusRegistered(IEndpointRouteBuilder builder, DaprOptions daprOptions)
104+
{
105+
if (daprOptions.IsEventBusRegistered)
106+
{
107+
return;
108+
}
109+
110+
var serviceCheck = builder.ServiceProvider.GetRequiredService<IServiceProviderIsService>();
111+
if (!serviceCheck.IsService(typeof(IEventBus)))
112+
{
113+
throw new InvalidOperationException(
114+
$"{nameof(IEventBus)} has not been registered. Did you forget to call IServiceCollection.AddDaprEventBus()?");
115+
}
116+
117+
daprOptions.IsEventBusRegistered = true;
118+
}
119+
120+
private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder builder, DaprOptions daprOptions)
99121
{
100-
if (DaprOptions.IsDaprSubscribeHandlerMapped)
122+
if (daprOptions.IsDaprSubscribeHandlerMapped)
101123
{
102124
return;
103125
}
@@ -108,6 +130,6 @@ private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder build
108130
}
109131

110132
builder.MapSubscribeHandler();
111-
DaprOptions.IsDaprSubscribeHandlerMapped = true;
133+
daprOptions.IsDaprSubscribeHandlerMapped = true;
112134
}
113135
}

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)