高性能 web service 笔记

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
//using System.Net;   //winform 引用cookie
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            localhost.Service t = new WindowsApplication1.localhost.Service();

            MessageBox.Show(t.HelloWorld());

            MessageBox.Show("done");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            localhost.Service t = new WindowsApplication1.localhost.Service();
 
            t.HelloWorldCompleted += new WindowsApplication1.localhost.HelloWorldCompletedEventHandler(t_HelloWorldCompleted);
            t.HelloWorldAsync();
            MessageBox.Show("done");
            //MessageBox.Show(t.HelloWorld());
    
        }
        //异步调用
        private void t_HelloWorldCompleted(object sender, localhost.HelloWorldCompletedEventArgs e)
        {
            MessageBox.Show(e.Result.ToString());
            //MessageBox.Show("it is oK!");
        }

        //数据缓存 10秒
        private void button3_Click(object sender, EventArgs e)
        {
            localhost.Service t = new WindowsApplication1.localhost.Service();
            MessageBox.Show(t.getTime());
        }

        //没有这句话,在windows 调用时 变量不能++的,因为没有cookie 保存,但浏览web  service 时,变量可以自动增加,因为那是通过IE的Cookie 保存的值。
        CookieContainer cc = new CookieContainer();

        //会话 (在需要时才使用会话)
        private void button4_Click(object sender, EventArgs e)
        {
            localhost.Service t = new WindowsApplication1.localhost.Service();
            t.CookieContainer = cc;
            MessageBox.Show(t.Count().ToString());
        }
    }
}



//================  service 端

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Threading;
using System.EnterpriseServices;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

public class Service : System.Web.Services.WebService
{
    public Service () {

        //如果使用设计的组件,请取消注释以下行
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld() {
        Thread.Sleep(3000);
        return "Hello World";
    }
    [WebMethod(false,TransactionOption.NotSupported,10)]
    public string getTime()
    {
        return DateTime.Now.ToString();
    }

    [WebMethod(true)]
    public int Count()
    {
        int temp = (int)Session["counter"];
        temp++;
        Session["counter"] = temp;
        return temp;
    }
}

 

posted on 2007-09-20 17:50  willlove  阅读(235)  评论(0)    收藏  举报

导航