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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Net.Http;
using System.Web.Http.Dispatcher;
using System.Web.Http.Filters;
using System.Web.Http.Internal;
namespace System.Web.Http.Controllers
{
public class HttpControllerDescriptor
{
private readonly ConcurrentDictionary<object, object> _properties = new ConcurrentDictionary<object, object>();
private HttpConfiguration _configuration;
private string _controllerName;
private Type _controllerType;
private IHttpControllerActivator _controllerActivator;
private IHttpActionSelector _actionSelector;
private IHttpActionInvoker _actionInvoker;
private IActionValueBinder _actionValueBinder;
private object[] _attrCached;
public HttpControllerDescriptor(HttpConfiguration configuration, string controllerName, Type controllerType)
{
if (configuration == null)
{
throw Error.ArgumentNull("configuration");
}
if (controllerName == null)
{
throw Error.ArgumentNull("controllerName");
}
if (controllerType == null)
{
throw Error.ArgumentNull("controllerType");
}
_configuration = configuration;
_controllerName = controllerName;
_controllerType = controllerType;
Initialize();
}
/// <summary>
/// Initializes a new instance of the <see cref="HttpControllerDescriptor"/> class.
/// </summary>
/// <remarks>The default constructor is intended for use by unit testing only.</remarks>
public HttpControllerDescriptor()
{
}
/// <summary>
/// Gets the properties associated with this instance.
/// </summary>
public ConcurrentDictionary<object, object> Properties
{
get { return _properties; }
}
public HttpConfiguration Configuration
{
get { return _configuration; }
set
{
if (value == null)
{
throw Error.PropertyNull();
}
_configuration = value;
}
}
public string ControllerName
{
get { return _controllerName; }
set
{
if (value == null)
{
throw Error.PropertyNull();
}
_controllerName = value;
}
}
public Type ControllerType
{
get { return _controllerType; }
set
{
if (value == null)
{
throw Error.PropertyNull();
}
_controllerType = value;
}
}
public IHttpControllerActivator HttpControllerActivator
{
get { return _controllerActivator; }
set
{
if (value == null)
{
throw Error.PropertyNull();
}
_controllerActivator = value;
}
}
public IHttpActionSelector HttpActionSelector
{
get { return _actionSelector; }
set
{
if (value == null)
{
throw Error.PropertyNull();
}
_actionSelector = value;
}
}
public IHttpActionInvoker HttpActionInvoker
{
get { return _actionInvoker; }
set
{
if (value == null)
{
throw Error.PropertyNull();
}
_actionInvoker = value;
}
}
public IActionValueBinder ActionValueBinder
{
get { return _actionValueBinder; }
set
{
if (value == null)
{
throw Error.PropertyNull();
}
_actionValueBinder = value;
}
}
/// <summary>
/// Creates a controller instance for the given <see cref="HttpRequestMessage"/>
/// </summary>
/// <param name="request">The request message</param>
/// <returns></returns>
public virtual IHttpController CreateController(HttpRequestMessage request)
{
if (request == null)
{
throw Error.ArgumentNull("request");
}
// Invoke the controller activator
IHttpController instance = HttpControllerActivator.Create(request, this, ControllerType);
return instance;
}
/// <summary>
/// Returns the collection of <see cref="IFilter">filters</see> associated with this descriptor's controller.
/// </summary>
/// <remarks>The default implementation calls <see cref="GetCustomAttributes{IFilter}()"/>.</remarks>
/// <returns>A collection of filters associated with this controller.</returns>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Filters can be built dynamically")]
public virtual Collection<IFilter> GetFilters()
{
return GetCustomAttributes<IFilter>();
}
/// <summary>
/// Returns a collection of attributes that can be assigned to <typeparamref name="T"/> for this descriptor's controller.
/// </summary>
/// <remarks>The default implementation retrieves the matching set of attributes declared on <see cref="ControllerType"/>.</remarks>
/// <typeparam name="T">Used to filter the collection of attributes. Use a value of <see cref="Object"/> to retrieve all attributes.</typeparam>
/// <returns>A collection of attributes associated with this controller.</returns>
public virtual Collection<T> GetCustomAttributes<T>() where T : class
{
// Getting custom attributes via reflection is slow.
// But iterating over a object[] to pick out specific types is fast.
// Furthermore, many different services may call to ask for different attributes, so we have multiple callers.
// That means there's not a single cache for the callers, which means there's some value caching here.
if (_attrCached == null)
{
// Even in a race, we'll just ask for the custom attributes twice.
_attrCached = ControllerType.GetCustomAttributes(inherit: true);
}
return new Collection<T>(TypeHelper.OfType<T>(_attrCached));
}
private void Initialize()
{
// Look for attribute to provide specialized information for this controller type
HttpControllerConfigurationAttribute controllerConfig =
_controllerType.GetCustomAttributes<HttpControllerConfigurationAttribute>(inherit: true).FirstOrDefault();
// If we find attribute then first ask dependency resolver and if we get null then create it ourselves
if (controllerConfig != null)
{
if (controllerConfig.HttpControllerActivator != null)
{
_controllerActivator = GetService<IHttpControllerActivator>(_configuration, controllerConfig.HttpControllerActivator);
}
if (controllerConfig.HttpActionSelector != null)
{
_actionSelector = GetService<IHttpActionSelector>(_configuration, controllerConfig.HttpActionSelector);
}
if (controllerConfig.HttpActionInvoker != null)
{
_actionInvoker = GetService<IHttpActionInvoker>(_configuration, controllerConfig.HttpActionInvoker);
}
if (controllerConfig.ActionValueBinder != null)
{
_actionValueBinder = GetService<IActionValueBinder>(_configuration, controllerConfig.ActionValueBinder);
}
}
// For everything still null we fall back to the default service list.
if (_controllerActivator == null)
{
_controllerActivator = Configuration.Services.GetHttpControllerActivator();
}
if (_actionSelector == null)
{
_actionSelector = Configuration.Services.GetActionSelector();
}
if (_actionInvoker == null)
{
_actionInvoker = Configuration.Services.GetActionInvoker();
}
if (_actionValueBinder == null)
{
_actionValueBinder = Configuration.Services.GetActionValueBinder();
}
}
/// <summary>
/// Helper for looking up or activating <see cref="IHttpControllerActivator"/>, <see cref="IHttpActionSelector"/>,
/// and <see cref="IHttpActionInvoker"/>. Note that we here use the slow <see cref="M:Activator.CreateInstance"/>
/// as the instances live for the lifetime of the <see cref="HttpControllerDescriptor"/> instance itself so there is
/// little benefit in caching a delegate.
/// </summary>
/// <typeparam name="TBase">The type of the base.</typeparam>
/// <param name="configuration">The configuration.</param>
/// <param name="serviceType">Type of the service.</param>
/// <returns>A new instance.</returns>
private static TBase GetService<TBase>(HttpConfiguration configuration, Type serviceType) where TBase : class
{
Contract.Assert(configuration != null);
if (serviceType != null)
{
return (TBase)configuration.DependencyResolver.GetService(serviceType)
?? (TBase)Activator.CreateInstance(serviceType);
}
return null;
}
}
}
|