Click here to Skip to main content
15,891,033 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.7K   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;
using System.IO;

namespace ProxyStuff
{
    public class StreamWrapper : Stream, IDisposable
    {
        private Stream WrappedStream;
        private Action<byte[]> OnCompleteRead;
        private MemoryStream InternalBuffer;

        public StreamWrapper(Stream stream, int internalBufferCapacity, Action<byte[]> onCompleteRead)
        {
            this.WrappedStream = stream;
            this.OnCompleteRead = onCompleteRead;
            this.InternalBuffer = new MemoryStream(internalBufferCapacity);
        }
        public override bool CanRead
        {
            get { return this.WrappedStream.CanRead; }
        }

        public override bool CanSeek
        {
            get { return this.WrappedStream.CanSeek; }
        }

        public override bool CanWrite
        {
            get { return this.WrappedStream.CanWrite; }
        }

        public override void Flush()
        {
            this.WrappedStream.Flush();
        }

        public override long Length
        {
            get { return this.WrappedStream.Length; }
        }

        public override long Position
        {
            get
            {
                return this.WrappedStream.Position;
            }
            set
            {
                this.WrappedStream.Position = value;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            int bytesRead = this.WrappedStream.Read(buffer, offset, count);

            if (bytesRead > 0)
                this.InternalBuffer.Write(buffer, offset, bytesRead);
            else
                this.OnCompleteRead(this.InternalBuffer.ToArray());

            return bytesRead;
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return this.WrappedStream.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            this.WrappedStream.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            this.WrappedStream.Write(buffer, offset, count);
        }

        public new void Dispose()
        {
            this.WrappedStream.Dispose();
        }
    }

}

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