Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

Tweaking WCF to build highly scalable async REST API

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

About the Author


| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 31 Jul 2011
Article Copyright 2011 by Omar Al Zabir
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid