diff --git a/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/DaprOptions.cs b/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/DaprOptions.cs
index 6236c33..ce21a14 100644
--- a/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/DaprOptions.cs
+++ b/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/DaprOptions.cs
@@ -18,5 +18,7 @@ public class DaprOptions
///
/// 是否调用过 app.MapSubscribeHandler()
///
- internal static bool IsDaprSubscribeHandlerMapped { get; set; }
+ internal bool IsDaprSubscribeHandlerMapped { get; set; }
+
+ internal bool IsEventBusRegistered { get; set; }
}
\ No newline at end of file
diff --git a/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs b/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs
index 6ca512b..b22b860 100644
--- a/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs
+++ b/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs
@@ -2,6 +2,8 @@
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
using Cnblogs.Architecture.Ddd.EventBus.Dapr;
using Microsoft.AspNetCore.Builder;
+using Microsoft.Extensions.DependencyInjection;
+using Microsoft.Extensions.Options;
// ReSharper disable once CheckNamespace
namespace Microsoft.AspNetCore.Routing;
@@ -61,7 +63,10 @@ public static IEndpointConventionBuilder Subscribe(
string appName)
where TEvent : IntegrationEvent
{
- EnsureDaprSubscribeHandlerMapped(builder);
+ var daprOptions = builder.ServiceProvider.GetRequiredService>().Value;
+ EnsureDaprSubscribeHandlerMapped(builder, daprOptions);
+ EnsureEventBusRegistered(builder, daprOptions);
+
var result = builder
.MapPost(route, (TEvent receivedEvent, IEventBus eventBus) => eventBus.ReceiveAsync(receivedEvent))
.WithTopic(DaprOptions.PubSubName, DaprUtils.GetDaprTopicName(appName));
@@ -95,9 +100,26 @@ public static void Subscribe(this IEndpointRouteBuilder builder, params Assembly
}
}
- private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder builder)
+ private static void EnsureEventBusRegistered(IEndpointRouteBuilder builder, DaprOptions daprOptions)
+ {
+ if (daprOptions.IsEventBusRegistered)
+ {
+ return;
+ }
+
+ var serviceCheck = builder.ServiceProvider.GetRequiredService();
+ if (!serviceCheck.IsService(typeof(IEventBus)))
+ {
+ throw new InvalidOperationException(
+ $"{nameof(IEventBus)} has not been registered. Did you forget to call IServiceCollection.AddDaprEventBus()?");
+ }
+
+ daprOptions.IsEventBusRegistered = true;
+ }
+
+ private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder builder, DaprOptions daprOptions)
{
- if (DaprOptions.IsDaprSubscribeHandlerMapped)
+ if (daprOptions.IsDaprSubscribeHandlerMapped)
{
return;
}
@@ -108,6 +130,6 @@ private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder build
}
builder.MapSubscribeHandler();
- DaprOptions.IsDaprSubscribeHandlerMapped = true;
+ daprOptions.IsDaprSubscribeHandlerMapped = true;
}
}
\ No newline at end of file
diff --git a/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs b/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs
index da3439a..bd06489 100644
--- a/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs
+++ b/test/Cnblogs.Architecture.IntegrationTestProject/Program.cs
@@ -1,7 +1,5 @@
using Cnblogs.Architecture.Ddd.Cqrs.AspNetCore;
-using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection;
using Cnblogs.Architecture.Ddd.Cqrs.DependencyInjection.EventBus.Dapr;
-using Cnblogs.Architecture.Ddd.EventBus.Dapr;
using Cnblogs.Architecture.IntegrationTestProject.Application.Commands;
using Cnblogs.Architecture.IntegrationTestProject.Application.Queries;
using Cnblogs.Architecture.IntegrationTestProject.Payloads;
diff --git a/test/Cnblogs.Architecture.TestIntegrationEvents/AssemblyInfo.cs b/test/Cnblogs.Architecture.TestIntegrationEvents/AssemblyInfo.cs
index a804d50..3f39a5c 100644
--- a/test/Cnblogs.Architecture.TestIntegrationEvents/AssemblyInfo.cs
+++ b/test/Cnblogs.Architecture.TestIntegrationEvents/AssemblyInfo.cs
@@ -1,3 +1,3 @@
using Cnblogs.Architecture.Ddd.EventBus.Abstractions;
-[assembly:AssemblyAppName("test")]
\ No newline at end of file
+[assembly: AssemblyAppName("test")]
\ No newline at end of file
diff --git a/test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj b/test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj
index 654b351..3afa87b 100644
--- a/test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj
+++ b/test/Cnblogs.Architecture.UnitTests/Cnblogs.Architecture.UnitTests.csproj
@@ -15,6 +15,7 @@
+
diff --git a/test/Cnblogs.Architecture.UnitTests/EventBus/AssemblyAttributeTests.cs b/test/Cnblogs.Architecture.UnitTests/EventBus/AssemblyAttributeTests.cs
index 1c37ed7..2a7c7e7 100644
--- a/test/Cnblogs.Architecture.UnitTests/EventBus/AssemblyAttributeTests.cs
+++ b/test/Cnblogs.Architecture.UnitTests/EventBus/AssemblyAttributeTests.cs
@@ -1,9 +1,8 @@
using Cnblogs.Architecture.TestIntegrationEvents;
-
using FluentAssertions;
-
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
+using Microsoft.Extensions.DependencyInjection;
namespace Cnblogs.Architecture.UnitTests.EventBus;
@@ -14,6 +13,7 @@ public void SubscribeByAssemblyMeta_Success()
{
// Arrange
var builder = WebApplication.CreateBuilder();
+ builder.Services.AddDaprEventBus(nameof(AssemblyAttributeTests));
var app = builder.Build();
// Act
@@ -22,4 +22,18 @@ public void SubscribeByAssemblyMeta_Success()
// Assert
act.Should().NotThrow();
}
+
+ [Fact]
+ public void SubscribeByAssemblyMeta_Throw()
+ {
+ // Arrange
+ var builder = WebApplication.CreateBuilder();
+ var app = builder.Build();
+
+ // Act
+ var act = () => app.Subscribe();
+
+ // Assert
+ act.Should().Throw();
+ }
}
\ No newline at end of file