How to Use session in Asp.net WebService

Source: Internet
Author: User

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

Related Article

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.