Skip to content

Commit 1dcec5d

Browse files
committed
26: Separate service location from dependency injection
1 parent 0ed4083 commit 1dcec5d

File tree

63 files changed

+1908
-930
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1908
-930
lines changed

src/System.Web.Http.WebHost/GlobalConfiguration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public static class GlobalConfiguration
1414
() =>
1515
{
1616
HttpConfiguration config = new HttpConfiguration(new HostedHttpRouteCollection(RouteTable.Routes));
17-
config.ServiceResolver.SetService(typeof(IAssembliesResolver), new WebHostAssembliesResolver());
18-
config.ServiceResolver.SetService(typeof(IHttpControllerTypeResolver), new WebHostHttpControllerTypeResolver());
17+
config.Services.Replace(typeof(IAssembliesResolver), new WebHostAssembliesResolver());
18+
config.Services.Replace(typeof(IHttpControllerTypeResolver), new WebHostHttpControllerTypeResolver());
1919
return config;
2020
});
2121

src/System.Web.Http/Controllers/HttpActionBinding.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public virtual Task ExecuteBindingAsync(HttpActionContext actionContext, Cancell
9393
if (_metadataProvider == null)
9494
{
9595
HttpConfiguration config = actionContext.ControllerContext.Configuration;
96-
_metadataProvider = config.ServiceResolver.GetModelMetadataProvider();
96+
_metadataProvider = config.Services.GetModelMetadataProvider();
9797
}
9898

9999
// Execute all the binders.

src/System.Web.Http/Controllers/HttpActionContextExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static ModelMetadataProvider GetMetadataProvider(this HttpActionContext a
2626
throw Error.ArgumentNull("actionContext");
2727
}
2828

29-
return actionContext.ControllerContext.Configuration.ServiceResolver.GetModelMetadataProvider();
29+
return actionContext.ControllerContext.Configuration.Services.GetModelMetadataProvider();
3030
}
3131

3232
/// <summary>
@@ -41,7 +41,7 @@ public static IEnumerable<ModelValidatorProvider> GetValidatorProviders(this Htt
4141
throw Error.ArgumentNull("actionContext");
4242
}
4343

44-
return actionContext.ControllerContext.Configuration.ServiceResolver.GetModelValidatorProviders();
44+
return actionContext.ControllerContext.Configuration.Services.GetModelValidatorProviders();
4545
}
4646

4747
/// <summary>
@@ -88,7 +88,7 @@ public static bool TryGetBinder(this HttpActionContext actionContext, ModelBindi
8888
}
8989
else
9090
{
91-
binder = actionContext.ControllerContext.Configuration.ServiceResolver.GetModelBinderProviders()
91+
binder = actionContext.ControllerContext.Configuration.Services.GetModelBinderProviders()
9292
.Select(p => p.GetBinder(actionContext, bindingContext))
9393
.Where(b => b != null)
9494
.FirstOrDefault();

src/System.Web.Http/Controllers/HttpActionDescriptor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public virtual Collection<FilterInfo> GetFilterPipeline()
203203

204204
private Collection<FilterInfo> InitializeFilterPipeline()
205205
{
206-
IEnumerable<IFilterProvider> filterProviders = _configuration.ServiceResolver.GetFilterProviders();
206+
IEnumerable<IFilterProvider> filterProviders = _configuration.Services.GetFilterProviders();
207207

208208
IEnumerable<FilterInfo> filters = filterProviders.SelectMany(fp => fp.GetFilters(_configuration, this)).OrderBy(f => f, FilterInfoComparer.Instance);
209209

src/System.Web.Http/Controllers/HttpControllerConfigurationAttribute.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
using System.Web.Http.Dispatcher;
1+
using System.Web.Http.Dependencies;
2+
using System.Web.Http.Dispatcher;
23

34
namespace System.Web.Http.Controllers
45
{
56
/// <summary>
67
/// Provides a mechanism for a <see cref="IHttpController"/> implementation to indicate
78
/// what kind of <see cref="IHttpControllerActivator"/>, <see cref="IHttpActionSelector"/>, <see cref="IActionValueBinder"/>
89
/// and <see cref="IHttpActionInvoker"/> to use for that controller. The types are
9-
/// first looked up in the <see cref="Services.DependencyResolver"/> and if not found there
10+
/// first looked up in the <see cref="IDependencyResolver"/> and if not found there
1011
/// then created directly.
1112
/// </summary>
1213
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]

src/System.Web.Http/Controllers/HttpControllerDescriptor.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -255,25 +255,25 @@ private void Initialize()
255255
}
256256
}
257257

258-
// For everything still null we call the dependency resolver as normal.
258+
// For everything still null we fall back to the default service list.
259259
if (_controllerActivator == null)
260260
{
261-
_controllerActivator = Configuration.ServiceResolver.GetHttpControllerActivator();
261+
_controllerActivator = Configuration.Services.GetHttpControllerActivator();
262262
}
263263

264264
if (_actionSelector == null)
265265
{
266-
_actionSelector = Configuration.ServiceResolver.GetActionSelector();
266+
_actionSelector = Configuration.Services.GetActionSelector();
267267
}
268268

269269
if (_actionInvoker == null)
270270
{
271-
_actionInvoker = Configuration.ServiceResolver.GetActionInvoker();
271+
_actionInvoker = Configuration.Services.GetActionInvoker();
272272
}
273273

274274
if (_actionValueBinder == null)
275275
{
276-
_actionValueBinder = Configuration.ServiceResolver.GetActionValueBinder();
276+
_actionValueBinder = Configuration.Services.GetActionValueBinder();
277277
}
278278
}
279279

