1
- using Microsoft . Extensions . DependencyInjection . Extensions ;
1
+ using Microsoft . Extensions . Configuration ;
2
+ using Microsoft . Extensions . DependencyInjection . Extensions ;
3
+ using Microsoft . Extensions . Options ;
4
+ using System ;
5
+ using System . Collections . Generic ;
6
+ using System . Linq ;
2
7
using WebApiClientCore ;
3
8
using WebApiClientCore . Implementations ;
4
9
@@ -24,6 +29,7 @@ public static IWebApiClientBuilder AddWebApiClient(this IServiceCollection servi
24
29
{
25
30
services . AddOptions ( ) ;
26
31
services . AddMemoryCache ( ) ;
32
+ services . TryAddTransient < IOptionsFactory < HttpApiOptions > , HttpApiOptionsFactory > ( ) ;
27
33
28
34
services . TryAddSingleton ( typeof ( IHttpApiActivator < > ) , typeof ( DefaultHttpApiActivator < > ) ) ;
29
35
services . TryAddSingleton < IApiActionDescriptorProvider , DefaultApiActionDescriptorProvider > ( ) ;
@@ -45,6 +51,42 @@ public static IWebApiClientBuilder UseJsonFirstApiActionDescriptor(this IWebApiC
45
51
return builder ;
46
52
}
47
53
54
+ /// <summary>
55
+ /// 配置HttpApiOptions的默认值
56
+ /// </summary>
57
+ /// <param name="builder"></param>
58
+ /// <param name="configureOptions">配置选项</param>
59
+ /// <returns></returns>
60
+ public static IWebApiClientBuilder ConfigureHttpApi ( this IWebApiClientBuilder builder , Action < HttpApiOptions , IServiceProvider > configureOptions )
61
+ {
62
+ builder . Services . AddOptions < HttpApiOptions > ( ) . Configure ( configureOptions ) ;
63
+ return builder ;
64
+ }
65
+
66
+ /// <summary>
67
+ /// 配置HttpApiOptions的默认值
68
+ /// </summary>
69
+ /// <param name="builder"></param>
70
+ /// <param name="configureOptions">配置选项</param>
71
+ /// <returns></returns>
72
+ public static IWebApiClientBuilder ConfigureHttpApi ( this IWebApiClientBuilder builder , Action < HttpApiOptions > configureOptions )
73
+ {
74
+ builder . Services . AddOptions < HttpApiOptions > ( ) . Configure ( configureOptions ) ;
75
+ return builder ;
76
+ }
77
+
78
+ /// <summary>
79
+ /// 配置HttpApiOptions的默认值
80
+ /// </summary>
81
+ /// <param name="builder"></param>
82
+ /// <param name="configuration">配置</param>
83
+ /// <returns></returns>
84
+ public static IWebApiClientBuilder ConfigureHttpApi ( this IWebApiClientBuilder builder , IConfiguration configuration )
85
+ {
86
+ builder . Services . AddOptions < HttpApiOptions > ( ) . Bind ( configuration ) ;
87
+ return builder ;
88
+ }
89
+
48
90
/// <summary>
49
91
/// WebApiClient全局配置的Builder
50
92
/// </summary>
@@ -60,5 +102,89 @@ public WebApiClientBuilder(IServiceCollection services)
60
102
this . Services = services ;
61
103
}
62
104
}
105
+
106
+ /// <summary>
107
+ /// HttpApiOptions工厂
108
+ /// </summary>
109
+ private class HttpApiOptionsFactory : IOptionsFactory < HttpApiOptions >
110
+ {
111
+ private readonly IConfigureOptions < HttpApiOptions > [ ] _setups ;
112
+ private readonly IPostConfigureOptions < HttpApiOptions > [ ] _postConfigures ;
113
+ private readonly IValidateOptions < HttpApiOptions > [ ] _validations ;
114
+
115
+ /// <summary>
116
+ /// HttpApiOptions工厂
117
+ /// </summary>
118
+ /// <param name="setups"></param>
119
+ /// <param name="postConfigures"></param>
120
+ /// <param name="validations"></param>
121
+ public HttpApiOptionsFactory ( IEnumerable < IConfigureOptions < HttpApiOptions > > setups , IEnumerable < IPostConfigureOptions < HttpApiOptions > > postConfigures , IEnumerable < IValidateOptions < HttpApiOptions > > validations )
122
+ {
123
+ _setups = setups as IConfigureOptions < HttpApiOptions > [ ] ?? setups . ToArray ( ) ;
124
+ _postConfigures = postConfigures as IPostConfigureOptions < HttpApiOptions > [ ] ?? postConfigures . ToArray ( ) ;
125
+ _validations = validations as IValidateOptions < HttpApiOptions > [ ] ?? validations . ToArray ( ) ;
126
+ }
127
+
128
+ /// <summary>
129
+ /// 创建Options
130
+ /// </summary>
131
+ /// <param name="name"></param>
132
+ /// <returns></returns>
133
+ public HttpApiOptions Create ( string name )
134
+ {
135
+ var defaultOptions = this . Create ( Options . Options . DefaultName , default ) ;
136
+ return this . Create ( name , defaultOptions ) ;
137
+ }
138
+
139
+ /// <summary>
140
+ /// 创建Options
141
+ /// </summary>
142
+ /// <param name="name"></param>
143
+ /// <param name="options">传入的实例</param>
144
+ /// <returns></returns>
145
+ private HttpApiOptions Create ( string name , HttpApiOptions ? options )
146
+ {
147
+ if ( options == null )
148
+ {
149
+ options = new HttpApiOptions ( ) ;
150
+ }
151
+
152
+ foreach ( var setup in _setups )
153
+ {
154
+ if ( setup is IConfigureNamedOptions < HttpApiOptions > namedSetup )
155
+ {
156
+ namedSetup . Configure ( name , options ) ;
157
+ }
158
+ else if ( name == Options . Options . DefaultName )
159
+ {
160
+ setup . Configure ( options ) ;
161
+ }
162
+ }
163
+
164
+ foreach ( var post in _postConfigures )
165
+ {
166
+ post . PostConfigure ( name , options ) ;
167
+ }
168
+
169
+ if ( _validations != null )
170
+ {
171
+ var failures = new List < string > ( ) ;
172
+ foreach ( var validate in _validations )
173
+ {
174
+ var result = validate . Validate ( name , options ) ;
175
+ if ( result != null && result . Failed )
176
+ {
177
+ failures . AddRange ( result . Failures ) ;
178
+ }
179
+ }
180
+ if ( failures . Count > 0 )
181
+ {
182
+ throw new OptionsValidationException ( name , typeof ( HttpApiOptions ) , failures ) ;
183
+ }
184
+ }
185
+
186
+ return options ;
187
+ }
188
+ }
63
189
}
64
190
}
0 commit comments