-
Notifications
You must be signed in to change notification settings - Fork 9.8k
/
AnalysisBuilder.cs
75 lines (63 loc) · 2.38 KB
/
AnalysisBuilder.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
namespace Microsoft.AspNetCore.MiddlewareAnalysis;
/// <summary>
/// An <see cref="IApplicationBuilder"/> decorator used by <see cref="AnalysisStartupFilter"/>
/// to add <see cref="AnalysisMiddleware"/> before and after each other middleware in the pipeline.
/// </summary>
public class AnalysisBuilder : IApplicationBuilder
{
private const string NextMiddlewareName = "analysis.NextMiddlewareName";
/// <summary>
/// Initializes a new instance of <see cref="AnalysisBuilder"/>.
/// </summary>
/// <param name="inner">The <see cref="IApplicationBuilder"/> to decorate.</param>
public AnalysisBuilder(IApplicationBuilder inner)
{
InnerBuilder = inner;
}
private IApplicationBuilder InnerBuilder { get; }
/// <inheritdoc />
public IServiceProvider ApplicationServices
{
get { return InnerBuilder.ApplicationServices; }
set { InnerBuilder.ApplicationServices = value; }
}
/// <inheritdoc />
public IDictionary<string, object?> Properties
{
get { return InnerBuilder.Properties; }
}
/// <inheritdoc />
public IFeatureCollection ServerFeatures
{
get { return InnerBuilder.ServerFeatures; }
}
/// <inheritdoc />
public RequestDelegate Build()
{
// Add one maker at the end before the default 404 middleware (or any fancy Join middleware).
return InnerBuilder.UseMiddleware<AnalysisMiddleware>("EndOfPipeline")
.Build();
}
/// <inheritdoc />
public IApplicationBuilder New()
{
return new AnalysisBuilder(InnerBuilder.New());
}
/// <inheritdoc />
public IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware)
{
var middlewareName = string.Empty; // UseMiddleware doesn't work with null params.
if (Properties.TryGetValue(NextMiddlewareName, out var middlewareNameObj) && middlewareNameObj != null)
{
middlewareName = middlewareNameObj.ToString();
Properties.Remove(NextMiddlewareName);
}
return InnerBuilder.UseMiddleware<AnalysisMiddleware>(middlewareName)
.Use(middleware);
}
}