Adding TPL Sugar to HttpVerbAsyncUtility

Coordinator
Mar 2, 2012 at 12:53 AM

Sweet:

using System;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Fox.Net
{
    /// <summary>
    /// Static methods for asynchronous HTTP operations.
    /// </summary>
    public static partial class HttpVerbAsyncUtility
    {
        /// <summary>
        /// Gets as task.
        /// </summary>
        /// <param name="resourceIndicator">The resource indicator.</param>
        public static Task<string> GetAsTask(Uri resourceIndicator)
        {
            return GetAsTask(resourceIndicator, null);
        }

        /// <summary>
        /// Gets as task.
        /// </summary>
        /// <param name="resourceIndicator">The resource indicator.</param>
        /// <param name="getCompleteAction">The get complete action.</param>
        /// <remarks>
        /// For details, see the �Converting an Event-Based Pattern� section in 
        /// �Simplify Asynchronous Programming with Tasks�
        /// by Igor Ostrovsky
        /// [http://msdn.microsoft.com/en-us/magazine/ff959203.aspx]
        /// </remarks>
        public static Task<string> GetAsTask(Uri resourceIndicator, Action<object, DownloadStringCompletedEventArgs> getCompleteAction)
        {
            var completionSource = new TaskCompletionSource<string>();
            var web = new WebClient();
            web.Encoding = Encoding.UTF8;

            web.DownloadStringCompleted += (o, args) =>
            {
                try
                {
                    if (args.Error != null) completionSource.SetException(args.Error);
                    else if (args.Cancelled) completionSource.SetCanceled();
                    else
                    {
                        completionSource.SetResult(args.Result);
                        if (getCompleteAction != null) getCompleteAction.Invoke(o, args);
                    }
                }
                finally
                {
#if !SILVERLIGHT
                    web.Dispose();
#endif
                }
            };

            web.DownloadStringAsync(resourceIndicator);

            return completionSource.Task;
        }
    }
}

Edit:

using System;
using System.Net;
using System.Text;

namespace Fox.Net
{
    /// <summary>
    /// Static methods for asynchronous HTTP operations.
    /// </summary>
    public static partial class HttpVerbAsyncUtility
    {
        /// <summary>
        /// Gets the specified resource as a string.
        /// </summary>
        /// <param name="resourceIndicator">The resource indicator.</param>
        /// <param name="getCompleteAction">The get complete action.</param>
        public static void Get(Uri resourceIndicator, Action<object, DownloadStringCompletedEventArgs> getCompleteAction)
        {
            if (getCompleteAction == null) throw new ArgumentNullException("getCompleteAction", "The required GET-complete Action is null.");

            var web = new WebClient();
            web.Encoding = Encoding.UTF8;

            web.DownloadStringCompleted += (o, args) =>
            {
                try
                {
                    getCompleteAction.Invoke(o, args);
                }
                finally
                {
#if !SILVERLIGHT
                    web.Dispose();
#endif
                }
            };

            web.DownloadStringAsync(resourceIndicator);
        }

        /// <summary>
        /// Gets the specified resource as a stream.
        /// </summary>
        /// <param name="resourceIndicator">The resource indicator.</param>
        /// <param name="getCompleteAction">The get complete action.</param>
        public static void GetStream(Uri resourceIndicator, Action<object, OpenReadCompletedEventArgs> getCompleteAction)
        {
            if (getCompleteAction == null) throw new ArgumentNullException("getCompleteAction", "The required GET-complete Action is null.");

            var web = new WebClient();
            web.Encoding = Encoding.UTF8;

            web.OpenReadCompleted += (o, args) =>
            {
                try
                {
                    getCompleteAction.Invoke(o, args);
                }
                finally
                {
#if !SILVERLIGHT
                    web.Dispose();
#endif
                }
            };

            web.OpenReadAsync(resourceIndicator);
        }

        /// <summary>
        /// Posts the specified resource.
        /// </summary>
        /// <param name="resourceIndicator">The resource indicator.</param>
        /// <param name="postData">The post data.</param>
        /// <param name="postCompleteAction">The post complete action.</param>
        public static void Post(Uri resourceIndicator, string postData, Action<object, UploadStringCompletedEventArgs> postCompleteAction)
        {
            if (postCompleteAction == null) throw new ArgumentNullException("postCompleteAction", "The required POST-complete Action is null.");

            var web = new WebClient();
            web.Encoding = Encoding.UTF8;

            web.UploadStringCompleted += (o, args) =>
            {
                try
                {
                    postCompleteAction.Invoke(o, args);
                }
                finally
                {
#if !SILVERLIGHT
                    web.Dispose();
#endif
                }
            };

            web.UploadStringAsync(resourceIndicator, postData);
        }
    }
}