Click here to Skip to main content
12,350,653 members (26,003 online)
Click here to Skip to main content

Stats

72.1K views
1K downloads
83 bookmarked
Posted

Tweaking WCF to build highly scalable async REST API

, 31 Jul 2011 CPOL
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)

Share

About the Author


You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160621.1 | Last Updated 31 Jul 2011
Article Copyright 2011 by Omar Al Zabir
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid