forked from haoduotnt/aspnetwebstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServicesExtensions.cs
More file actions
193 lines (164 loc) · 7.24 KB
/
ServicesExtensions.cs
File metadata and controls
193 lines (164 loc) · 7.24 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) Microsoft Open Technologies, Inc. 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.Hosting;
using System.Web.Http.Metadata;
using System.Web.Http.ModelBinding;
using System.Web.Http.Properties;
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
{
public static IEnumerable<ModelBinderProvider> GetModelBinderProviders(this ServicesContainer services)
{
return services.GetServices<ModelBinderProvider>();
}
public static ModelMetadataProvider GetModelMetadataProvider(this ServicesContainer services)
{
return services.GetServiceOrThrow<ModelMetadataProvider>();
}
public static IEnumerable<ModelValidatorProvider> GetModelValidatorProviders(this ServicesContainer services)
{
return services.GetServices<ModelValidatorProvider>();
}
internal static IModelValidatorCache GetModelValidatorCache(this ServicesContainer services)
{
return services.GetService<IModelValidatorCache>();
}
public static IContentNegotiator GetContentNegotiator(this ServicesContainer services)
{
return services.GetService<IContentNegotiator>();
}
/// <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 ServicesContainer services)
{
return services.GetServiceOrThrow<IHttpControllerActivator>();
}
public static IHttpActionSelector GetActionSelector(this ServicesContainer services)
{
return services.GetServiceOrThrow<IHttpActionSelector>();
}
public static IHttpActionInvoker GetActionInvoker(this ServicesContainer services)
{
return services.GetServiceOrThrow<IHttpActionInvoker>();
}
public static IActionValueBinder GetActionValueBinder(this ServicesContainer services)
{
return services.GetService<IActionValueBinder>();
}
/// <summary>
/// Get ValueProviderFactories. The order of returned providers is the priority order that we search the factories.
/// </summary>
public static IEnumerable<ValueProviderFactory> GetValueProviderFactories(this ServicesContainer services)
{
return services.GetServices<ValueProviderFactory>();
}
public static IBodyModelValidator GetBodyModelValidator(this ServicesContainer services)
{
return services.GetService<IBodyModelValidator>();
}
public static IHostBufferPolicySelector GetHostBufferPolicySelector(this ServicesContainer services)
{
return services.GetService<IHostBufferPolicySelector>();
}
/// <summary>
/// Get a controller selector, which selects an <see cref="HttpControllerDescriptor"/> given an <see cref="HttpRequestMessage"/>.
/// </summary>
public static IHttpControllerSelector GetHttpControllerSelector(this ServicesContainer services)
{
return services.GetServiceOrThrow<IHttpControllerSelector>();
}
public static IAssembliesResolver GetAssembliesResolver(this ServicesContainer services)
{
return services.GetServiceOrThrow<IAssembliesResolver>();
}
public static IHttpControllerTypeResolver GetHttpControllerTypeResolver(this ServicesContainer services)
{
return services.GetServiceOrThrow<IHttpControllerTypeResolver>();
}
public static IApiExplorer GetApiExplorer(this ServicesContainer services)
{
return services.GetServiceOrThrow<IApiExplorer>();
}
public static IDocumentationProvider GetDocumentationProvider(this ServicesContainer services)
{
return services.GetService<IDocumentationProvider>();
}
public static IEnumerable<IFilterProvider> GetFilterProviders(this ServicesContainer services)
{
return services.GetServices<IFilterProvider>();
}
public static IHostPrincipalService GetHostPrincipalService(this ServicesContainer services)
{
if (services == null)
{
throw new ArgumentNullException("services");
}
return services.GetService<IHostPrincipalService>();
}
public static ITraceManager GetTraceManager(this ServicesContainer services)
{
return services.GetService<ITraceManager>();
}
public static ITraceWriter GetTraceWriter(this ServicesContainer services)
{
return services.GetService<ITraceWriter>();
}
// 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 ServicesContainer services)
{
if (services == null)
{
throw Error.ArgumentNull("services");
}
return (TService)services.GetService(typeof(TService));
}
private static IEnumerable<TService> GetServices<TService>(this ServicesContainer services)
{
if (services == null)
{
throw Error.ArgumentNull("services");
}
return services.GetServices(typeof(TService)).Cast<TService>();
}
private static T GetServiceOrThrow<T>(this ServicesContainer services)
{
T result = services.GetService<T>();
if (result == null)
{
throw Error.InvalidOperation(SRResources.DependencyResolverNoService, typeof(T).FullName);
}
return result;
}
}
}