3
3
using Cnblogs . Architecture . Ddd . EventBus . Dapr ;
4
4
using Microsoft . AspNetCore . Builder ;
5
5
using Microsoft . Extensions . DependencyInjection ;
6
+ using Microsoft . Extensions . Options ;
6
7
7
8
// ReSharper disable once CheckNamespace
8
9
namespace Microsoft . AspNetCore . Routing ;
@@ -62,14 +63,9 @@ public static IEndpointConventionBuilder Subscribe<TEvent>(
62
63
string appName )
63
64
where TEvent : IntegrationEvent
64
65
{
65
- 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. Did you forget to call IServiceCollection.AddDaprEventBus()?") ;
72
- }
66
+ var daprOptions = builder . ServiceProvider . GetRequiredService < IOptions < DaprOptions > > ( ) . Value ;
67
+ EnsureDaprSubscribeHandlerMapped ( builder , daprOptions ) ;
68
+ EnsureEventBusRegistered ( builder , daprOptions ) ;
73
69
74
70
var result = builder
75
71
. MapPost ( route , ( TEvent receivedEvent , IEventBus eventBus ) => eventBus . ReceiveAsync ( receivedEvent ) )
@@ -104,9 +100,26 @@ public static void Subscribe(this IEndpointRouteBuilder builder, params Assembly
104
100
}
105
101
}
106
102
107
- 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 )
108
121
{
109
- if ( DaprOptions . IsDaprSubscribeHandlerMapped )
122
+ if ( daprOptions . IsDaprSubscribeHandlerMapped )
110
123
{
111
124
return ;
112
125
}
@@ -117,6 +130,6 @@ private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder build
117
130
}
118
131
119
132
builder . MapSubscribeHandler ( ) ;
120
- DaprOptions . IsDaprSubscribeHandlerMapped = true ;
133
+ daprOptions . IsDaprSubscribeHandlerMapped = true ;
121
134
}
122
135
}
0 commit comments