异步WebService调用

大城市里の小女人 提交于 2020-02-06 03:17:08
    同步调用,主调函数要等待被调函数返回结果.  
    异步调用,不用等待被调函数返回,自行运行


  在.net1.x中,异步webservice异步调用的一般方式为调用方法xx对应
的BeginXX方法来完成,其过程类似于异步委托的使用。详情请点此了解。

    在.net2.0中(准确的说是vs 2005中),异步WebService异步调用的方式的例子:

void DoSomethingTest()
        {
            localhost.Service service = new WindowsApp.localhost.Service();
 
            service.HelloWorldCompleted += new WindowsApp.localhost.HelloWorldCompletedEventHandler(service_HelloWorldCompleted);
            // do Asyn calling here
            service.HelloWorldAsync();
        }
 
        void service_HelloWorldCompleted(object sender, WindowsApp.localhost.HelloWorldCompletedEventArgs e)
        {
            if (e.Error == )
            {
                MessageBox.Show(e.Result);
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
        }
服务器端代码


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    public Service () {
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
 }


        很简单,没有了AsyncCallback、IAsyncResult 这两个烦人的东西,调用的代码变得简洁、优雅了,而且可以从e.Result得到强类型的返回值(上例为"Hello World")。但是,有兴趣的话,可以看看vs 2005生成的Referance.cs文件,那可比2003中的复杂很多。其中可以看到System.ComponentModel.AsyncCompletedEventArgs 、 System.Threading.SendOrPostCallback(delegate)这两个在 .net 1.x 中没有的“怪物”,估计用到的地方还不止WebService客户端。有时间再研究看看。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!