diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2013-02-10 15:45:14 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2013-02-10 15:45:14 -0800 |
commit | 8d530aa91c05b14be12c5b1177f39eb5f62c669e (patch) | |
tree | e3da76a89c8d39ac61d36024ae298c4d931cf318 /samples/DotNetOpenAuth.ApplicationBlock/HttpAsyncHandlerBase.cs | |
parent | 4e85bc002819c123a6e2dd88f18fcc82fa78c38f (diff) | |
download | DotNetOpenAuth-8d530aa91c05b14be12c5b1177f39eb5f62c669e.zip DotNetOpenAuth-8d530aa91c05b14be12c5b1177f39eb5f62c669e.tar.gz DotNetOpenAuth-8d530aa91c05b14be12c5b1177f39eb5f62c669e.tar.bz2 |
Fixes up WCF OAuth 1 samples.
Diffstat (limited to 'samples/DotNetOpenAuth.ApplicationBlock/HttpAsyncHandlerBase.cs')
-rw-r--r-- | samples/DotNetOpenAuth.ApplicationBlock/HttpAsyncHandlerBase.cs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/HttpAsyncHandlerBase.cs b/samples/DotNetOpenAuth.ApplicationBlock/HttpAsyncHandlerBase.cs new file mode 100644 index 0000000..a72a9b1 --- /dev/null +++ b/samples/DotNetOpenAuth.ApplicationBlock/HttpAsyncHandlerBase.cs @@ -0,0 +1,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; + } + } +} |