-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathActionFilterWrapper.cs
132 lines (117 loc) · 5.55 KB
/
ActionFilterWrapper.cs
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
// This software is part of the Autofac IoC container
// Copyright (c) 2012 Autofac Contributors
// http://autofac.org
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Autofac.Features.Metadata;
namespace Autofac.Integration.WebApi
{
/// <summary>
/// Resolves a filter for the specified metadata for each controller request.
/// </summary>
[SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes", Justification = "Derived attribute adds filter override support")]
internal class ActionFilterWrapper : ActionFilterAttribute, IAutofacActionFilter, IFilterWrapper
{
private readonly FilterMetadata _filterMetadata;
/// <summary>
/// Initializes a new instance of the <see cref="ActionFilterWrapper"/> class.
/// </summary>
/// <param name="filterMetadata">The filter metadata.</param>
public ActionFilterWrapper(FilterMetadata filterMetadata)
{
if (filterMetadata == null)
{
throw new ArgumentNullException(nameof(filterMetadata));
}
this._filterMetadata = filterMetadata;
}
/// <summary>
/// Gets the metadata key used to retrieve the filter metadata.
/// </summary>
public virtual string MetadataKey
{
get { return AutofacWebApiFilterProvider.ActionFilterMetadataKey; }
}
/// <summary>
/// Occurs after the action method is invoked.
/// </summary>
/// <param name="actionExecutedContext">The context for the action.</param>
/// <param name="cancellationToken">A cancellation token for signaling task ending.</param>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="actionExecutedContext" /> is <see langword="null" />.
/// </exception>
public override async Task OnActionExecutedAsync(HttpActionExecutedContext actionExecutedContext, CancellationToken cancellationToken)
{
if (actionExecutedContext == null)
{
throw new ArgumentNullException(nameof(actionExecutedContext));
}
var dependencyScope = actionExecutedContext.Request.GetDependencyScope();
var lifetimeScope = dependencyScope.GetRequestLifetimeScope();
var filters = lifetimeScope.Resolve<IEnumerable<Meta<Lazy<IAutofacActionFilter>>>>();
foreach (var filter in filters.Where(this.FilterMatchesMetadata))
{
await filter.Value.Value.OnActionExecutedAsync(actionExecutedContext, cancellationToken);
}
}
/// <summary>
/// Occurs before the action method is invoked.
/// </summary>
/// <param name="actionContext">The context for the action.</param>
/// <param name="cancellationToken">A cancellation token for signaling task ending.</param>
/// <exception cref="System.ArgumentNullException">
/// Thrown if <paramref name="actionContext" /> is <see langword="null" />.
/// </exception>
public override async Task OnActionExecutingAsync(HttpActionContext actionContext, CancellationToken cancellationToken)
{
if (actionContext == null)
{
throw new ArgumentNullException(nameof(actionContext));
}
var dependencyScope = actionContext.Request.GetDependencyScope();
var lifetimeScope = dependencyScope.GetRequestLifetimeScope();
var filters = lifetimeScope.Resolve<IEnumerable<Meta<Lazy<IAutofacActionFilter>>>>();
foreach (var filter in filters.Where(this.FilterMatchesMetadata))
{
await filter.Value.Value.OnActionExecutingAsync(actionContext, cancellationToken);
}
}
private bool FilterMatchesMetadata(Meta<Lazy<IAutofacActionFilter>> filter)
{
var metadata = filter.Metadata.ContainsKey(this.MetadataKey)
? filter.Metadata[this.MetadataKey] as FilterMetadata : null;
return metadata != null
&& metadata.ControllerType == this._filterMetadata.ControllerType
&& metadata.FilterScope == this._filterMetadata.FilterScope
&& metadata.MethodInfo == this._filterMetadata.MethodInfo;
}
}
}