博客园  :: 首页  :: 新随笔  :: 联系 :: 管理

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

Posted on 2008-10-19 17:23  codingsilence  阅读(209)  评论(0编辑  收藏  举报
  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. }