Hi,
I'm trying to find out how one should create f.ex. an implementation of a IHttpAsyncHandler which again creates several async requests to different endpoints.
I've tried the following:
using System;
using System.Net;
using System.Threading;
using System.Web;
namespace MyExampleNameSpace
{
public class MyExampleHandler : IHttpAsyncHandler
{
public void ProcessRequest(HttpContext context) { throw new NotImplementedException(); }
public bool IsReusable
{
get
{
return false;
}
}
private AsyncProcessorDelegate _Delegate;
protected delegate void AsyncProcessorDelegate(HttpContext context);
private static int timeoutInMilliseconds = 2000;
public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
{
_Delegate = new AsyncProcessorDelegate(ServeContent);
return _Delegate.BeginInvoke(context, cb, extraData);
}
private void ServeContent(HttpContext context)
{
HttpWebRequest firstRequest = (HttpWebRequest)WebRequest.Create("http://firstSite");
firstRequest.AllowAutoRedirect = false;
firstRequest.Method = "GET";
firstRequest.Proxy = null;
HttpWebRequest secondRequest = (HttpWebRequest)WebRequest.Create("http://secondSite");
secondRequest.AllowAutoRedirect = false;
secondRequest.Method = "GET";
secondRequest.Proxy = null;
var firstRequestAsyncResult = firstRequest.BeginGetResponse(null, null);
var secondRequestAsyncResult = secondRequest.BeginGetResponse(null, null);
var waitHandles = new WaitHandle[] { firstRequestAsyncResult.AsyncWaitHandle, secondRequestAsyncResult.AsyncWaitHandle };
bool b = WaitHandle.WaitAll(waitHandles, timeoutInMilliseconds);
if (firstRequestAsyncResult.IsCompleted)
{
secondRequest.Abort();
var firstResponse = firstRequest.EndGetResponse(firstRequestAsyncResult);
WriteResponse(context, firstResponse);
return;
}
else if (secondRequestAsyncResult.IsCompleted)
{
firstRequest.Abort();
var secondResponse = secondRequest.EndGetResponse(secondRequestAsyncResult);
WriteResponse(context, secondResponse);
return;
}
WaitHandle.WaitAll(waitHandles);
var yetAnotherFirstResponseObject = firstRequest.EndGetResponse(firstRequestAsyncResult);
WriteResponse(context, yetAnotherFirstResponseObject);
}
private void WriteResponse(HttpContext context, WebResponse response)
{
}
public void EndProcessRequest(IAsyncResult result)
{
_Delegate.EndInvoke(result);
}
}
}
I know that I haven't implemented any errorhandling what so ever, but my question relates to if this approach can scale, or will the WaitHandle.WaitAll() cause blocking of my IIS, limiting concurrent requests.
I have also added to my web.config to override outbound concurrent request limits:
<system.net>
<connectionManagement>
<add address="http://firstSite" maxconnection="100"/>
<add address="http://secondSite" maxconnection="100"/>
</connectionManagement>
</system.net>
If this approach will block my IIS Threadpool.. How can one achieve my intended functionality without exhausting the resources and maintaining a high level of service. Any suggestions or articles which adresses this issue for NET 3.5?