WebService异步调用(与委托有关)

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace WindowsFormsApplication1
  10. {
  11.       public partial class Form1 : Form
  12.       {
  13.           public Form1()
  14.           {
  15.               InitializeComponent();
  16.           }
  17.           private void button1_Click(object sender, EventArgs e)
  18.           {
  19.               localhost.Service t = new WindowsFormsApplication1.localhost.Service();
  20.               MessageBox.Show(t.HelloWorld());
  21.               MessageBox.Show("Down!");
  22.           }
  23.           private void button2_Click(object sender, EventArgs e)
  24.           {
  25.               localhost.Service t = new WindowsFormsApplication1.localhost.Service();
  26.               t.HelloWorldCompleted += new WindowsFormsApplication1.localhost.HelloWorldCompletedEventHandler(t_HellowordCompleted);
  27.               t.HelloWorldAsync();
  28.               MessageBox.Show("Down!");
  29.           }
  30.           private void t_HellowordCompleted(object sender, localhost.HelloWorldCompletedEventArgs e)
  31.           {
  32.               MessageBox.Show(e.Result.ToString());
  33.           }
  34.           private void button3_Click(object sender, EventArgs e)
  35.           {
  36.               MessageBox.Show((new localhost.Service()).GetTime());
  37.           }
  38.       }
  39. }
  40. ---------------------webService----------------------------
  41. using System;
  42. using System.Linq;
  43. using System.Web;
  44. using System.Web.Services;
  45. using System.Web.Services.Protocols;
  46. using System.Xml.Linq;
  47. using System.Threading;
  48. [WebService(Namespace = "http://tempuri.org/")]
  49. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  50. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
  51. // [System.Web.Script.Services.ScriptService]
  52. public class Service : System.Web.Services.WebService
  53. {
  54.      public Service () {
  55.          //Uncomment the following line if using designed components 
  56.          //InitializeComponent(); 
  57.      }
  58.      [WebMethod]
  59.      public string HelloWorld() {
  60.          Thread.Sleep(3000);
  61.          return "Hello World";
  62.      }
  63.     缓存10秒
  64.      [WebMethod(false,System.EnterpriseServices.TransactionOption.NotSupported,10)]
  65.      public string GetTime()
  66.      {
  67.          return DateTime.Now.ToString();
  68.      }
  69.     
  70. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值