blob: a72a9b1a80d6ff4f670f1c556552a31124493e73 (
plain)
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
|
//-----------------------------------------------------------------------
// <copyright file="HttpAsyncHandlerBase.cs" company="Andrew Arnott">
// Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.ApplicationBlock {
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
public abstract class HttpAsyncHandlerBase : IHttpAsyncHandler {
public abstract bool IsReusable { get; }
public IAsyncResult BeginProcessRequest(HttpContext context, System.AsyncCallback cb, object extraData) {
return ToApm(this.ProcessRequestAsync(context), cb, extraData);
}
public void EndProcessRequest(IAsyncResult result) {
((Task)result).Wait(); // rethrows exceptions
}
public void ProcessRequest(HttpContext context) {
this.ProcessRequestAsync(context).GetAwaiter().GetResult();
}
protected abstract Task ProcessRequestAsync(HttpContext context);
private static Task ToApm(Task task, AsyncCallback callback, object state) {
if (task == null) {
throw new ArgumentNullException("task");
}
var tcs = new TaskCompletionSource<object>(state);
task.ContinueWith(
t => {
if (t.IsFaulted) {
tcs.TrySetException(t.Exception.InnerExceptions);
} else if (t.IsCanceled) {
tcs.TrySetCanceled();
} else {
tcs.TrySetResult(null);
}
if (callback != null) {
callback(tcs.Task);
}
},
CancellationToken.None,
TaskContinuationOptions.None,
TaskScheduler.Default);
return tcs.Task;
}
}
}
|