当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  体验.NET 2.0的优雅之异步Web服务调用

 体验.NET 2.0的优雅之异步Web服务调用

点击次数:24 次 发布日期:2008-11-26 12:26:44 作者:源代码网
源代码网推荐      在.net1.x中,异步WebService异步调用的一般方式为调用方法XX对应的BeginXX方法来完成,其过程类似于异步委托的使用。
源代码网推荐  
源代码网推荐    在.net2.0中(准确的说是vs.net 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 == null)
源代码网推荐   {
源代码网推荐    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.net2005生成的Referance.cs文件,那可比2003中的复杂很多。其中可以看到System.ComponentModel.AsyncCompletedEventArgs 、 System.Threading.SendOrPostCallback(delegate)这两个在 .net 1.x 中没有的“怪物”,估计用到的地方还不止WebService客户端。
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华