在Asp.net异步Web服务 [英] Asynchronous webservice in Asp.net

查看:137
本文介绍了在Asp.net异步Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置在asp.net异步Web服务?

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

我要调用一个web服务的一些数据发布到数据库中,但我不在乎,如果响应失败或成功。

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

我只能.NET使用2.0或3.5,它可以在VB或C#。

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

推荐答案

在创建Visual Studio中的服务引用点击高级按钮,勾选生成异步操作。然后,你将不得不作出对Web服务异步调用的选项。

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.

下面是两个同步和公共Web服务相同的异步调用的样本。

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);
}



这可能是很有诱惑力的只是调用的BeginXXX 与不做,结果什么。实际上,你会虽然泄漏资源。重要的是,每个的BeginXXX 呼叫与相应的 EndXxx 呼叫匹配。

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.

即使你有一个回调,调用 EndXxx ,这是触发一个线程池线程并调用原来的线程的BeginXXX 是免费尽快完成的的BeginXXX 调用完成(它不等待响应)。

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

这篇关于在Asp.net异步Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