WebService的Sesinon保存利用的是Cookie
例子:
ClientForm:
WebService:
例子:
ClientForm:
1
public partial
class Form1 : Form
2 {
3 CookieContainer mycookie = new CookieContainer();
4 showna.Service myService = new showna.Service();
5
6 public Form1()
7 {
8 InitializeComponent();
9 }
10
11 private void button1_Click(object sender, EventArgs e)
12 {
13 myService.CookieContainer = mycookie;//必须要有一个CookieContainer保存Seesion
14 textBox1.Text = myService.SessionHitCounter().ToString();
15 }
16 }
2 {
3 CookieContainer mycookie = new CookieContainer();
4 showna.Service myService = new showna.Service();
5
6 public Form1()
7 {
8 InitializeComponent();
9 }
10
11 private void button1_Click(object sender, EventArgs e)
12 {
13 myService.CookieContainer = mycookie;//必须要有一个CookieContainer保存Seesion
14 textBox1.Text = myService.SessionHitCounter().ToString();
15 }
16 }
WebService:
[ WebMethod(Description="Per session Hit Counter",EnableSession=
true)]
//
EnableSession必须为True
public int SessionHitCounter() {
if (Session["HitCounter"] == null) {
Session["HitCounter"] = 1;
}
else {
Session["HitCounter"] = ((int) Session["HitCounter"]) + 1;
}
return ((int) Session["HitCounter"]);
}
public int SessionHitCounter() {
if (Session["HitCounter"] == null) {
Session["HitCounter"] = 1;
}
else {
Session["HitCounter"] = ((int) Session["HitCounter"]) + 1;
}
return ((int) Session["HitCounter"]);
}
本文转自94cool博客园博客,原文链接:http://www.cnblogs.com/94cool/archive/2010/03/01/1675782.html,如需转载请自行联系原作者