Web services status management
By default, web services under. NET are stateless. However, you can use the status management provided by ASP. NET.
That is, SESSION and APPLICATION. This makes it easy to manage the status of web services. You only need to use the ENABLESESSION sub-attribute of the WEBMETHOD attribute to [WEBMETHOD (ENABLESESSION = TRUE)].
The server code is as follows:
[WebMethod (EnableSession = true)]
Public client GetClientState ()
{
Cstate = (client) Session ["clientstate"];
If (cstate = null)
{
Cstate = new client ();
Session ["clientstate"] = cstate;
}
Return cstate;
}
[WebMethod (EnableSession = true)]
Public void click ()
{
Client c = this. GetClientState ();
C. requsest ++;
}
}
Public class client
{
Public int requsest;
}
Create a CLIENT class to indicate the number of CLIENT connections. The GetClientState () method is used to return the status of the current user.
The client code is as follows:
Partial class Form1: Form
{
Webstate. Service ws;
Webstate. client c;
Public Form1 ()
{
InitializeComponent ();
Ws = new testwebstate. webstate. Service ();
Ws. CookieContainer = new System. Net. CookieContainer ();
// Set the COOKIE container so that the proxy object can correctly use cookies to provide status information
}
Private void button#click (object sender, EventArgs e)
{
Ws. click (); // each time a user clicks, the REQUEST of the current user's client state is ++
C = ws. GetClientState (); // obtain the status information.
MessageBox. Show ("you have click" + c. requsest + "times ");
}
}
The preceding section describes the status management of SEESION. The following section describes the APPLICATION.
The server code is as follows:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.