How to make a web service method that calls an async call wait?

N

None

I am creating a web service method that will make an async. call. I need
the web service method to wait for the callback from that async call and
return an int passed to the callback.

I can't figure out a way to do this. Any ideas?
 
N

None

I guess this would work. Is there a simpler way?

public delegate void MyCallbackDelegate(int data);

public class MyWebService : System.Web.Services.WebServices
{
public void MyMethod()
{
CallbackWrapper callbackWrapper = new CallbackWrapper();
DoAsyncStuff(new MyCallbackDelegate(callbackWrapper.Callback));
callbackWrapper.WaitForCallback();
}
}

public class CallbackWrapper()
{
bool done = false;
int myInt;

public void Callback(int data)
{
myInt = data;
done = true;
}

public void WaitForCallback()
{
while (done == false); // Can add timeout logic as well
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

None,

I would create a delegate like you are, then call the BeginInvoke method
on the delegate. It will return an implementation of IAsyncResult to you.
Once you have that, you can call the WaitOne method on the WaitHandle
returned by the AsyncWaitHandle property.

This would require you to have a blocking version of DoAsyncStuff
basically. You don't have to worry about the threading, since the
delegate's implementation of BeginInvoke will handle that for you.

Hope this helps.
 
N

None

Thanks for the reply.

The problem is that I *don't* have a blocking version of DoAsyncStuff!
That's basically what I am trying to achieve.

The solution I posted is working well so I'll go with that for now.
 
S

Sami Vaaraniemi

There are some potential issues in making asynchronous server side web
methods. If you call BeginInvoke on a delegate and wait for the results,
then you are not really gaining anything. The original thread is waiting and
thus it won't be doing anything productive such as servicing other requests.
BeginInvoke takes another thread from the same thread pool that ASP.NET uses
to service requests. You end up using two precious threads from the thread
pool.

To read more about server-side asynchronous processing with ASP.NET, check
out these articles:

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

http://msdn.microsoft.com/msdnmag/issues/03/06/Threading/default.aspx

Regards,
Sami
www.capehill.net

Nicholas Paldino said:
None,

I would create a delegate like you are, then call the BeginInvoke method
on the delegate. It will return an implementation of IAsyncResult to you.
Once you have that, you can call the WaitOne method on the WaitHandle
returned by the AsyncWaitHandle property.

This would require you to have a blocking version of DoAsyncStuff
basically. You don't have to worry about the threading, since the
delegate's implementation of BeginInvoke will handle that for you.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

None said:
I guess this would work. Is there a simpler way?

public delegate void MyCallbackDelegate(int data);

public class MyWebService : System.Web.Services.WebServices
{
public void MyMethod()
{
CallbackWrapper callbackWrapper = new CallbackWrapper();
DoAsyncStuff(new MyCallbackDelegate(callbackWrapper.Callback));
callbackWrapper.WaitForCallback();
}
}

public class CallbackWrapper()
{
bool done = false;
int myInt;

public void Callback(int data)
{
myInt = data;
done = true;
}

public void WaitForCallback()
{
while (done == false); // Can add timeout logic as well
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top