I need to process long running requests inside IIS, handling the request itself is very lightweight but takes a lot of time mostly due to IO. Basically I need to query another server, which sometimes queries a third server. So I want to process as many requests as I can simultaneously. For that I need to process the requests asynchronously how do I do that correctly?
Using the Socket class I could easily write something like :
// ..listening code
// ..Accepting code
void CalledOnRecive(Request request)
{
//process the request a little
Context context = new Context()
context.Socket = request.Socket;
remoteServer.Begin_LongRunningDemonicMethod(request.someParameter, DoneCallBack, context);
}
void DoneCallBack( )
{
IAsyncresult result = remoteServer.Begin_CallSomeVeryLongRunningDemonicMethod( request.someParameter, DoneCallBack, context);
Socket socket = result.Context.Socket;
socket.Send(result.Result);
}
In the example above the thread is released as soon as I call the "Begin..." method, and the response is sent on another thread, so you can easily achieve very high concurrency . How do you do the same in an HTTP handler inside IIS?
An HTTPhandler may be defined as an end point that is executed in response to a request and is used to handle specific requests based on extensions. The ASP.Net runtime engine selects the appropriate handler to serve an incoming request based on the file extension of the request URL.
To create a custom HTTP handler, you create a class that implements the IHttpHandler interface to create a synchronous handler. Alternatively, you can implement IHttpAsyncHandler to create an asynchronous handler. Both handler interfaces require that you implement the IsReusable property and the ProcessRequest method.
aspnet_isapi. dll initiates HttpRuntime. HttpRutime calls the ProcessRequest method in the page requested which starts processing of the request. During this time, it also creates and initializes core objects such as HttpContext, HttpRequest, and HttpResponse.
When the server receives an HTTP request, it processes that request and responds to the client. The response tells the client if the request was successful or not. ASP.NET Core is a web application framework that runs on the server.
You can implement an HttpHandler with IHttpAsyncHandler
. MSDN has a nice walkthrough with examples on how to do that here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With