How to asynchronously call a web service from an ASP.NET application?

I need to invoke a web service asynchronously from an ASP.NET application. Aspx does not need a response from the web service. This is a simple notification.

I use the ...Async() method from the web service stub and <%@Page Async="True" %> .

 ws.HelloWorldAsync(); 

My problem: a webpage request is awaiting a web service response.

How to solve this problem? How to avoid resource leakage when disabling a web service or when overloading?

+6
asynchronous web-services
source share
4 answers

A web service proxy usually has a Begin and End method. You can use them. The example below shows how you can call the begin method and use the callback to end the call. A call to MakeWebServiceAsynCall will immediately return. The using statement ensures that the object will be deleted safely.

 void MakeWebServiceAsynCall() { WebServiceProxy proxy = new WebServiceProxy(); proxy.BeginHelloWorld(OnCompleted, proxy); } void OnCompleted(IAsyncResult result) { try { using (WebServiceProxy proxy = (WebServiceProxy)result.AsyncState) proxy.EndHelloWorld(result); } catch (Exception ex) { // handle as required } } 

If you need to know if the call was successful or not, you will need to wait for the result.

+1
source share

In your scenario, you can use ThreadPool ThreadPool.QueueUserWorkItem (...) to call a web service in a federated chain.

0
source share

I have used simple threads to do this before. eg:

 Thread t = new Thread(delegate() { ws.HelloWorld(); }); t.Start(); 

The thread will continue to work after the method returns. Looking back, it seems that the ThreadPool approach is not always recommended.

0
source share

Starting a new thread is probably the easiest solution since you don't care about getting notified of the results.

 new Thread(() => ws.HelloWorld()).Start 
0
source share

All Articles