@@ -63,11 +63,7 @@ public static IEndpointConventionBuilder Subscribe<TEvent>(
63
63
string appName )
64
64
where TEvent : IntegrationEvent
65
65
{
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 ( )
71
67
. MapPost ( route , ( TEvent receivedEvent , IEventBus eventBus ) => eventBus . ReceiveAsync ( receivedEvent ) )
72
68
. WithTopic ( DaprOptions . PubSubName , DaprUtils . GetDaprTopicName < TEvent > ( appName ) ) ;
73
69
return result ;
@@ -80,6 +76,8 @@ public static IEndpointConventionBuilder Subscribe<TEvent>(
80
76
/// <param name="assemblies"><see cref="Assembly"/></param>
81
77
public static void Subscribe ( this IEndpointRouteBuilder builder , params Assembly [ ] assemblies )
82
78
{
79
+ builder . EnsureProvision ( ) ;
80
+
83
81
var method = typeof ( EndPointExtensions ) . GetMethod (
84
82
nameof ( Subscribe ) ,
85
83
new [ ] { typeof ( IEndpointRouteBuilder ) , typeof ( string ) } ) ! ;
@@ -100,11 +98,11 @@ public static void Subscribe(this IEndpointRouteBuilder builder, params Assembly
100
98
}
101
99
}
102
100
103
- private static void EnsureEventBusRegistered ( IEndpointRouteBuilder builder , DaprOptions daprOptions )
101
+ private static DaprOptions EnsureEventBusRegistered ( this DaprOptions daprOptions , IEndpointRouteBuilder builder )
104
102
{
105
103
if ( daprOptions . IsEventBusRegistered )
106
104
{
107
- return ;
105
+ return daprOptions ;
108
106
}
109
107
110
108
var serviceCheck = builder . ServiceProvider . GetRequiredService < IServiceProviderIsService > ( ) ;
@@ -115,13 +113,14 @@ private static void EnsureEventBusRegistered(IEndpointRouteBuilder builder, Dapr
115
113
}
116
114
117
115
daprOptions . IsEventBusRegistered = true ;
116
+ return daprOptions ;
118
117
}
119
118
120
- private static void EnsureDaprSubscribeHandlerMapped ( IEndpointRouteBuilder builder , DaprOptions daprOptions )
119
+ private static DaprOptions EnsureDaprSubscribeHandlerMapped ( this DaprOptions daprOptions , IEndpointRouteBuilder builder )
121
120
{
122
121
if ( daprOptions . IsDaprSubscribeHandlerMapped )
123
122
{
124
- return ;
123
+ return daprOptions ;
125
124
}
126
125
127
126
if ( builder is IApplicationBuilder app )
@@ -131,5 +130,17 @@ private static void EnsureDaprSubscribeHandlerMapped(IEndpointRouteBuilder build
131
130
132
131
builder . MapSubscribeHandler ( ) ;
133
132
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 ;
134
145
}
135
146
}
0 commit comments