Skip to content

Commit 3ab6fff

Browse files
committed
Merge branch 'master' into dev
2 parents e78cc9f + f93da03 commit 3ab6fff

File tree

1 file changed

+23
-9
lines changed

1 file changed

+23
-9
lines changed

src/WebJobs.Script.WebHost/HttpRequestQueue.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public async Task<bool> Post(HttpContext httpContext, RequestDelegate next)
4343
{
4444
HttpContext = httpContext,
4545
Next = next,
46-
CompletionSource = new TaskCompletionSource<object>()
46+
CompletionSource = new TaskCompletionSource<object>(),
47+
ExecutionContext = System.Threading.ExecutionContext.Capture()
4748
};
4849

4950
if (_requestQueue.Post(item))
@@ -69,15 +70,26 @@ private void InitializeRequestQueue()
6970

7071
_requestQueue = new ActionBlock<HttpRequestItem>(async item =>
7172
{
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 _ =>
7876
{
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;
8193
}, blockOptions);
8294
}
8395

@@ -97,6 +109,8 @@ private class HttpRequestItem
97109
/// Gets or sets the completion source to use.
98110
/// </summary>
99111
public TaskCompletionSource<object> CompletionSource { get; set; }
112+
113+
public System.Threading.ExecutionContext ExecutionContext { get; set; }
100114
}
101115
}
102116
}

0 commit comments

Comments
 (0)