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 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.
/// Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Activation;
//using Microsoft.ServiceModel.Web;
using System.Linq;
using System.Net;
using System.IO;
using System.Text;
using ProxyStuff;
using System.Web;

// The following line sets the default namespace for DataContract serialized typed to be ""
[assembly: ContractNamespace("", ClrNamespace = "WcfAsyncRestApi")]
namespace WcfAsyncRestApi
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple),
    AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed),
    ServiceContract]
    public partial class RegularService
    {
        /// <summary>
        /// Returns the content from destination URL and caches the response in the browser for specified seconds.
        /// </summary>
        /// <param name="url">URL to fetch</param>
        /// <param name="cacheDuration">Cache duration</param>
        /// <returns></returns>
        [OperationContract()]
        //[WebGet(BodyStyle = WebMessageBodyStyle.Bare, UriTemplate="/Url?uri={url}&cacheDuration={cacheDuration}")]
        [WebGet(BodyStyle=WebMessageBodyStyle.Bare)]
        public Stream GetUrl(string url, int cacheDuration)
        {
            using (WebClient client = new WebClient())
            {
                return new MemoryStream(client.DownloadData(url));
            }
        }
    }

}

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

Omar Al Zabir
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

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