@@ -63,11 +63,7 @@ public static IEndpointConventionBuilder Subscribe<TEvent>(
6363 string appName )
6464 where TEvent : IntegrationEvent
6565 {
66- var daprOptions = builder . ServiceProvider . GetRequiredService < IOptions < DaprOptions > > ( ) . Value ;
67- EnsureDaprSubscribeHandlerMapped ( builder , daprOptions ) ;
68- EnsureEventBusRegistered ( builder , daprOptions ) ;
69-
70- var result = builder
66+ var result = builder . EnsureProvision ( )
7167 . MapPost ( route , ( TEvent receivedEvent , IEventBus eventBus ) => eventBus . ReceiveAsync ( receivedEvent ) )
7268 . WithTopic ( DaprOptions . PubSubName , DaprUtils . GetDaprTopicName < TEvent > ( appName ) ) ;
7369 return result ;
@@ -80,6 +76,8 @@ public static IEndpointConventionBuilder Subscribe<TEvent>(
8076 /// <param name="assemblies"><see cref="Assembly"/></param>
8177 public static void Subscribe ( this IEndpointRouteBuilder builder , params Assembly [ ] assemblies )
8278 {
79+ builder . EnsureProvision ( ) ;
80+
8381 var method = typeof ( EndPointExtensions ) . GetMethod (
8482 nameof ( Subscribe ) ,
8583 new [ ] { typeof ( IEndpointRouteBuilder ) , typeof ( string ) } ) ! ;
@@ -100,11 +98,11 @@ public static void Subscribe(this IEndpointRouteBuilder builder, params Assembly
10098 }
10199 }
102100
103- private static void EnsureEventBusRegistered ( IEndpointRouteBuilder builder , DaprOptions daprOptions )
101+ private static DaprOptions EnsureEventBusRegistered ( this DaprOptions daprOptions , IEndpointRouteBuilder builder )
104102 {
105103 if ( daprOptions . IsEventBusRegistered )
106104 {
107- return ;
105+ return daprOptions ;
108106 }
109107
110108 var serviceCheck = builder . ServiceProvider . GetRequiredService < IServiceProviderIsService > ( ) ;
@@ -115,13 +113,14 @@ private static void EnsureEventBusRegistered(IEndpointRouteBuilder builder, Dapr
115113 }
116114
117115 daprOptions . IsEventBusRegistered = true ;
116+ return daprOptions ;
118117 }
119118
120- private static void EnsureDaprSubscribeHandlerMapped ( IEndpointRouteBuilder builder , DaprOptions daprOptions )
119+ private static DaprOptions EnsureDaprSubscribeHandlerMapped ( this DaprOptions daprOptions , IEndpointRouteBuilder builder )
121120 {
122121 if ( daprOptions . IsDaprSubscribeHandlerMapped )
123122 {
124- return ;
123+ return daprOptions ;
125124 }
126125
127126 if ( builder is IApplicationBuilder app )
@@ -131,5 +130,17 @@ private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder build
131130
132131 builder . MapSubscribeHandler ( ) ;
133132 daprOptions . IsDaprSubscribeHandlerMapped = true ;
133+ return daprOptions ;
134+ }
135+
136+ private static DaprOptions GetDaprOptions ( this IEndpointRouteBuilder builder )
137+ => builder . ServiceProvider . GetRequiredService < IOptions < DaprOptions > > ( ) . Value ;
138+
139+ private static IEndpointRouteBuilder EnsureProvision ( this IEndpointRouteBuilder builder )
140+ {
141+ builder . GetDaprOptions ( )
142+ . EnsureDaprSubscribeHandlerMapped ( builder )
143+ . EnsureEventBusRegistered ( builder ) ;
144+ return builder ;
134145 }
135146}
0 commit comments