Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I need to asychronously call a web service from an ASP.NET application. The aspx does not need the answer from the web service. It's just a simple notification.

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

ws.HelloWorldAsync();

My problem: the web page request is waiting for the web service response.

How to solve this problem? How to avoid any resource leak when the web service is down or when there is an overload?

like image 546
Jorge Avatar asked Aug 27 '10 19:08

Jorge


1 Answers

A Web Service proxy normally has a Begin and End method too. You could use these. The example below shows how you can call the begin method and use a callback to complete the call. The call to MakeWebServiceAsynCall would return straight away. The using statement will make sure the object is disposed 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 whether the call was successful or not you would need to wait for the result.

like image 129
Tim Carter Avatar answered Nov 19 '22 19:11

Tim Carter