forked from dotnetcore/AspectCore-Framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
183 lines (152 loc) · 4.64 KB
/
Copy pathProgram.cs
File metadata and controls
183 lines (152 loc) · 4.64 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AspectCore.DynamicProxy;
using AspectCore.Extensions.AspectScope;
using AspectCore.Injector;
namespace AspectScope.Sample
{
class Program
{
static void Main(string[] args)
{
IServiceContainer serviceContainer = new ServiceContainer();
serviceContainer.AddAspectScope();
serviceContainer.AddType<IA, A>();
serviceContainer.AddType<IB, B>();
serviceContainer.AddType<IC, C>();
var r = serviceContainer.Build();
Task.WaitAll(
Task.Run(() => { r.CreateScope().Resolve<IA>().None(); }),
Task.Run(() => { r.CreateScope().Resolve<IB>().None(); }),
Task.Run(() => { r.CreateScope().Resolve<IC>().None(); }),
Task.Run(() => { r.CreateScope().Resolve<IA>().Nested(); }),
Task.Run(() => { r.CreateScope().Resolve<IB>().Nested(); }),
Task.Run(() => { r.CreateScope().Resolve<IC>().Nested(); }),
Task.Run(() => { r.CreateScope().Resolve<IA>().Aspect(); }),
Task.Run(() => { r.CreateScope().Resolve<IB>().Aspect(); }),
Task.Run(() => { r.CreateScope().Resolve<IC>().Aspect(); }));
IServiceContainer serviceContainer1 = new ServiceContainer();
serviceContainer1.AddAspectScope();
serviceContainer1.AddType<IA, A>();
serviceContainer1.AddType<IB, B>();
serviceContainer1.AddType<IC, C>();
Console.WriteLine();
var r1 = serviceContainer1.Build();
r1.Resolve<IA>().None();
r1.Resolve<IB>().None();
r1.Resolve<IC>().None();
r1.Resolve<IA>().Nested();
r1.Resolve<IB>().Nested();
r1.Resolve<IC>().Nested();
r1.Resolve<IA>().Aspect();
r1.Resolve<IB>().Aspect();
r1.Resolve<IC>().Aspect();
Console.ReadKey();
}
}
public interface IA
{
[ScopeIntercept(Scope=Scope.None)]
void None();
[ScopeIntercept(Scope = Scope.Nested)]
void Nested();
[ScopeIntercept(Scope = Scope.Aspect)]
void Aspect();
}
public interface IB
{
[ScopeIntercept(Scope = Scope.None)]
void None();
[ScopeIntercept(Scope = Scope.Nested)]
void Nested();
[ScopeIntercept(Scope = Scope.Aspect)]
void Aspect();
}
public interface IC
{
[ScopeIntercept(Scope = Scope.None)]
void None();
[ScopeIntercept(Scope = Scope.Nested)]
void Nested();
[ScopeIntercept(Scope = Scope.Aspect)]
void Aspect();
}
public class A : IA
{
private readonly IB b;
private readonly IC c;
public A(IB b, IC c)
{
this.b = b;
this.c = c;
}
public void Aspect()
{
b.Aspect();
}
public void Nested()
{
b.Nested();
}
public void None()
{
b.None();
}
}
public class B : IB
{
private readonly IC c;
public B(IC c)
{
this.c = c;
}
public void None()
{
c.None();
}
public void Nested()
{
c.Nested();
}
public void Aspect()
{
c.Aspect();
}
}
public class C : IC
{
public void None()
{
}
public void Nested()
{
}
public void Aspect()
{
}
}
public class ScopeIntercept : ScopeInterceptorAttribute
{
public override Scope Scope { get; set; } = Scope.None;
public override Task Invoke(AspectContext context, AspectDelegate next)
{
Console.WriteLine("trace id: {0} . execute method : {1}.{2}", GetTraceId(context), context.ServiceMethod.DeclaringType, context.ServiceMethod.Name);
return context.Invoke(next);
}
private string GetTraceId(AspectContext currentContext)
{
var scheduler = (IAspectScheduler)currentContext.ServiceProvider.GetService(typeof(IAspectScheduler));
var firstContext = scheduler.GetCurrentContexts().First();
if (firstContext.AdditionalData.TryGetValue("trace-id", out var traceId))
{
return traceId.ToString();
}
traceId = Guid.NewGuid();
firstContext.AdditionalData["trace-id"] = traceId;
return traceId.ToString();
}
}
}