@@ -43,7 +43,8 @@ public async Task<bool> Post(HttpContext httpContext, RequestDelegate next)
43
43
{
44
44
HttpContext = httpContext ,
45
45
Next = next ,
46
- CompletionSource = new TaskCompletionSource < object > ( )
46
+ CompletionSource = new TaskCompletionSource < object > ( ) ,
47
+ ExecutionContext = System . Threading . ExecutionContext . Capture ( )
47
48
} ;
48
49
49
50
if ( _requestQueue . Post ( item ) )
@@ -69,15 +70,26 @@ private void InitializeRequestQueue()
69
70
70
71
_requestQueue = new ActionBlock < HttpRequestItem > ( async item =>
71
72
{
72
- try
73
- {
74
- await item . Next . Invoke ( item . HttpContext ) ;
75
- item . CompletionSource . SetResult ( null ) ;
76
- }
77
- catch ( Exception ex )
73
+ TaskCompletionSource < object > complete = new TaskCompletionSource < object > ( ) ;
74
+
75
+ System . Threading . ExecutionContext . Run ( item . ExecutionContext , async _ =>
78
76
{
79
- item . CompletionSource . SetException ( ex ) ;
80
- }
77
+ try
78
+ {
79
+ await item . Next . Invoke ( item . HttpContext ) ;
80
+ item . CompletionSource . SetResult ( null ) ;
81
+ }
82
+ catch ( Exception ex )
83
+ {
84
+ item . CompletionSource . SetException ( ex ) ;
85
+ }
86
+ finally
87
+ {
88
+ complete . SetResult ( null ) ;
89
+ }
90
+ } , null ) ;
91
+
92
+ await complete . Task ;
81
93
} , blockOptions ) ;
82
94
}
83
95
@@ -97,6 +109,8 @@ private class HttpRequestItem
97
109
/// Gets or sets the completion source to use.
98
110
/// </summary>
99
111
public TaskCompletionSource < object > CompletionSource { get ; set ; }
112
+
113
+ public System . Threading . ExecutionContext ExecutionContext { get ; set ; }
100
114
}
101
115
}
102
116
}
0 commit comments