Web Service学习笔记:状态管理

在默认情况下,.NET下的WEB SERVICES是无状态的.不过可以用ASP.NET提供的状态管理,那就是SESSION和APPLICATION.

这使得WEB SERVICES下状态管理变得简单了,只需使用WEBMETHOD属性的ENABLESESSION子属性就可[WEBMETHOD(ENABLESESSION=TRUE)]。

服务端代码如下:

 

        [WebMethod(EnableSession  =   true )]
        
public  client GetClientState()
ExpandedBlockStart.gifContractedBlock.gif        
{
            cstate 
= (client)Session["clientstate"];
            
if (cstate == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                cstate 
= new client();
                Session[
"clientstate"= cstate;
            }

            
return cstate;
        }

        [WebMethod(EnableSession 
=   true )]
        
public   void  click()
ExpandedBlockStart.gifContractedBlock.gif        
{
            client c 
= this.GetClientState();
            c.requsest
++;
        }

        
public   class  client
ExpandedBlockStart.gifContractedBlock.gif        
{
            
public int requsest;
        }

 

先创建一个CLIENT类,用于表示客户端连接的次数,GetClientState()方法用于返回当前用户的状态.

客户端代码如下:

 

 

CODE:
        
public   sealed   class  e1 : ApplicationException
ExpandedBlockStart.gifContractedBlock.gif        
{
            
public e1(string message)
                : 
base(message)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//描述该错误的信息,不做任何事,依赖于基类
                
// TODO: Add constructor logic here
                
//
            }

        }

        
public   sealed   class  e2 : ApplicationException
ExpandedBlockStart.gifContractedBlock.gif        
{
            
public e2(string message)
                : 
base(message)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//描述该错误的信息,不做任何事,依赖于基类
                
// TODO: Add constructor logic here
                
//
            }

        }

        
partial   class  Form1 : Form
ExpandedBlockStart.gifContractedBlock.gif        
{
            webstate.Service ws;
            webstate.client c;
            
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                InitializeComponent();
                ws 
= new testwebstate.webstate.Service();
                ws.CookieContainer 
= new System.Net.CookieContainer();
                
//设置COOKIE容器,以便代理对象能正确使用COOKIE来提供状态信息
            }

            
private void button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                ws.click();
//每点击一次,当前用户的CLIENT STATE的REQUEST就++
                c = ws.GetClientState();//获取状态信息
                MessageBox.Show("you have click" + c.requsest + "times");
            }

        }

        
// 以上的是SEESION的状态管理,下面介绍APPLICATION.服务端代码如下:
        [WebServiceBinding(ConformanceClaims  =  WsiClaims.BP10, EmitConformanceClaims  =   true )]
        
public   class  Service : System.Web.Services.WebService
ExpandedBlockStart.gifContractedBlock.gif        
{
            ArrayList clist;
            [WebMethod]
            
public string GetHistory()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                StringBuilder xbuider 
= new StringBuilder();//要添加USING SYSTEM.TEXT;
                clist = (ArrayList)Application["client"];
                
if (clist == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    clist 
= new ArrayList();
                    Application[
"client"= clist;
                }

                
foreach (client c in clist)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    xbuider.Append(c.name 
+ "" + c.request + "" + "\r\n");
                }

                
return xbuider.ToString();
            }

            [WebMethod]
            
public void click(ref client c)//这里用的是传递对象的引用
ExpandedSubBlockStart.gifContractedSubBlock.gif
            {
                clist 
= (ArrayList)Application["client"];
                
if (clist == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    clist 
= new ArrayList();
                    Application[
"client"= clist;
                }

                clist.Add(c);
            }

        }

        
public   class  client
ExpandedBlockStart.gifContractedBlock.gif        
{
            
public int request;
            
public string name;
        }

 

 

客户端代码如下:

 

         partial   class  Form1 : Form
ExpandedBlockStart.gifContractedBlock.gif        
{
            webstate.client c;
            
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                InitializeComponent();
                c 
= new testwebstate2.webstate.client();
                c.name 
= "jisiki";
            }

            
private void button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                c.request
++;
                webstate.Service s 
= new testwebstate2.webstate.Service();
                s.click(
ref c);//这里用的是传递对象的引用
                this.richTextBox1.Text = s.GetHistory();
            }

        }


 

对于WEB SERVICES而言,APPLICATION属性总是可用的,APPLICATION返回HTTPAPPLICATIONSTATE类
的一个实例,它能存储来自任何客户端的可访问的"名称/值"对.

转载于:https://www.cnblogs.com/VisualStudio/archive/2008/10/17/1313662.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值