在asp.net webservice中如何使用session

简介: 原文:在asp.net webservice中如何使用session原文:刘武|在asp.net webservice中如何使用session   在使用asp.net编写webservice时,默认情况下是不支持session的,但我们可以把WebMethod的EnableSession...
原文: 在asp.net webservice中如何使用session

原文:刘武|在asp.net webservice中如何使用session  

在使用asp.net编写webservice时,默认情况下是不支持session的,但我们可以把WebMethod的EnableSession选项设为true来显式的打开它,请看以下例子:

1 新建网站WebSite 

2 新建web服务WebService.asmx,它具有以下两个方法:

[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 添加asp.net页面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();
}

问题似乎到此结束了,按Login按钮记录用户名以后,再按GetName就可以获取到刚才输入的名字。

但如果我们另外新建一个website,并添加web引用来调用刚才编写的webservice,问题就出来了,GeName方法并没有获取到我们刚才登录的用户名(如果是在winform中调用该方法,也会出现同样的问题)。莫非这个方法行不通了?

其实不然,我们给该WebService的CookieContainer赋值就可以了,修改SessionInWebservice.aspx.cs 的代码:

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();
}

请注意:Login方法和GetName方法必须指定同一个CookieContainer,因此在这里我们使用了静态变量。

但如果是在不同的页面中调用该webservice,问题依旧存在,因此我们需要重新修改代码,通过编写新类继承上面的webservice,并给CookieContainer赋值就可以解决该问题了: 

public class WebService1:localhost.WebService
{
    private static System.Net.CookieContainer cookieContainer;


    static WebService1()
    {
        cookieContainer = new System.Net.CookieContainer();
    }


    public WebService1()
    {
        this.CookieContainer = cookieContainer;
    }
}

调用的时候也不需要重新给CookieContainer赋值了:

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();
}
目录
相关文章
|
3月前
|
存储 开发框架 NoSQL
ASP.NET WEB——项目中Cookie与Session的用法
ASP.NET WEB——项目中Cookie与Session的用法
39 0
|
4月前
|
开发框架 .NET
ASP.NET Core NET7 增加session的方法
ASP.NET Core NET7 增加session的方法
38 0
|
开发框架 JavaScript .NET
Asp.Net Core中Session使用
Asp.Net Core中Session使用
117 0
|
缓存 前端开发 API
.Net WebApi中使用Session使用
.Net WebApi中使用Session使用
253 0
|
存储 开发框架 .NET
ASP.NET中利用Application和Session统计在线人数、历史访问量
先来简单说一下ASP.NET中的Application和Session 下图是我们非常熟悉的Web应用程序的结构:
ASP.NET中利用Application和Session统计在线人数、历史访问量
|
存储 中间件 .NET
ASP.NET Core Web Api之JWT VS Session VS Cookie(二)
ASP.NET Core Web Api之JWT VS Session VS Cookie(二)前言本文我们来探讨下JWT VS Session的问题,这个问题本没有过多的去思考,看到评论讨论太激烈,就花了一点时间去研究和总结,顺便说一句,这就是写博客的好处,一篇博客写出有的可能是经验积累,有的可能是学习分享,但都逃不过看到文章的你有更多或更好的想法,往返交流自身能收获更多,何乐而不为呢?希望本文能解惑或者能得到更多的交流。
2865 0
|
.NET 容器 开发框架
ASP.NET Core 2 学习笔记(十一)Cookies & Session
原文:ASP.NET Core 2 学习笔记(十一)Cookies & Session 基本上HTTP是没有记录状态的协定,但可以通过Cookies将Request来源区分出来,并将部分数据暂存于Cookies及Session,是写网站常用的用户数据暂存方式。
1330 0
|
4月前
|
开发框架 前端开发 .NET
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
ASP.NET CORE 3.1 MVC“指定的网络名不再可用\企图在不存在的网络连接上进行操作”的问题解决过程
46 0
|
12天前
|
开发框架 前端开发 JavaScript
JavaScript云LIS系统源码ASP.NET CORE 3.1 MVC + SQLserver + Redis医院实验室信息系统源码 医院云LIS系统源码
实验室信息系统(Laboratory Information System,缩写LIS)是一类用来处理实验室过程信息的软件,云LIS系统围绕临床,云LIS系统将与云HIS系统建立起高度的业务整合,以体现“以病人为中心”的设计理念,优化就诊流程,方便患者就医。
19 0