@@ -292,8 +292,8 @@ private static TBase GetService<TBase>(HttpConfiguration configuration, Type ser
292292
Contract.Assert(configuration != null);
293293
if (serviceType != null)
294294
{
295-
return (TBase)configuration.ServiceResolver.GetService(serviceType) ??
296-
(TBase)Activator.CreateInstance(serviceType);
295+
return (TBase)configuration.DependencyResolver.GetService(serviceType)
296+
?? (TBase)Activator.CreateInstance(serviceType);
297297
}
298298

299299
return null;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace System.Web.Http.Dependencies
5+
{
6+
internal class EmptyResolver : IDependencyResolver
7+
{
8+
private static readonly IDependencyResolver _instance = new EmptyResolver();
9+
10+
private EmptyResolver()
11+
{
12+
}
13+
14+
public static IDependencyResolver Instance
15+
{
16+
get { return _instance; }
17+
}
18+
19+
public IDependencyScope BeginScope()
20+
{
21+
return this;
22+
}
23+
24+
public void Dispose()
25+
{
26+
}
27+
28+
public object GetService(Type serviceType)
29+
{
30+
return null;
31+
}
32+
33+
public IEnumerable<object> GetServices(Type serviceType)
34+
{
35+
return Enumerable.Empty<object>();
36+
}
37+
}
38+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace System.Web.Http.Dependencies
2+
{
3+
/// <summary>
4+
/// Represents a dependency injection container.
5+
/// </summary>
6+
public interface IDependencyResolver : IDependencyScope
7+
{
8+
/// <summary>
9+
/// Starts a resolution scope. Objects which are resolved in the given scope will belong to
10+
/// that scope, and when the scope is disposed, those objects are returned to the container.
11+
/// Implementers should return a new instance of <see cref="IDependencyScope"/> every time this
12+
/// method is called, unless the container does not have any concept of scope or resource
13+
/// release (in which case, it would be okay to return 'this', so long as the calls to
14+
/// <see cref="IDisposable.Dispose"/> are effectively NOOPs).
15+
/// </summary>
16+
/// <returns>The dependency scope.</returns>
17+
IDependencyScope BeginScope();
18+
}
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
3+
namespace System.Web.Http.Dependencies
4+
{
5+
/// <summary>
6+
/// Represents a scope that is tracked by the dependency injection container. The scope is
7+
/// used to keep track of resources that have been provided, so that they can then be
8+
/// subsequently released when <see cref="IDisposable.Dispose"/> is called.
9+
/// </summary>
10+
public interface IDependencyScope : IDisposable
11+
{
12+
/// <summary>
13+
/// Gets an instance of the given <paramref name="serviceType"/>. Must never throw.
14+
/// </summary>
15+
/// <param name="serviceType">The object type.</param>
16+
/// <returns>The requested object, if found; <c>null</c> otherwise.</returns>
17+
object GetService(Type serviceType);
18+
19+
/// <summary>
20+
/// Gets all instances of the given <paramref name="serviceType"/>. Must never throw.
21+
/// </summary>
22+
/// <param name="serviceType">The object type.</param>
23+
/// <returns>A sequence of isntances of the requested <paramref name="serviceType"/>. The sequence
24+
/// should be empty (not null) if no objects of the given type are available.</returns>
25+
IEnumerable<object> GetServices(Type serviceType);
26+
}
27+
}

src/System.Web.Http/Description/ApiExplorer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public virtual Collection<HttpMethod> GetHttpMethodsSupportedByAction(IHttpRoute
142142
private Collection<ApiDescription> InitializeApiDescriptions()
143143
{
144144
Collection<ApiDescription> apiDescriptions = new Collection<ApiDescription>();
145-
IHttpControllerSelector controllerSelector = _config.ServiceResolver.GetHttpControllerSelector();
145+
IHttpControllerSelector controllerSelector = _config.Services.GetHttpControllerSelector();
146146
IDictionary<string, HttpControllerDescriptor> controllerMappings = controllerSelector.GetControllerMapping();
147147
if (controllerMappings != null)
148148
{
@@ -365,7 +365,7 @@ private ApiParameterDescription CreateParameterDescriptionFromBinding(HttpParame
365365

366366
private string GetApiDocumentation(HttpActionDescriptor actionDescriptor)
367367
{
368-
IDocumentationProvider documentationProvider = DocumentationProvider ?? _config.ServiceResolver.GetDocumentationProvider();
368+
IDocumentationProvider documentationProvider = DocumentationProvider ?? _config.Services.GetDocumentationProvider();
369369
if (documentationProvider == null)
370370
{
371371
return string.Format(CultureInfo.CurrentCulture, SRResources.ApiExplorer_DefaultDocumentation, actionDescriptor.ActionName);
@@ -376,7 +376,7 @@ private string GetApiDocumentation(HttpActionDescriptor actionDescriptor)
376376

377377
private string GetApiParameterDocumentation(HttpParameterDescriptor parameterDescriptor)
378378
{
379-
IDocumentationProvider documentationProvider = DocumentationProvider ?? _config.ServiceResolver.GetDocumentationProvider();
379+
IDocumentationProvider documentationProvider = DocumentationProvider ?? _config.Services.GetDocumentationProvider();
380380
if (documentationProvider == null)
381381
{
382382
return string.Format(CultureInfo.CurrentCulture, SRResources.ApiExplorer_DefaultDocumentation, parameterDescriptor.Prefix ?? parameterDescriptor.ParameterName);

0 commit comments

Comments
 (0)