6

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

15
  • 3
    I think you mean "asynchronous". Commented Feb 18, 2011 at 1:47
  • Why do you want it to be asynchronous? Is it because calling a simple web-service is not fast enough for you? Commented Feb 18, 2011 at 1:50
  • I want to log things but I don't want what I'm doing to slow down the user's experience. So if this webservice craps out or is slow for some reason, I want the main web application to keep trucking along. So if that is asynchronous or synchronous, I'd like to do that. Commented Feb 18, 2011 at 2:08
  • Asynchronous is definitely what you want then. I've edited your post to reflect your intent. Commented Feb 18, 2011 at 2:19
  • Are you calling the web service server side or client side through javascript? Commented Feb 18, 2011 at 2:20

3 Answers 3

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

This MSDN article had a good breakdown of how to create the methods you mentioned above. Posted by @gbvb
2

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 .

10 Comments

This is good information but it refers to implementing an async web service. guanome asked about calling a web service asynchronously.
got it. I was seeing it from the web service side. I guess 4 beers is one too many to answer on stackoverflow. :)
@gbvb, hold onto that answer, I'm sure the right question will come along. :-)
Implementing a web service asynchronously is far less common and not always beneficial. The server side of a web service performs some actions and returns a result. With the techniques mentioned above the actions can be broken up into parts which is helpful if the service in turn waits on other resources, like a long db call, another web service call. Async web services are also ideally suited for implementing long polling.
Also note that when consuming web services, some environments like .NET, offer both sync and async calls, but others like JavaScript only offer async and even others like ColdFusion only offer sync calls.
|
0

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.

6 Comments

Will the page this is calling the web service have to wait on a response from the web service?
It sounds like what you want is an asynchronous http request (AJAX). Look up XmlHttpRequest. jQuery has a good API that can help you. If you use Ajax, your page will not have to wait on a response from the webservice.
I've used the jquery ajax method before, but I need to be able to call the web service from the server side.
Can I use XmlHttpRequest on the server side?
@Smartcaveman, this answer, or more specifically the follow on vomments, really doesn't answer the question. Guanome is asking how to call a web service asynchronoudly from a c# client. Answers with XmlHttpRequest and jQuery do not fit here.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.