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
|
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Metadata;
namespace System.Web.Http.Controllers
{
/// <summary>
/// This describes *how* the binding will happen. Does not actually bind.
/// This is static for a given action descriptor and can be reused across requests.
/// This may be a nice thing to log. Or set a breakpoint after we create and preview what's about to happen.
/// In theory, this could be precompiled for each Action descriptor.
/// </summary>
public class HttpActionBinding
{
private HttpActionDescriptor _actionDescriptor;
private HttpParameterBinding[] _parameterBindings;
public HttpActionBinding()
{
}
public HttpActionBinding(HttpActionDescriptor actionDescriptor, HttpParameterBinding[] bindings)
{
ActionDescriptor = actionDescriptor;
ParameterBindings = bindings;
}
/// <summary>
/// Back pointer to the action this binding is for.
/// This can also provide the Type[], string[] names for the parameters.
/// </summary>
public HttpActionDescriptor ActionDescriptor
{
get
{
return _actionDescriptor;
}
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
_actionDescriptor = value;
}
}
/// <summary>
/// Specifies synchronous bindings for each parameter.This is a parallel array to the ActionDescriptor's parameter array.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Want an array")]
public HttpParameterBinding[] ParameterBindings
{
get
{
return _parameterBindings;
}
set
{
if (value == null)
{
throw new ArgumentNullException("value");
}
_parameterBindings = value;
}
}
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Caller is responsible for disposing of response instance.")]
public virtual Task ExecuteBindingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
// First, make sure the actionBinding is valid before trying to execute it. This keeps us in a known state in case of errors.
foreach (HttpParameterBinding parameterBinder in ParameterBindings)
{
if (!parameterBinder.IsValid)
{
// Error code here is 500 because the WebService developer's action signature is bad.
return TaskHelpers.FromError(new HttpResponseException(actionContext.Request.CreateResponse(
HttpStatusCode.InternalServerError, parameterBinder.ErrorMessage)));
}
}
ModelMetadataProvider metadataProvider = actionContext.ControllerContext.Configuration.ServiceResolver.GetModelMetadataProvider();
// Execute all the binders.
IEnumerable<Task> tasks = from parameterBinder in ParameterBindings select parameterBinder.ExecuteBindingAsync(metadataProvider, actionContext, cancellationToken);
return TaskHelpers.Iterate(tasks, cancellationToken);
}
}
}
|