您现在的位置: 无忧电子商务网 >> 信息学院 >> 程序开发 >> asp.net >> 正文

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

作者:佚名    信息学院来源:整理    点击数:    更新时间:2008-1-30 我要参与讨论

    在.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客户端。

在google里搜索更多体验.NET 2.0的优雅之异步Web服务调用

Google
Web www.51ec.org
【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
我来说两句 对此文章发表了评论
  昵 称: *必填    ·注册用户·
  评 分: 1分 2分 3分 4分 5分     严禁发表危害国家安全、政治、黄色淫秽等内容的评论,用户需对自己在使用本网站服务过程中的行为承担法律责任。本站管理员有权保留或删除评论内容,评论内容只代表机友个人观点,与本网站立场无关。  
评 论
内 容

 
评论列表 (最新 评论仅限网友观点!)

供求信息




| 设为首页 | 加入收藏 | 关于我们 | 广告服务 | 联系方式 | 友情链接 | 版权申明