Click here to Skip to main content
15,885,216 members
Articles / General Programming / Performance

Tweaking WCF to build highly scalable async REST API

Rate me:
Please Sign up or sign in to vote.
4.92/5 (35 votes)
31 Jul 2011CPOL25 min read 118.5K   1.2K   85  
You can build async REST API using WCF but due to some bug in WCF implementation it does not scale as you would want it to. Here's my journey with Microsoft's WCF team to explore the problem and find the right fix.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AsyncServiceLibrary
{
    public static class WcfAsyncHelper
    {
        public static bool IsSync<TState>(IAsyncResult result)
        {
            return result is CompletedAsyncResult<TState>;
        }

        public static CustomAsyncResult<TState> BeginAsync<TState>(
            TState state,
            AsyncCallback wcfCallback, object wcfState,
            Func<TState, AsyncCallback, object, IAsyncResult> beginCall)
        {
            var customState = new CustomState<TState>(wcfCallback, wcfState, state);
            var externalServiceCallback = new AsyncCallback(CallbackFromExternalService<TState>);
            var externalServiceResult = beginCall(state, externalServiceCallback, customState);
            return new CustomAsyncResult<TState>(externalServiceResult, customState);
        }

        public static CompletedAsyncResult<TState> BeginSync<TState>(
            TState state,
            AsyncCallback wcfCallback, object wcfState)            
        {
            //var customState = new CustomState<TState>(wcfCallback, wcfState, state);
            var completedResult = new CompletedAsyncResult<TState>(state, wcfState);
            wcfCallback(completedResult);
            return completedResult;
        }

        private static void CallbackFromExternalService<TState>(IAsyncResult serviceResult)
        {
            var serviceState = serviceResult.AsyncState as CustomState<TState>;
            serviceState.WcfCallback(
                new CustomAsyncResult<TState>(serviceResult, serviceState));
        }

        public static TResult EndAsync<TState, TResult>(IAsyncResult result,
            Func<TState, IAsyncResult, TResult> endCall,
            Action<Exception, TState> onException,
            Action<TState> dispose)
        {
            var myAsyncResult = result as CustomAsyncResult<TState>;
            var myState = myAsyncResult.CustomState;
            try
            {
                return endCall(myState.State, myAsyncResult.ExternalAsyncResult);
            }
            catch (Exception x)
            {
                onException(x, myState.State);
                return default(TResult);
            }
            finally
            {
                try
                {
                    dispose(myState.State);
                }
                finally
                {
                }
            }
        }

        public static TResult EndSync<TState, TResult>(IAsyncResult result,
            Func<TState, IAsyncResult, TResult> endCall,
            Action<Exception, TState> onException,
            Action<TState> dispose)
        {
            var myAsyncResult = result as CompletedAsyncResult<TState>;
            var myState = myAsyncResult.Data;
            try
            {
                return endCall(myState, myAsyncResult);
            }
            catch (Exception x)
            {
                onException(x, myState);
                return default(TResult);
            }
            finally
            {
                try
                {
                    dispose(myState);
                }
                finally
                {
                }
            }
        }
    }

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

Comments and Discussions