blob: 9a3532bd6961750ea6ac725ad850551a89c366ba (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Web.Http.Controllers;
using System.Web.Http.Description;
using System.Web.Http.Dispatcher;
using System.Web.Http.Filters;
using System.Web.Http.Metadata;
using System.Web.Http.ModelBinding;
using System.Web.Http.Properties;
using System.Web.Http.Services;
using System.Web.Http.Tracing;
using System.Web.Http.Validation;
using System.Web.Http.ValueProviders;
namespace System.Web.Http
{
/// <summary>
/// This provides a centralized list of type-safe accessors describing where and how we get services.
/// This also provides a single entry point for each service request. That makes it easy
/// to see which parts of the code use it, and provides a single place to comment usage.
/// Accessors encapsulate usage like:
/// <list type="bullet">
/// <item>Type-safe using {T} instead of unsafe <see cref="System.Type"/>.</item>
/// <item>which type do we key off? This is interesting with type hierarchies.</item>
/// <item>do we ask for singular or plural?</item>
/// <item>is it optional or mandatory?</item>
/// <item>what are the ordering semantics</item>
/// </list>
/// Expected that any <see cref="IEnumerable{T}"/> we return is non-null, although possibly empty.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class ServicesExtensions
{
/// <summary>
/// Get ValueProviderFactories. The order of returned providers is the priority order that we search the factories.
/// </summary>
public static IEnumerable<ValueProviderFactory> GetValueProviderFactories(this DefaultServices services)
{
return services.GetServices<ValueProviderFactory>();
}
/// <summary>
/// Get a controller selector, which selects an <see cref="HttpControllerDescriptor"/> given an <see cref="HttpRequestMessage"/>.
/// </summary>
public static IHttpControllerSelector GetHttpControllerSelector(this DefaultServices services)
{
return services.GetServiceOrThrow<IHttpControllerSelector>();
}
/// <summary>
/// Controller activator is used to instantiate an <see cref="IHttpController"/>.
/// </summary>
/// <returns>
/// An <see cref="IHttpControllerActivator"/> instance or null if none are registered.
/// </returns>
public static IHttpControllerActivator GetHttpControllerActivator(this DefaultServices services)
{
return services.GetServiceOrThrow<IHttpControllerActivator>();
}
public static IAssembliesResolver GetAssembliesResolver(this DefaultServices services)
{
return services.GetServiceOrThrow<IAssembliesResolver>();
}
public static IHttpControllerTypeResolver GetHttpControllerTypeResolver(this DefaultServices services)
{
return services.GetServiceOrThrow<IHttpControllerTypeResolver>();
}
public static IHttpActionSelector GetActionSelector(this DefaultServices services)
{
return services.GetServiceOrThrow<IHttpActionSelector>();
}
public static IHttpActionInvoker GetActionInvoker(this DefaultServices services)
{
return services.GetServiceOrThrow<IHttpActionInvoker>();
}
public static IApiExplorer GetApiExplorer(this DefaultServices services)
{
return services.GetServiceOrThrow<IApiExplorer>();
}
public static IDocumentationProvider GetDocumentationProvider(this DefaultServices services)
{
return services.GetService<IDocumentationProvider>();
}
public static IEnumerable<IFilterProvider> GetFilterProviders(this DefaultServices services)
{
return services.GetServices<IFilterProvider>();
}
public static ModelMetadataProvider GetModelMetadataProvider(this DefaultServices services)
{
return services.GetServiceOrThrow<ModelMetadataProvider>();
}
public static IEnumerable<ModelBinderProvider> GetModelBinderProviders(this DefaultServices services)
{
return services.GetServices<ModelBinderProvider>();
}
public static IEnumerable<ModelValidatorProvider> GetModelValidatorProviders(this DefaultServices services)
{
return services.GetServices<ModelValidatorProvider>();
}
public static IContentNegotiator GetContentNegotiator(this DefaultServices services)
{
return services.GetService<IContentNegotiator>();
}
public static IActionValueBinder GetActionValueBinder(this DefaultServices services)
{
return services.GetService<IActionValueBinder>();
}
public static ITraceManager GetTraceManager(this DefaultServices services)
{
return services.GetService<ITraceManager>();
}
public static ITraceWriter GetTraceWriter(this DefaultServices services)
{
return services.GetService<ITraceWriter>();
}
public static IBodyModelValidator GetBodyModelValidator(this DefaultServices services)
{
return services.GetService<IBodyModelValidator>();
}
// Runtime code shouldn't call GetService() directly. Instead, have a wrapper (like the ones above) and call through the wrapper.
private static TService GetService<TService>(this DefaultServices services)
{
if (services == null)
{
throw Error.ArgumentNull("services");
}
return (TService)services.GetService(typeof(TService));
}
private static IEnumerable<TService> GetServices<TService>(this DefaultServices services)
{
if (services == null)
{
throw Error.ArgumentNull("services");
}
return services.GetServices(typeof(TService)).Cast<TService>();
}
private static T GetServiceOrThrow<T>(this DefaultServices services)
{
T result = services.GetService<T>();
if (result == null)
{
throw Error.InvalidOperation(SRResources.DependencyResolverNoService, typeof(T).FullName);
}
return result;
}
}
}
|