在默认情况下,.NET下的WEB SERVICES是无状态的.不过可以用ASP.NET提供的状态管理,那就是SESSION和APPLICATION.
这使得WEB SERVICES下状态管理变得简单了,只需使用WEBMETHOD属性的ENABLESESSION子属性就可[WEBMETHOD(ENABLESESSION=TRUE)]。
服务端代码如下:
[WebMethod(EnableSession
=
true
)]
public
client GetClientState()
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
cstate = (client)Session["clientstate"];
if (cstate == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
cstate = new client();
Session["clientstate"] = cstate;
}
return cstate;
}
[WebMethod(EnableSession
=
true
)]
public
void
click()
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
client c = this.GetClientState();
c.requsest++;
}
public
class
client
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public int requsest;
}
先创建一个CLIENT类,用于表示客户端连接的次数,GetClientState()方法用于返回当前用户的状态.
客户端代码如下:
CODE:
public
sealed
class
e1 : ApplicationException
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public e1(string message)
: base(message)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//描述该错误的信息,不做任何事,依赖于基类
// TODO: Add constructor logic here
//
}
}
public
sealed
class
e2 : ApplicationException
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public e2(string message)
: base(message)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//描述该错误的信息,不做任何事,依赖于基类
// TODO: Add constructor logic here
//
}
}
partial
class
Form1 : Form
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
webstate.Service ws;
webstate.client c;
public Form1()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
ws = new testwebstate.webstate.Service();
ws.CookieContainer = new System.Net.CookieContainer();
//设置COOKIE容器,以便代理对象能正确使用COOKIE来提供状态信息
}
private void button1_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.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.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
ArrayList clist;
[WebMethod]
public string GetHistory()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
StringBuilder xbuider = new StringBuilder();//要添加USING SYSTEM.TEXT;
clist = (ArrayList)Application["client"];
if (clist == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
clist = new ArrayList();
Application["client"] = clist;
}
foreach (client c in clist)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xbuider.Append(c.name + "" + c.request + "" + "\r\n");
}
return xbuider.ToString();
}
[WebMethod]
public void click(ref client c)//这里用的是传递对象的引用
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
clist = (ArrayList)Application["client"];
if (clist == null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
clist = new ArrayList();
Application["client"] = clist;
}
clist.Add(c);
}
}
public
class
client
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
public int request;
public string name;
}
客户端代码如下:
partial
class
Form1 : Form
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
webstate.client c;
public Form1()
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
InitializeComponent();
c = new testwebstate2.webstate.client();
c.name = "jisiki";
}
private void button1_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
c.request++;
webstate.Service s = new testwebstate2.webstate.Service();
s.click(ref c);//这里用的是传递对象的引用
this.richTextBox1.Text = s.GetHistory();
}
}
对于WEB SERVICES而言,APPLICATION属性总是可用的,APPLICATION返回HTTPAPPLICATIONSTATE类
的一个实例,它能存储来自任何客户端的可访问的"名称/值"对.