@@ -34,10 +34,79 @@ public static IServiceCollection AddMvc([NotNull] this IServiceCollection servic
34
34
{
35
35
ConfigureDefaultServices ( services ) ;
36
36
37
+ AddMvcServices ( services ) ;
38
+
39
+ return services ;
40
+ }
41
+
42
+ /// <summary>
43
+ /// Configures a set of <see cref="MvcOptions"/> for the application.
44
+ /// </summary>
45
+ /// <param name="services">The services available in the application.</param>
46
+ /// <param name="setupAction">The <see cref="MvcOptions"/> which need to be configured.</param>
47
+ public static void ConfigureMvc (
48
+ [ NotNull ] this IServiceCollection services ,
49
+ [ NotNull ] Action < MvcOptions > setupAction )
50
+ {
51
+ services . Configure ( setupAction ) ;
52
+ }
53
+
54
+ /// <summary>
55
+ /// Register the specified <paramref name="controllerTypes"/> as services and as a source for controller
56
+ /// discovery.
57
+ /// </summary>
58
+ /// <param name="services">The <see cref="IServiceCollection"/>.</param>
59
+ /// <param name="controllerTypes">A sequence of controller <see cref="Type"/>s to register in the
60
+ /// <paramref name="services"/> and used for controller discovery.</param>
61
+ /// <returns>The <see cref="IServiceCollection"/>.</returns>
62
+ public static IServiceCollection WithControllersAsServices (
63
+ [ NotNull ] this IServiceCollection services ,
64
+ [ NotNull ] IEnumerable < Type > controllerTypes )
65
+ {
66
+ var controllerTypeProvider = new FixedSetControllerTypeProvider ( ) ;
67
+ foreach ( var type in controllerTypes )
68
+ {
69
+ services . TryAdd ( ServiceDescriptor . Transient ( type , type ) ) ;
70
+ controllerTypeProvider . ControllerTypes . Add ( type . GetTypeInfo ( ) ) ;
71
+ }
72
+
73
+ services . Replace ( ServiceDescriptor . Transient < IControllerActivator , ServiceBasedControllerActivator > ( ) ) ;
74
+ services . Replace ( ServiceDescriptor . Instance < IControllerTypeProvider > ( controllerTypeProvider ) ) ;
75
+
76
+ return services ;
77
+ }
78
+
79
+ /// <summary>
80
+ /// Registers controller types from the specified <paramref name="assemblies"/> as services and as a source
81
+ /// for controller discovery.
82
+ /// </summary>
83
+ /// <param name="services">The <see cref="IServiceCollection"/>.</param>
84
+ /// <param name="controllerAssemblies">Assemblies to scan.</param>
85
+ /// <returns>The <see cref="IServiceCollection"/>.</returns>
86
+ public static IServiceCollection WithControllersAsServices (
87
+ [ NotNull ] this IServiceCollection services ,
88
+ [ NotNull ] IEnumerable < Assembly > controllerAssemblies )
89
+ {
90
+ var assemblyProvider = new FixedSetAssemblyProvider ( ) ;
91
+ foreach ( var assembly in controllerAssemblies )
92
+ {
93
+ assemblyProvider . CandidateAssemblies . Add ( assembly ) ;
94
+ }
95
+
96
+ var controllerTypeProvider = new DefaultControllerTypeProvider ( assemblyProvider ) ;
97
+ var controllerTypes = controllerTypeProvider . ControllerTypes ;
98
+
99
+ return WithControllersAsServices ( services , controllerTypes . Select ( type => type . AsType ( ) ) ) ;
100
+ }
101
+
102
+ // To enable unit testing
103
+ internal static void AddMvcServices ( IServiceCollection services )
104
+ {
37
105
// Options and core services.
38
- services . TryAdd ( ServiceDescriptor . Transient < IConfigureOptions < MvcOptions > , MvcOptionsSetup > ( ) ) ;
39
- services . TryAdd (
40
- ServiceDescriptor . Transient < IConfigureOptions < RazorViewEngineOptions > , RazorViewEngineOptionsSetup > ( ) ) ;
106
+ // multiple registration service
107
+ services . AddTransient < IConfigureOptions < MvcOptions > , MvcOptionsSetup > ( ) ;
108
+ // multiple registration service
109
+ services . AddTransient < IConfigureOptions < RazorViewEngineOptions > , RazorViewEngineOptionsSetup > ( ) ;
41
110
42
111
services . TryAdd ( ServiceDescriptor . Transient < IAssemblyProvider , DefaultAssemblyProvider > ( ) ) ;
43
112
@@ -62,7 +131,8 @@ public static IServiceCollection AddMvc([NotNull] this IServiceCollection servic
62
131
63
132
// This provider needs access to the per-request services, but might be used many times for a given
64
133
// request.
65
- services . TryAdd ( ServiceDescriptor . Transient < IActionConstraintProvider , DefaultActionConstraintProvider > ( ) ) ;
134
+ // multiple registration service
135
+ services . AddTransient < IActionConstraintProvider , DefaultActionConstraintProvider > ( ) ;
66
136
67
137
services . TryAdd ( ServiceDescriptor
68
138
. Singleton < IActionSelectorDecisionTreeProvider , ActionSelectorDecisionTreeProvider > ( ) ) ;
@@ -76,18 +146,20 @@ public static IServiceCollection AddMvc([NotNull] this IServiceCollection servic
76
146
return new DefaultObjectValidator ( options . ValidationExcludeFilters , modelMetadataProvider ) ;
77
147
} ) ) ;
78
148
79
- services . TryAdd ( ServiceDescriptor
80
- . Transient < IActionDescriptorProvider , ControllerActionDescriptorProvider > ( ) ) ;
149
+ // multiple registration service
150
+ services . AddTransient < IActionDescriptorProvider , ControllerActionDescriptorProvider > ( ) ;
81
151
82
- services . TryAdd ( ServiceDescriptor . Transient < IActionInvokerProvider , ControllerActionInvokerProvider > ( ) ) ;
152
+ // multiple registration service
153
+ services . AddTransient < IActionInvokerProvider , ControllerActionInvokerProvider > ( ) ;
83
154
84
155
services . TryAdd ( ServiceDescriptor
85
156
. Singleton < IActionDescriptorsCollectionProvider , DefaultActionDescriptorsCollectionProvider > ( ) ) ;
86
157
87
158
// The IGlobalFilterProvider is used to build the action descriptors (likely once) and so should
88
159
// remain transient to avoid keeping it in memory.
89
160
services . TryAdd ( ServiceDescriptor . Transient < IGlobalFilterProvider , DefaultGlobalFilterProvider > ( ) ) ;
90
- services . TryAdd ( ServiceDescriptor . Transient < IFilterProvider , DefaultFilterProvider > ( ) ) ;
161
+ // multiple registration service
162
+ services . AddTransient < IFilterProvider , DefaultFilterProvider > ( ) ;
91
163
92
164
services . TryAdd ( ServiceDescriptor . Transient < FormatFilter , FormatFilter > ( ) ) ;
93
165
services . TryAdd ( ServiceDescriptor . Transient < CorsAuthorizationFilter , CorsAuthorizationFilter > ( ) ) ;
@@ -184,74 +256,13 @@ public static IServiceCollection AddMvc([NotNull] this IServiceCollection servic
184
256
// Api Description
185
257
services . TryAdd ( ServiceDescriptor
186
258
. Singleton < IApiDescriptionGroupCollectionProvider , ApiDescriptionGroupCollectionProvider > ( ) ) ;
187
- services . TryAdd ( ServiceDescriptor . Transient < IApiDescriptionProvider , DefaultApiDescriptionProvider > ( ) ) ;
259
+ // multiple registration service
260
+ services . AddTransient < IApiDescriptionProvider , DefaultApiDescriptionProvider > ( ) ;
188
261
189
262
// Temp Data
190
263
services . TryAdd ( ServiceDescriptor . Scoped < ITempDataDictionary , TempDataDictionary > ( ) ) ;
191
264
// This does caching so it should stay singleton
192
265
services . TryAdd ( ServiceDescriptor . Singleton < ITempDataProvider , SessionStateTempDataProvider > ( ) ) ;
193
-
194
- return services ;
195
- }
196
-
197
- /// <summary>
198
- /// Configures a set of <see cref="MvcOptions"/> for the application.
199
- /// </summary>
200
- /// <param name="services">The services available in the application.</param>
201
- /// <param name="setupAction">The <see cref="MvcOptions"/> which need to be configured.</param>
202
- public static void ConfigureMvc (
203
- [ NotNull ] this IServiceCollection services ,
204
- [ NotNull ] Action < MvcOptions > setupAction )
205
- {
206
- services . Configure ( setupAction ) ;
207
- }
208
-
209
- /// <summary>
210
- /// Register the specified <paramref name="controllerTypes"/> as services and as a source for controller
211
- /// discovery.
212
- /// </summary>
213
- /// <param name="services">The <see cref="IServiceCollection"/>.</param>
214
- /// <param name="controllerTypes">A sequence of controller <see cref="Type"/>s to register in the
215
- /// <paramref name="services"/> and used for controller discovery.</param>
216
- /// <returns>The <see cref="IServiceCollection"/>.</returns>
217
- public static IServiceCollection WithControllersAsServices (
218
- [ NotNull ] this IServiceCollection services ,
219
- [ NotNull ] IEnumerable < Type > controllerTypes )
220
- {
221
- var controllerTypeProvider = new FixedSetControllerTypeProvider ( ) ;
222
- foreach ( var type in controllerTypes )
223
- {
224
- services . TryAdd ( ServiceDescriptor . Transient ( type , type ) ) ;
225
- controllerTypeProvider . ControllerTypes . Add ( type . GetTypeInfo ( ) ) ;
226
- }
227
-
228
- services . Replace ( ServiceDescriptor . Transient < IControllerActivator , ServiceBasedControllerActivator > ( ) ) ;
229
- services . Replace ( ServiceDescriptor . Instance < IControllerTypeProvider > ( controllerTypeProvider ) ) ;
230
-
231
- return services ;
232
- }
233
-
234
- /// <summary>
235
- /// Registers controller types from the specified <paramref name="assemblies"/> as services and as a source
236
- /// for controller discovery.
237
- /// </summary>
238
- /// <param name="services">The <see cref="IServiceCollection"/>.</param>
239
- /// <param name="controllerAssemblies">Assemblies to scan.</param>
240
- /// <returns>The <see cref="IServiceCollection"/>.</returns>
241
- public static IServiceCollection WithControllersAsServices (
242
- [ NotNull ] this IServiceCollection services ,
243
- [ NotNull ] IEnumerable < Assembly > controllerAssemblies )
244
- {
245
- var assemblyProvider = new FixedSetAssemblyProvider ( ) ;
246
- foreach ( var assembly in controllerAssemblies )
247
- {
248
- assemblyProvider . CandidateAssemblies . Add ( assembly ) ;
249
- }
250
-
251
- var controllerTypeProvider = new DefaultControllerTypeProvider ( assemblyProvider ) ;
252
- var controllerTypes = controllerTypeProvider . ControllerTypes ;
253
-
254
- return WithControllersAsServices ( services , controllerTypes . Select ( type => type . AsType ( ) ) ) ;
255
266
}
256
267
257
268
private static void ConfigureDefaultServices ( IServiceCollection services )
0 commit comments