This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
MiddlewareFilterAttribute.cs
54 lines (45 loc) · 1.96 KB
/
MiddlewareFilterAttribute.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
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using Microsoft.AspNetCore.Mvc.Filters;
using Microsoft.AspNetCore.Mvc.Internal;
using Microsoft.Extensions.DependencyInjection;
namespace Microsoft.AspNetCore.Mvc
{
/// <summary>
/// Executes a middleware pipeline provided the by the <see cref="MiddlewareFilterAttribute.ConfigurationType"/>.
/// The middleware pipeline will be treated as an async resource filter.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class MiddlewareFilterAttribute : Attribute, IFilterFactory, IOrderedFilter
{
/// <summary>
/// Instantiates a new instance of <see cref="MiddlewareFilterAttribute"/>.
/// </summary>
/// <param name="configurationType">A type which configures a middleware pipeline.</param>
public MiddlewareFilterAttribute(Type configurationType)
{
if (configurationType == null)
{
throw new ArgumentNullException(nameof(configurationType));
}
ConfigurationType = configurationType;
}
public Type ConfigurationType { get; }
/// <inheritdoc />
public int Order { get; set; }
/// <inheritdoc />
public bool IsReusable => true;
/// <inheritdoc />
public IFilterMetadata CreateInstance(IServiceProvider serviceProvider)
{
if (serviceProvider == null)
{
throw new ArgumentNullException(nameof(serviceProvider));
}
var middlewarePipelineService = serviceProvider.GetRequiredService<MiddlewareFilterBuilder>();
var pipeline = middlewarePipelineService.GetPipeline(ConfigurationType);
return new MiddlewareFilter(pipeline);
}
}
}