Making an Asynchronous and Cache-Friendly Proxy ASP.NET

You can make proxy methods asynchronous by using the new Ajax ASMX handler.This will solve a majority of the proxy web service’s scalability problems.Moreover,the proxy service will become cache-friendly for browsers, and they will be able to down load responses faster from the proxy by using the Content-Length header.

The Get String and Get Xml method can become asynchronous very easily by using the Http Web Request class and its asynchronous methods. Http Web Request has the Begin Get Response function, which works asynchronously. So, you just need to call Begin Response in the Begin Get String class of the proxy (see Example ).

A proxy’s Begin Get String function asynchronously downloads responses from an external source

private class GetStringState :AsyncWebMethodState
public HttpWebRequest Request;
public string Url;
public int CacheDuration;
public GetStringState(object state): base(state) {}
public IAsyncResult BeginGetString(string url, int cacheDuration, AsyncCallback cb, object state)
// See if the response from the URL is already cached on server
string cachedContent = Context.Cache[url] as string;
if (!string.IsNullOrEmpty(cachedContent))
this.CacheResponse(Context, cacheDuration);
return new AsmxHandlerSyncResult(cachedContent);
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
GetStringState myState = new GetStringState(state);
myState.Request = request;
myState.Url = url;
myState.CacheDuration = cacheDuration;
return request.BeginGetResponse(cb, myState);


The Begin Get String method has two modes.It executes them synchronously when the content is cached in the ASP.NET cache. Then there’s no need to return the thread to the thread pool because the method can complete right away. If there isn’t any content in the cache, it makes a Begin Get Response call and returns execution to the ASMX handler.The custom state object, Get String State, inherits from the Async Web Method State defined in the AJAXASMX Handler project. In its constructor, it takes the original state object passed down to the Begin Get String function.The ASMXHttp Handler needs the original state so that it can fire the End Get String function on proper context.

When Http Web Request gets the response,it fires the ASMX handler’s callback.The ASMX handler,in turn,calls EndGetString to complete the response.End Get String downloads the response,caches it, and returns it as a return value (see Example).

The EndGetString method of a proxy web service

public string EndGetString(IAsyncResult result)

The EndGetString method of a proxy web service (continued)


GetStringState state = result.AsyncState as GetStringState;

HttpWebRequest request = state.Request;

using(HttpWebResponse response =request.EndGetResponse(result) as HttpWebResponse)


using(StreamReader reader = new

StreamReader(response.GetResponseStream( )))


string content = reader.ReadToEnd( );

state.Context.Cache.Insert(state.Url, content, null,



CacheItemPriority.Normal, null);

// produce cache headers for response caching

this.CacheResponse(state.Context, state.CacheDuration);

return content;




Keep in mind that the Context object is unavailable in the End Get String function because this function is fired on a different thread that is no longer tied to the original thread that initiated the HTTP request.So, you need to get a reference to the original Context from the state object.Similarly, you can make GetRss asynchronous by introducing a BeginGetRss and End Get Rss pair.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

ASP.NET Topics