When you use asp.net to compile webservice, session is not supported by default. However, you can set the EnableSession option of WebMethod to true to enable it explicitly. See the following example:
1. Create a WebSite
2. Create a web Service WebService. asmx. It has the following two methods:
C #-Code:
[WebMethod (EnableSession = true)] public string Login (string name) {Context. session ["name"] = name; return name;} [WebMethod (EnableSession = true)] public string GetName () {if (Context. session ["name"]! = Null) return Context. Session ["name"]. ToString (); else return "";}
3 add asp.net page SessionInWebservice. aspx
ASP. NET-Code:
<Form id = "form1" runat = "server"> <div> <asp: TextBox ID = "txtName" runat = "server"> </asp: TextBox> <asp: button ID = "btnLogin" runat = "server" Text = "Login" OnClick = "btnLogin_Click"/> </div> <asp: button ID = "btnGetName" runat = "server" Text = "GetName" OnClick = "btnGetName_Click"/> <asp: label ID = "lblName" runat = "server" Text = "Label"> </asp: Label> </div> </form>
SessionInWebservice. aspx. cs
C #-Code:
Protected void btnLogin_Click (object sender, EventArgs e) {WebService ws = new WebService (); ws. login (txtName. text);} protected void btnGetName_Click (object sender, EventArgs e) {WebService ws = new WebService (); lblName. text = ws. getName ();}
The problem seems to have ended. After logging the user name by pressing the Login button, you can press GetName to get the name you just entered.
However, if we create another website and add a web reference to call the webservice we just compiled, the problem arises, the GeName method does not get the username we just logged on to (if you call this method in winform, the same problem will occur ). Is this method not feasible?
Otherwise, we can assign a value to CookieContainer of the WebService. Modify the SessionInWebservice. aspx. cs code:
C #-Code:
Private static System. net. cookieContainer cookieContainer = new System. net. cookieContainer (); protected void btnLogin_Click (object sender, EventArgs e) {localhost. webService ws = new localhost. webService (); ws. cookieContainer = cookieContainer; ws. login (txtName. text);} protected void btnGetName_Click (object sender, EventArgs e) {localhost. webService ws = new localhost. webService (); ws. cookieContainer = cookieContainer; lblName. text = ws. getName ();}
Note: The Login and GetName methods must specify the same CookieContainer. Therefore, static variables are used here.
However, if the webservice is called on different pages, the problem persists. Therefore, we need to modify the code again and inherit the webservice by writing a new class, assign a value to CookieContainer to solve the problem:
C #-Code:
Public class WebService1: localhost. webService {private static System. net. cookieContainer cookieContainer; static WebService1 () {cookieContainer = new System. net. cookieContainer ();} public WebService1 () {this. cookieContainer = cookieContainer ;}}
You do not need to assign a new value to CookieContainer during the call:
C #-Code:
Protected void btnLogin_Click (object sender, EventArgs e) {WebService1 ws = new WebService1 (); ws. login (txtName. text);} protected void btnGetName_Click (object sender, EventArgs e) {WebService1 ws = new WebService1 (); lblName. text = ws. getName ();}