Web Service 异步调用


//Author: stardicky                                                           //
//E-mail: stardicky@hotmail.com                                               //
//QQNumber: 9531511                                                           //
//CompanyName: Ezone International                                            //
//Class: HBS-0308                                                             //
//title: 异步调用Web服务                                                      //

//文件数量2个

///文件1

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace WebServiceDemo
{
 /// <summary>
 /// Service1 的摘要说明。
 /// </summary>
 [WebService(Namespace="http://www.ezone.com.cn")]
 public class EzoneService : System.Web.Services.WebService
 {
  public EzoneService()
  {
   //CODEGEN: 该调用是 ASP.NET Web 服务设计器所必需的
   InitializeComponent();
  }

  #region 组件设计器生成的代码
  
  //Web 服务设计器所必需的
  private IContainer components = null;
    
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {
  }

  /// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if(disposing && components != null)
   {
    components.Dispose();
   }
   base.Dispose(disposing);  
  }
  
  #endregion

  // WEB 服务示例
  // HelloWorld() 示例服务返回字符串 Hello World
  // 若要生成,请取消注释下列行,然后保存并生成项目
  // 若要测试此 Web 服务,请按 F5 键

  [WebMethod]
  public string HelloWorld()
  {
   return "Hello World";
  }
 }
}
///文件2
using System;
using System.Web.Services;
using System.Web;
using ConsoleApp.www.ezone.com.cn;

namespace ConsoleApp
{
 /// <summary>
 /// Class1 的摘要说明。
 /// </summary>
 class MyConsole
 {
  static bool MyStatus=false;

  /// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main(string[] args)
  {
   //
   // TODO: 在此处添加代码以启动应用程序
   //
   //创建Web服务对象
   EzoneService MyServiceObj=new EzoneService();

   Console.WriteLine("开始调用远程Web服务......");

   //开始异步调用远程服务
   MyServiceObj.BeginHelloWorld(new AsyncCallback(MyHelloWorld),MyServiceObj);

   int i=0;
   while(MyConsole.MyStatus!=true)
   {
    i++;
    Console.WriteLine("等待Web服务调用结束......#"+i.ToString());
   }

   Console.WriteLine("远程调用结束");

   
  }

  //该方法由操作系统自动调用
  static void MyHelloWorld(IAsyncResult iar)
  {
   EzoneService MyService=(EzoneService)iar.AsyncState;
   Console.WriteLine(MyService.EndHelloWorld(iar));
   //结束远程调用

   MyConsole.MyStatus=true;
  }
 }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值