Asynchronous webservice in Asp.net

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).


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 .


It doesn't sound like there is any reason that the web service has to be synchronous. Go async for maximum performance, especially if some of the tasks may be long running.


Comments

  1. Lopez

    • 2016/11/26

    Seems nice, right? Yes it's a really wonderful mechanism. In ASP.NET we have a mechanism to call web services asynchronously. This will help 

  2. Santoro

    • 2020/3/14

    The sample provided was designed to provide a simple demonstration of asynchronous programming in ASP.NET 4.5. The sample is not intended to be a reference architecture for asynchronous programming in ASP.NET. The sample program calls ASP.NET Web API methods which in turn call Task.Delay to simulate long-running web service calls. Most production applications will not show such obvious benefits to using asynchronous Methods.

  3. Moshe

    • 2018/3/21

    NET web applications use web services to get some data that they display to ASP.NET: Synchronous and asynchronous page processing models 

  4. Rafael

    • 2017/1/12

    The advantage of an async call on the server side is that it doesn't tie up an ASP.NET worker thread waiting for results (which can seriously impair scalability) Your server-side code can be structured either as a web page (*.aspx) or a WCF service, depending on what you want to have it return.

  5. Bernardi

    • 2018/8/11

    Hi, I have asp.net asmx web service which was implemented using async await as below, public Class SolutionService { [web method] public 

  6. Armando

    • 2020/1/26

    In ASP.NET 4.5, all the ASP.NET settings have good default values for asynchronous requests, but there are a couple of other settings you might want to change. The first is an IIS setting: consider raising the IIS/HTTP.sys queue limit (Application Pools | Advanced Settings | Queue Length) from its default of 1,000 to 5,000.

  7. Giordano

    • 2016/10/21

    The button calls the web methods in asynchronous fashion and the return results are displayed in the bulleted list. Also, set the Async property 

  8. Moretti

    • 2017/6/20

    larger --You might want to consider a one-way web service call. It is much easier to implement. Just a thought. Aside from that, I suggest you should make sure you web service is running in a separate web context, not the current web site, as I have had issues when the web service is part of the web site calling the web service.

  9. Henrik

    • 2015/9/26

    An asynchronous webpage in ASP.Net should have the async attribute set in the page directive. All that you need to do is specify Page Async=" 

  10. Santoro

    • 2015/9/3

    NET in C#". There are two ways to access web services from a client application synchronously and asynchronously. A synchronous Web service 

  11. Donald

    • 2015/10/7

    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 

  12. Grady

    • 2020/1/9

    When page initializes we will create new PageAsyncyTask and provide it with references to BeginRequest and EndRequest operations. First one of 

Comments are closed.

Recent Posts