WEB Services State Management

Source: Internet
Author: User
Tags reference web services client
Services|web Web Services State Management

By default,. NET services are stateless. However, you can use ASP.net to provide state management
That's the session and the application. This makes it easy to state management under Web services by simply using the EnableSession child properties of the WebMethod property to [WebMethod (enablesession= TRUE)]

The service-side 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;
}


First, create a client class that represents the number of clients connected, and the Getclientstate () method is used to return the current user's state.
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 use cookies correctly to provide status information
}

private void Button1_Click (object sender, EventArgs e)
{
Ws.click ()//per click, the current user's client state request on + +
c = ws. Getclientstate ()//Get status information
MessageBox.Show ("You have click" + C.requsest + "Times");
}
}
The above is seesion state management, the following introduction application.
The service-side code is as follows:
[WebServiceBinding (conformanceclaims = wsiclaims.bp10, EmitConformanceClaims = True)]
public class Service:System.Web.Services.WebService
{
ArrayList CList;

[WebMethod]
public string Gethistory ()
{
StringBuilder Xbuider = new StringBuilder ();//To add a using SYSTEM. TEXT;
CList = (ArrayList) application["Client"];
if (clist = null)
{
CList = new ArrayList ();
application["Client"] = CList;
}
foreach (client C in CList)
{
Xbuider. Append (C.name + "" + C.request + "" + "\ r \ n");

}
Return Xbuider. ToString ();

}
[WebMethod]
public void Click (ref client C)//Here is a reference to the passing object
{
CList = (ArrayList) application["Client"];
if (clist = null)
{
CList = new ArrayList ();
application["Client"] = CList;
}
CList. ADD (c);

}

}
public class Client
{
public int request;
public string name;
}
The client code is as follows:
Partial class Form1:form
{

Webstate.client C;
Public Form1 ()
{
InitializeComponent ();
c = new Testwebstate2.webstate.client ();
C.name = "Jisiki";

}

private void Button1_Click (object sender, EventArgs e)
{

c.request++;
Webstate. Service s = new Testwebstate2.webstate.Service ();
S.click (ref c);//Here is a reference to the passing object
This.richTextBox1.Text = S.gethistory ();

}
}
For Web services, the Application property is always available, application returns the HttpApplicationState class
An instance of that can store an accessible "name/value" pair from any client.





Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.