When using Asp.net to write WebService, session is not supported by default, but we canEnablesessionSet the option 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:
[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
<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>
<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
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 the cookiecontainer of the WebService.Sessioninwebservice. aspx. CSCode:
Private Static system. net. 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: LoginMethod andGetnameThe method must specify the same cookiecontainer, so we use static variables 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:
Public class webservice1: localhost. WebService
{
Private Static system. net. 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:
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 ();
}
Original article: http://www.liuwu.me/post/use-session-state-in-aspnet-webservice.aspx