客户端使用web service异步调用
首先客户端必须通过UUDI找到web service服务,通过DISCO来寻找服务的位置URL,再通过WSDL产生代理,通过它来异步调用完成服务的引用。
启动VS自带的命令输入程序,输入C:\> WSDL http://localhost/WebService1/Service1.asmx
其中 c:\> 为文件下载到本地的C盘下。其文件名为自动以服务的类名为自己的名称,如服务类名为Service1
产生的代理类自动为服务的方法加两个方法,此两个方法正为异步所用。比如服务方法为HelloWord();
产生两个BeginHelloWord()和EndHelloWord()。
把该文件添加到你的程序中。
服务的内容为
首先客户端必须通过UUDI找到web service服务,通过DISCO来寻找服务的位置URL,再通过WSDL产生代理,通过它来异步调用完成服务的引用。
启动VS自带的命令输入程序,输入C:\> WSDL http://localhost/WebService1/Service1.asmx
其中 c:\> 为文件下载到本地的C盘下。其文件名为自动以服务的类名为自己的名称,如服务类名为Service1
产生的代理类自动为服务的方法加两个方法,此两个方法正为异步所用。比如服务方法为HelloWord();
产生两个BeginHelloWord()和EndHelloWord()。
把该文件添加到你的程序中。
服务的内容为
public class Service1 : System.Web.Services.WebService
{
public Service1()
{
InitializeComponent();
}
[WebMethod]
public string HelloWorld(string name)
{
return "Hello World:"+name;
}
}
客户端调用为:{
public Service1()
{
InitializeComponent();
}
[WebMethod]
public string HelloWorld(string name)
{
return "Hello World:"+name;
}
}
//注意该Service1为WSDL生成的代理类,而不能使用localhost.Service1 sr=new localhost.Service();
Service1 service=new Service1();
//小明为传递参数
System.IAsyncResult ar=service.BeginHelloWorld("小明",null,null);
ar.AsyncWaitHandle.WaitOne();
if(ar.IsCompleted)
{
Console.WriteLine(service.EndHelloWorld(ar));
}
Console.WriteLine();
Service1 service=new Service1();
//小明为传递参数
System.IAsyncResult ar=service.BeginHelloWorld("小明",null,null);
ar.AsyncWaitHandle.WaitOne();
if(ar.IsCompleted)
{
Console.WriteLine(service.EndHelloWorld(ar));
}
Console.WriteLine();
浙公网安备 33010602011771号