Web服务下的异步调用
来源:互联网 发布:淘宝无线流量互刷软件 编辑:程序博客网 时间:2024/06/05 03:59
通过异步调用机制,可以提高程序的运行效率,同时,给用户良好的界面体验。
.net2.0下提供了很好的机制来支持Web服务的异步调用。
下面通过一个例子来说明Web服务下怎样实现异步调用。
首先先建立一个Web服务项目
public class MyService : System.Web.Services.WebService
...{
public MyService () ...{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() ...{
return "Hello World";
}
}
...{
public MyService () ...{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string HelloWorld() ...{
return "Hello World";
}
}
然后再建立一个控制台程序
添加Web Reference,会在Web Reference下生成一个localhost的Web Reference.
然后能过wsdl/namespace:MyService http://localhost/异步调用/service.asmx?wsdl生成MyService代理类,然后添加到控制台程序中。
using System;
using System.Collections.Generic;
using System.Text;
using MyService;
namespace AnsyDemo
...{
class Program
...{
static bool MyStatus = false;
static void Main(string[] args)
...{
System.Timers.Timer tr = new System.Timers.Timer();
tr.Interval = 1000;
tr.Enabled = true;
tr.Elapsed += new System.Timers.ElapsedEventHandler(OnTimeEven);
//localhost.MyService MySeriveObj = new AnsyDemo.localhost.MyService();
//Console.WriteLine("开始调用远程Web服务");
//开始异步调用远程服务
MyService.MyService MyServiceObj = new MyService.MyService();
Console.WriteLine("开始调用远程Web服务");
//开始异步调用远程服务
MyServiceObj.BeginHelloWorld(new AsyncCallback(MyHelloWord), MyServiceObj);
//int i = 0;
//while (Program.MyStatus != true)
//{
// i++;
// Console.WriteLine("等待Web服务调用结束....#" + i.ToString());
//}
//Console.WriteLine("远程调用结束");
int j = 0;
for (int i = 1; i < 100; i++)
...{
j += i;
}
Console.WriteLine(j.ToString());
Console.ReadLine();
}
static void MyHelloWord(IAsyncResult iar)
...{
MyService.MyService MyServices = (MyService.MyService)iar.AsyncState;
Console.WriteLine(MyServices.EndHelloWorld(iar));
//结束远程调用
Program.MyStatus = true;
}
private static void OnTimeEven(object source, System.Timers.ElapsedEventArgs e)
...{
if (MyStatus == true)
...{
Console.WriteLine("调用结束");
}
}
}
}
using System.Collections.Generic;
using System.Text;
using MyService;
namespace AnsyDemo
...{
class Program
...{
static bool MyStatus = false;
static void Main(string[] args)
...{
System.Timers.Timer tr = new System.Timers.Timer();
tr.Interval = 1000;
tr.Enabled = true;
tr.Elapsed += new System.Timers.ElapsedEventHandler(OnTimeEven);
//localhost.MyService MySeriveObj = new AnsyDemo.localhost.MyService();
//Console.WriteLine("开始调用远程Web服务");
//开始异步调用远程服务
MyService.MyService MyServiceObj = new MyService.MyService();
Console.WriteLine("开始调用远程Web服务");
//开始异步调用远程服务
MyServiceObj.BeginHelloWorld(new AsyncCallback(MyHelloWord), MyServiceObj);
//int i = 0;
//while (Program.MyStatus != true)
//{
// i++;
// Console.WriteLine("等待Web服务调用结束....#" + i.ToString());
//}
//Console.WriteLine("远程调用结束");
int j = 0;
for (int i = 1; i < 100; i++)
...{
j += i;
}
Console.WriteLine(j.ToString());
Console.ReadLine();
}
static void MyHelloWord(IAsyncResult iar)
...{
MyService.MyService MyServices = (MyService.MyService)iar.AsyncState;
Console.WriteLine(MyServices.EndHelloWorld(iar));
//结束远程调用
Program.MyStatus = true;
}
private static void OnTimeEven(object source, System.Timers.ElapsedEventArgs e)
...{
if (MyStatus == true)
...{
Console.WriteLine("调用结束");
}
}
}
}
在这段程序中,定义了一个定时器,在一个固定的时间,来测试异步调用是否完成。(当MyStatus=true时,异步调用完成)
MyService.MyService MyServiceObj = new MyService.MyService();
Console.WriteLine("开始调用远程Web服务");
//开始异步调用远程服务
MyServiceObj.BeginHelloWorld(new AsyncCallback(MyHelloWord), MyServiceObj);
Console.WriteLine("开始调用远程Web服务");
//开始异步调用远程服务
MyServiceObj.BeginHelloWorld(new AsyncCallback(MyHelloWord), MyServiceObj);
这里首先实例化Web服务,然后通过代理类myservice,来异步调用方法HelloWorld,AsyncCallback是一个委托,当异步调用完成时,它会自动调用MyHelloWrod方法。
我们通过MyHelloWrod方法,来调用EndHelloWrold方法,然后调用完成,设置MyStatus的状态为true,当计时器再次检测MyStatus时,为true,则异步调用完成。
static void MyHelloWord(IAsyncResult iar)
...{
MyService.MyService MyServices = (MyService.MyService)iar.AsyncState;
Console.WriteLine(MyServices.EndHelloWorld(iar));
//结束远程调用
Program.MyStatus = true;
}
...{
MyService.MyService MyServices = (MyService.MyService)iar.AsyncState;
Console.WriteLine(MyServices.EndHelloWorld(iar));
//结束远程调用
Program.MyStatus = true;
}
- Web服务下的异步调用
- 异步调用Web服务
- Ajax异步调用Web服务的例子
- Web服务的异步和同步调用
- Anthem 异步调用web服务
- 异步调用Web服务方法
- 开发自己的Web服务处理程序(以支持Ajax框架异步调用Web服务方法)
- Web服务异步调用实训
- XMLHttpRequest 对象异步调用web服务
- Web Service 的异步调用
- 有关AJAX异步调用web服务中带参数的方法的心得
- WCF:异步调用长时间工作的服务
- 调用异步服务时的超时处理
- web服务的调用案例
- 调用WEB服务的方法
- 协调异步服务调用
- tornado_5_异步web服务
- 异步调用Web Service的方式
- FLEX学习建议——dreamer
- asp.net中页面间传值
- 简单谈谈C#开发短信收发软件的原理
- 一篇很好的个人理财研究文章[zz]
- Web开放趋势代表:无所不在的Google API
- Web服务下的异步调用
- PL/SQL Number数字类型函数
- 右下角弹出(淡出)消息(js)
- PL/SQL 日期时间类型函数及运算
- 从第二章开始规范翻译C++ GUI Programming with Qt 4
- PB中数据窗口导出数据到EXCEL的程序(1)
- PL/SQL 类型格式转换
- 远程方法调用(RMI)机制
- PB中数据窗口导出数据到EXCEL的程序(2)