-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDefaultApplicationBuilderFactory.cs
75 lines (71 loc) · 2.79 KB
/
DefaultApplicationBuilderFactory.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
/* ===============================================
* 功能描述:AspNetCore.Extensions.Internal.ApplicationBuilderFactory
* 创 建 者:WeiGe
* 创建日期:10/16/2018 6:19:08 PM
* ===============================================*/
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Builder.Internal;
using Microsoft.AspNetCore.Hosting.Builder;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
namespace Microsoft.AspNetCore.Builder
{
internal class DefaultApplicationBuilderFactory : IApplicationBuilderFactory
{
static private ConcurrentBag<BuilderAction> _builderActions
= new ConcurrentBag<BuilderAction>();
private readonly IServiceProvider _serviceProvider;
public DefaultApplicationBuilderFactory(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
//Func<IServiceProvider, List<ServiceDescriptor>> GetDescriptors;
public IApplicationBuilder CreateBuilder(IFeatureCollection serverFeatures)
{
var builder = new ApplicationBuilder(_serviceProvider, serverFeatures);
while (_builderActions!=null&&!_builderActions.IsEmpty)
{
BuilderAction action;
if (_builderActions.TryTake(out action))
{
action.Action?.Invoke(builder);
action.ActionWithState?.Invoke(builder, action.State);
}
else {
throw new Exception("TryTake");
}
}
_builderActions = null;
return builder;
}
public static void OnCreateBuilder(Action<IApplicationBuilder> builderAction)
{
if (builderAction != null)
{
_builderActions.Add(new BuilderAction { Action = builderAction });
}
}
public static void OnCreateBuilder(Action<IApplicationBuilder, object> builderAction, object state)
{
if (state == null)
{
throw new ArgumentNullException(nameof(state), "Please use OnCreateBuilder(Action<IApplicationBuilder> builderAction).");
}
if (builderAction != null)
{
_builderActions.Add(new BuilderAction { ActionWithState = builderAction, State = state });
}
}
class BuilderAction
{
public object State { get; set; }
public Action<IApplicationBuilder, object> ActionWithState { get; set; }
public Action<IApplicationBuilder> Action { get; set; }
}
}
}