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 dca799d..b22b860 100644
--- a/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs
+++ b/src/Cnblogs.Architecture.Ddd.EventBus.Dapr/EndPointExtensions.cs
@@ -3,6 +3,7 @@
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;
@@ -62,14 +63,9 @@ public static IEndpointConventionBuilder Subscribe(
string appName)
where TEvent : IntegrationEvent
{
- EnsureDaprSubscribeHandlerMapped(builder);
-
- 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()?");
- }
+ var daprOptions = builder.ServiceProvider.GetRequiredService>().Value;
+ EnsureDaprSubscribeHandlerMapped(builder, daprOptions);
+ EnsureEventBusRegistered(builder, daprOptions);
var result = builder
.MapPost(route, (TEvent receivedEvent, IEventBus eventBus) => eventBus.ReceiveAsync(receivedEvent))
@@ -104,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;
}
@@ -117,6 +130,6 @@ private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder build
}
builder.MapSubscribeHandler();
- DaprOptions.IsDaprSubscribeHandlerMapped = true;
+ daprOptions.IsDaprSubscribeHandlerMapped = true;
}
}
\ No newline at end of file