logo       

Re: How asp.net knows that my asynchronous handler is finished?: msg#00105

windows.devel.dotnet.web

Subject: Re: How asp.net knows that my asynchronous handler is finished?

Your handler will implement the 'async pattern' that appears throughout .NET.
This is talked about here:


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconasynchronousexecution.asp

IHttpAsyncHandler is just one example of this pattern. (And if you're
implementing an async web service, you'll also implement a very similar
pattern.)

This pattern is the very same pattern that you consume when using async
delegate invocation - the IHttpAsyncHandler's BeginProcessRequest and
EndProcessRequest methods are very similar to the Delegate.BeginInvoke and
Delegate.EndInvoke methods.

So to answer the question: how does ASP.NET know the request is finished? The
answer will be: the same way you know that an async operation you launched has
finished.


This means that in practice there are actually several things it could do:

(1) It might pass in a non-null AsyncCallback when it calls
BeginProcessRequest. In which case you are required to call that delegate when
your async request completes.

(2) It might poll the IsCompleted property of the IAsyncResult that you return
from BeginProcessRequest

(3) It might retrieve the AsyncWaitHandle from the IAsyncResult that you return
from BeginProcessRequest, and wait for that to become signalled.

(4) It might just call your EndProcessRequest method without waiting for the
call to complete.


You should make sure all 4 of these work as otherwise you've not implemented
the pattern correctly. In practice it'll probably not use all the possible
techniques - in particular it would be rather surprising if it just used
technique (4). (It will call EndProcessRequest at some point, it would just be
surprising if it did that before it had reason to suppose the request was
complete.)

Just in case it's useful, here's an async web service I wrote as a test a while
ago. An async HTTP handler could use similar techniques. (This isn't a very
useful handler by the way - it just waits for 10 seconds... I wrote it in
order to verify that handlers like this don't consume any threads while they
run.)


<%@ webservice language="c#" class="WebServiceAsync" %>

using System;
using System.Threading;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;

public class WebServiceAsync : System.Web.Services.WebService
{
[WebMethod]
public IAsyncResult BeginHelloWorld(
AsyncCallback cb,
object s)
{
return new CallObject(10000, cb, s);
}

[WebMethod]
public string EndHelloWorld(IAsyncResult call)
{
CallObject callObject = (CallObject)call;

callObject.WaitForCompletion();
return "foo";
}

private class CallObject : IAsyncResult
{
public CallObject(int milliseconds, AsyncCallback cb, object state)
{
this.state = state;
completed = false;
this.acb = cb;

Timer t = new Timer(new TimerCallback(OnTimer),
null, milliseconds, Timeout.Infinite);
}
private AsyncCallback acb;

private void OnTimer(object state)
{
completed = true;
lock (this)
{
if (wh != null)
{
wh.Set();
}
}
acb(this);
}

public void WaitForCompletion()
{
if (!completed)
{
wh.WaitOne();
}
lock (this)
{
if (wh != null)
{
wh.Close();
}
}
}

public object AsyncState
{
get { return state; }
}
private object state;

public WaitHandle AsyncWaitHandle
{
get
{
lock (this)
{
if (wh == null)
{
wh = new ManualResetEvent(completed);
}
return wh;
}
}
}
private ManualResetEvent wh;

public bool CompletedSynchronously
{
get { return false; }
}

public bool IsCompleted
{
get { return completed; }
}
private volatile bool completed;


}

}

--
Ian Griffiths


> -----Original Message-----
> From: Discussion of building .NET applications targeted for the Web
> [mailto:DOTNET-WEB@xxxxxxxxxxxxxxxxxxx] On Behalf Of Javier Martinez
> Álvarez
> Sent: 25 January 2005 14:22
> To: DOTNET-WEB@xxxxxxxxxxxxxxxxxxx
> Subject: [DOTNET-WEB] How asp.net knows that my asynchronous handler is
> finished?
>
> Hi
>
>
>
> I have a class implementig IHttpAsyncHandler, the class start a
> asynchronous
> in the method BeginProcessRequest, all works fine.
>
>
>
> But I have a doubt:
>
>
>
> How asp.net knows that my asynchronous request has finished? Is doing
> polling over the IAsyncResult returned by BeginProcessRequest or is doing
> something more smart?
>
>
>
> If is doing pooling, which is the thread doing this operation? A thread of
> ThreadPool, I suppose not, because in this case will the trhread will be
> blocked until the operation finished.
>
>
>
>
>
> Thanks in advance
>
> Javier
>
>
>
>
>
>
> ===================================
> This list is hosted by DevelopMentor® http://www.develop.com
>
> View archives and manage your subscription(s) at
> http://discuss.develop.com

===================================
This list is hosted by DevelopMentor® http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com



<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise