Click here to Skip to main content
15,910,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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);

            // Check if 
            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)
        {
            // Write the data to the contex.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?
Posted
Updated 16-Feb-15 3:48am
v2
Comments
sashje- 16-Feb-15 9:56am    
I think i have read somewhere that using this approach with a delegate will force the thread being used for executing the "ServeContent" method, meaning that this wouldn't be async at all. But how can i aggregate data from several sources async?
Richard Deeming 16-Feb-15 16:05pm    
If upgrading to .NET 4.5 is an option, this would be much easier. :)
HttpTaskAsyncHandler Class - MSDN[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900