Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous webservice in Asp.net

How can I set up an asynchronous web service in asp.net?

I want to call a webservice to post some data to a database, but I don't care if the response failed or succeeded.

I can use .net 2.0 or 3.5 only and it can be in vb or c#.

like image 594
FarFigNewton Avatar asked Feb 18 '11 01:02

FarFigNewton


2 Answers

When you create the service reference in visual studio click the "Advanced..." button and check off "Generate asynchronous operations". Then you'll have the option to make asynchronous calls against the web service.

Here's a sample of both a synchronous and the same asynchronous call to a public web service.

// http://wsf.cdyne.com/WeatherWS/Weather.asmx?wsdl
using(var wf = new WeatherForecasts.WeatherSoapClient())
{
    // example synchronous call
    wf.GetCityForecastByZIP("20850");

    // example asynchronous call
    wf.BeginGetCityForecastByZIP("20850", result => wf.EndGetCityForecastByZIP(result), null);
}

It might be tempting to just call BeginXxx and not do anything with the result since you don't care about it. You'll actually leak resources though. It's important that every BeginXxx call is matched with a corresponding EndXxx call.

Even though you have a callback that calls EndXxx, this is triggered on a thread pool thread and the original thread that called BeginXxx is free to finish as soon as the BeginXxx call is done (it doesn't wait for the response).

like image 176
Samuel Neff Avatar answered Sep 28 '22 02:09

Samuel Neff


Webservices are usually for Request/Response style services. That said, there is a simple mechanism to do async implementation: http://msdn.microsoft.com/en-us/library/aa480516.aspx. There are ways to just do fire and forget webservices as well: http://blogs.x2line.com/al/archive/2007/05/21/3108.aspx using OneWay attribute on SoapDocumentMethod .

like image 42
gbvb Avatar answered Sep 28 '22 00:09

gbvb