通过异步调用机制,可以提高程序的运行效率,同时,给用户良好的界面体验。
.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;
}