@@ -21,6 +21,26 @@ public static class AspireNatsClientExtensions
21
21
private const string DefaultConfigSectionName = "Aspire:NATS:Net" ;
22
22
private const string ActivityNameSource = "NATS.Net" ;
23
23
24
+ /// <inheritdoc cref="AddNatsClient(IHostApplicationBuilder, string, Action{NatsClientSettings}?, Func{IServiceProvider,NatsOpts,NatsOpts}?)"/>
25
+ public static void AddNatsClient ( this IHostApplicationBuilder builder , string connectionName )
26
+ => AddNatsClientInternal ( builder , connectionName : connectionName , serviceKey : null , configureSettings : null , configureOptions : null ) ;
27
+
28
+ /// <inheritdoc cref="AddNatsClient(IHostApplicationBuilder, string, Action{NatsClientSettings}?, Func{IServiceProvider,NatsOpts,NatsOpts}?)"/>
29
+ public static void AddNatsClient ( this IHostApplicationBuilder builder , string connectionName , Action < NatsClientSettings > ? configureSettings )
30
+ => AddNatsClientInternal ( builder , connectionName : connectionName , serviceKey : null , configureSettings : configureSettings , configureOptions : null ) ;
31
+
32
+ /// <inheritdoc cref="AddNatsClient(IHostApplicationBuilder, string, Action{NatsClientSettings}?, Func{IServiceProvider,NatsOpts,NatsOpts}?)"/>
33
+ public static void AddNatsClient ( this IHostApplicationBuilder builder , string connectionName , Func < NatsOpts , NatsOpts > ? configureOptions )
34
+ => AddNatsClientInternal ( builder , connectionName : connectionName , serviceKey : null , configureSettings : null , configureOptions : Wrap ( configureOptions ) ) ;
35
+
36
+ /// <inheritdoc cref="AddNatsClient(IHostApplicationBuilder, string, Action{NatsClientSettings}?, Func{IServiceProvider,NatsOpts,NatsOpts}?)"/>
37
+ public static void AddNatsClient ( this IHostApplicationBuilder builder , string connectionName , Func < IServiceProvider , NatsOpts , NatsOpts > ? configureOptions )
38
+ => AddNatsClientInternal ( builder , connectionName : connectionName , serviceKey : null , configureSettings : null , configureOptions : configureOptions ) ;
39
+
40
+ /// <inheritdoc cref="AddNatsClient(IHostApplicationBuilder, string, Action{NatsClientSettings}?, Func{IServiceProvider,NatsOpts,NatsOpts}?)"/>
41
+ public static void AddNatsClient ( this IHostApplicationBuilder builder , string connectionName , Action < NatsClientSettings > ? configureSettings , Func < NatsOpts , NatsOpts > ? configureOptions )
42
+ => AddNatsClientInternal ( builder , connectionName : connectionName , serviceKey : null , configureSettings : configureSettings , configureOptions : Wrap ( configureOptions ) ) ;
43
+
24
44
/// <summary>
25
45
/// Registers <see cref="INatsConnection"/> service for connecting NATS server with NATS client.
26
46
/// Configures health check and logging for the NATS client.
@@ -31,12 +51,44 @@ public static class AspireNatsClientExtensions
31
51
/// <param name="configureOptions">An optional delegate that can be used for customizing NATS options that aren't exposed as standard configuration.</param>
32
52
/// <exception cref="ArgumentNullException">Thrown if mandatory <paramref name="builder"/> is null.</exception>
33
53
/// <exception cref="InvalidOperationException">Thrown when mandatory <see cref="NatsClientSettings.ConnectionString"/> is not provided.</exception>
34
- public static void AddNatsClient ( this IHostApplicationBuilder builder , string connectionName , Action < NatsClientSettings > ? configureSettings = null , Func < NatsOpts , NatsOpts > ? configureOptions = null )
54
+ public static void AddNatsClient ( this IHostApplicationBuilder builder , string connectionName , Action < NatsClientSettings > ? configureSettings , Func < IServiceProvider , NatsOpts , NatsOpts > ? configureOptions )
35
55
{
36
- ArgumentNullException . ThrowIfNull ( builder ) ;
37
- ArgumentException . ThrowIfNullOrEmpty ( connectionName ) ;
56
+ AddNatsClientInternal ( builder , connectionName : connectionName , serviceKey : null , configureSettings : configureSettings , configureOptions : configureOptions ) ;
57
+ }
58
+
59
+ /// <inheritdoc cref="AddKeyedNatsClient(IHostApplicationBuilder, string, Action{NatsClientSettings}?, Func{IServiceProvider,NatsOpts,NatsOpts}?)"/>
60
+ public static void AddKeyedNatsClient ( this IHostApplicationBuilder builder , string name )
61
+ {
62
+ ArgumentException . ThrowIfNullOrEmpty ( name ) ;
63
+ AddNatsClientInternal ( builder , connectionName : name , serviceKey : name , configureSettings : null , configureOptions : null ) ;
64
+ }
38
65
39
- AddNatsClient ( builder , connectionName : connectionName , serviceKey : null , configureSettings : configureSettings , configureOptions : configureOptions ) ;
66
+ /// <inheritdoc cref="AddKeyedNatsClient(IHostApplicationBuilder, string, Action{NatsClientSettings}?, Func{IServiceProvider,NatsOpts,NatsOpts}?)"/>
67
+ public static void AddKeyedNatsClient ( this IHostApplicationBuilder builder , string name , Action < NatsClientSettings > ? configureSettings )
68
+ {
69
+ ArgumentException . ThrowIfNullOrEmpty ( name ) ;
70
+ AddNatsClientInternal ( builder , connectionName : name , serviceKey : name , configureSettings : configureSettings , configureOptions : null ) ;
71
+ }
72
+
73
+ /// <inheritdoc cref="AddKeyedNatsClient(IHostApplicationBuilder, string, Action{NatsClientSettings}?, Func{IServiceProvider,NatsOpts,NatsOpts}?)"/>
74
+ public static void AddKeyedNatsClient ( this IHostApplicationBuilder builder , string name , Func < NatsOpts , NatsOpts > ? configureOptions )
75
+ {
76
+ ArgumentException . ThrowIfNullOrEmpty ( name ) ;
77
+ AddNatsClientInternal ( builder , connectionName : name , serviceKey : name , configureSettings : null , configureOptions : Wrap ( configureOptions ) ) ;
78
+ }
79
+
80
+ /// <inheritdoc cref="AddKeyedNatsClient(IHostApplicationBuilder, string, Action{NatsClientSettings}?, Func{IServiceProvider,NatsOpts,NatsOpts}?)"/>
81
+ public static void AddKeyedNatsClient ( this IHostApplicationBuilder builder , string name , Func < IServiceProvider , NatsOpts , NatsOpts > ? configureOptions )
82
+ {
83
+ ArgumentException . ThrowIfNullOrEmpty ( name ) ;
84
+ AddNatsClientInternal ( builder , connectionName : name , serviceKey : name , configureSettings : null , configureOptions : configureOptions ) ;
85
+ }
86
+
87
+ /// <inheritdoc cref="AddKeyedNatsClient(IHostApplicationBuilder, string, Action{NatsClientSettings}?, Func{IServiceProvider,NatsOpts,NatsOpts}?)"/>
88
+ public static void AddKeyedNatsClient ( this IHostApplicationBuilder builder , string name , Action < NatsClientSettings > ? configureSettings , Func < NatsOpts , NatsOpts > ? configureOptions )
89
+ {
90
+ ArgumentException . ThrowIfNullOrEmpty ( name ) ;
91
+ AddNatsClientInternal ( builder , connectionName : name , serviceKey : name , configureSettings : configureSettings , configureOptions : Wrap ( configureOptions ) ) ;
40
92
}
41
93
42
94
/// <summary>
@@ -50,17 +102,16 @@ public static void AddNatsClient(this IHostApplicationBuilder builder, string co
50
102
/// <exception cref="ArgumentNullException">Thrown when <paramref name="builder"/> or <paramref name="name"/> is null.</exception>
51
103
/// <exception cref="ArgumentException">Thrown if mandatory <paramref name="name"/> is empty.</exception>
52
104
/// <exception cref="InvalidOperationException">Thrown when mandatory <see cref="NatsClientSettings.ConnectionString"/> is not provided.</exception>
53
- public static void AddKeyedNatsClient ( this IHostApplicationBuilder builder , string name , Action < NatsClientSettings > ? configureSettings = null , Func < NatsOpts , NatsOpts > ? configureOptions = null )
105
+ public static void AddKeyedNatsClient ( this IHostApplicationBuilder builder , string name , Action < NatsClientSettings > ? configureSettings , Func < IServiceProvider , NatsOpts , NatsOpts > ? configureOptions )
54
106
{
55
- ArgumentNullException . ThrowIfNull ( builder ) ;
56
107
ArgumentException . ThrowIfNullOrEmpty ( name ) ;
57
-
58
- AddNatsClient ( builder , connectionName : name , serviceKey : name , configureSettings : configureSettings , configureOptions : configureOptions ) ;
108
+ AddNatsClientInternal ( builder , connectionName : name , serviceKey : name , configureSettings : configureSettings , configureOptions : configureOptions ) ;
59
109
}
60
110
61
- private static void AddNatsClient ( this IHostApplicationBuilder builder , string connectionName , object ? serviceKey , Action < NatsClientSettings > ? configureSettings , Func < NatsOpts , NatsOpts > ? configureOptions )
111
+ private static void AddNatsClientInternal ( this IHostApplicationBuilder builder , string connectionName , object ? serviceKey , Action < NatsClientSettings > ? configureSettings , Func < IServiceProvider , NatsOpts , NatsOpts > ? configureOptions )
62
112
{
63
113
ArgumentNullException . ThrowIfNull ( builder ) ;
114
+ ArgumentException . ThrowIfNullOrEmpty ( connectionName ) ;
64
115
65
116
NatsClientSettings settings = new ( ) ;
66
117
var configSection = builder . Configuration . GetSection ( DefaultConfigSectionName ) ;
@@ -84,7 +135,7 @@ NatsConnection Factory(IServiceProvider provider)
84
135
85
136
if ( configureOptions != null )
86
137
{
87
- options = configureOptions ( options ) ;
138
+ options = configureOptions ( provider , options ) ;
88
139
}
89
140
90
141
if ( settings . ConnectionString == null )
@@ -145,4 +196,14 @@ public static void AddNatsJetStream(this IHostApplicationBuilder builder)
145
196
return new NatsJSContextFactory ( ) . CreateContext ( provider . GetRequiredService < INatsConnection > ( ) ) ;
146
197
} ) ;
147
198
}
199
+
200
+ private static Func < IServiceProvider , NatsOpts , NatsOpts > ? Wrap ( Func < NatsOpts , NatsOpts > ? func )
201
+ {
202
+ if ( func is null )
203
+ {
204
+ return null ;
205
+ }
206
+
207
+ return ( _ , options ) => func ( options ) ;
208
+ }
148
209
}
0 commit comments