注:

在ashx文件中使用Session

首先添加引用

using System.Web.SessionState;

实现接口

public class XXXX: IHttpHandler
==>
public class XXXX: IHttpHandler, IRequiresSessionState

使用的时候需要通过HttpContext对象调用,如

public void ProcessRequest (HttpContext hc) {
string code = hc.Session["Cold"].ToString();
}

为了实现负载均衡,Session的共享是必须要面对的事情

同一个用户,先后发出的多次请求很可能不是同一台服务器处理的, 那么默认的进程中 Session存储方式 在这多次的处理中就无法共享Session。

解决这个问题,要有两个事情要面对:

  • Session在服务器端是以字典的形式存储的,而获取Session的键就是Cookie(ASP.NET_SessionId),每个服务器都会创建一个SessionId来与客户端的保持联系,所以如何让多个服务器使用同一个SessionId是一个问题。
  • 原来Session是存到每个服务器的对应进程中的,现在要共享Session,就需要在统一的地点存储这些内容。

解决方案:

第一个问题,处理方法就是每个服务器都设置Cookie(ASP.NET_SessionId)的Domain,这样多个服务器就可以使用同一个SessionId了。

注:请求到达后台,如果没有发现同域下的SessionId,则会重新生成一个SessionId,并覆盖到客户端的Cookies中。

using System;
using System.Configuration;
using System.Reflection;
using System.Web;
using System.Web.SessionState; namespace Entity
{
public class SessionProviderHttpModule : IHttpModule
{
private string m_RootDomain = string.Empty; #region IHttpModule Members public void Dispose()
{ } public void Init(HttpApplication context)
{
m_RootDomain = ConfigurationManager.AppSettings["RootDomain"]; Type stateServerSessionProvider = typeof(HttpSessionState).Assembly.GetType("System.Web.SessionState.OutOfProcSessionStateStore");
FieldInfo uriField = stateServerSessionProvider.GetField("s_uribase", BindingFlags.Static | BindingFlags.NonPublic); if (uriField == null)
throw new ArgumentException("UriField was not found"); uriField.SetValue(null, m_RootDomain); context.EndRequest += new System.EventHandler(context_EndRequest);
} void context_EndRequest(object sender, System.EventArgs e)
{
HttpApplication app = sender as HttpApplication;
for (int i = ; i < app.Context.Response.Cookies.Count; i++)
{
if (app.Context.Response.Cookies[i].Name == "ASP.NET_SessionId")
{
app.Context.Response.Cookies[i].Domain = m_RootDomain;
}
}
} #endregion
}
}

然后还要将这个httpModule注入到项目中,即在<system.webServer>或<system.web>的modules中添加一个节点。

  <system.webServer>
<modules>
<add name="CrossDomainCookieModule" type="Entity.SessionProviderHttpModule,Entity" />
</modules>
</system.webServer>

第二个问题的解决方法有多种,如状态服务器、SQL Server、分布式缓存(Memcache)。

具体细节参考另一篇文章 ASP.NET Session的保存


原文:http://www.cnblogs.com/lori/p/3723714.html

ASP.NET Session的共享的更多相关文章

  1. asp.net session分布式共享解决方案

    Session共享是分布式系统设计时必须考虑的一个重要的点.相比较java中的session共享解决方案,.net中的解决方案还是比较少,MemcachedSessionProvider类库是比较优秀 ...

  2. ASP.NET Session 详解

    阅读本文章之前的准备 阅读本文章前,需要读者对以下知识有所了解.否则,阅读过程中会在相应的内容上遇到不同程度的问题. 懂得ASP/ASP.NET编程 了解ASP/ASP.NET的Session模型 了 ...

  3. [转]ASP.NET Session 详解

    来源:http://www.webshu.net/jiaocheng/programme/ASPNET/200606/1381.html 阅读本文章之前的准备 阅读本文章前,需要读者对以下知识有所了解 ...

  4. Session分布式共享 = Session + Redis + Nginx

    一.Session 1.Session 介绍 我相信,搞Web开发的对Session一定再熟悉不过了,所以我就简单的介绍一下. Session:在计算机中,尤其是在网络应用中,称为"会话控制 ...

  5. 详解Session分布式共享(.NET CORE版)

    一.前言&回顾 在上篇文章Session分布式共享 = Session + Redis + Nginx中,好多同学留言问了我好多问题,其中印象深刻的有:nginx挂了怎么办?采用Redis的S ...

  6. 什么是Session分布式共享

    在了解session分布式共享之前先来了解Session.Redis和Nginx的相关知识. 一.Session相关知识 1.Session 介绍 Session在网络应用中,称为“会话控制”. 每个 ...

  7. Session分布式共享 = Session + Redis + Nginx(转)

    出处:http://www.cnblogs.com/newP/p/6518918.html 一.Session 1.Session 介绍 我相信,搞Web开发的对Session一定再熟悉不过了,所以我 ...

  8. ASP.NET Session原理及处理方法

    session是怎么存储,提取的 1.在服务器端有一个session池,用来存储每个用户提交session中的数据,Session对于每一个客户端(或者说浏览器实例)是“人手一份”,用户首次与Web服 ...

  9. ASP.net session丢失

    ASP.NET Session的实现: asp.net的Session是基于HttpModule技术做的,HttpModule可以在请求被处理之前,对请求进行状态控制,由于Session本身就是用来做 ...

随机推荐

  1. Android 学习笔记之AndBase框架学习(五) 数据库ORM..注解,数据库对象映射...

    PS:好久没写博客了... 学习内容: 1.DAO介绍,通用DAO的简单调度过程.. 2.数据库映射关系... 3.使用泛型+反射+注解封装通用DAO.. 4.使用AndBase框架实现对DAO的调用 ...

  2. IOS7中自动计算label的宽度和高度的方法

    #import "ViewController.h" @implementation ViewController - (void)viewDidLoad { [super vie ...

  3. 控制网页的Panel是否显示

    在网页上有十二个Panel控件,默认状态是不显示的,根据当前月作为条件去控制对应的Panel控件显示. Insus.NET以下使用三种方法来实现它,先是第一种,使用FindControl方法 第二种方 ...

  4. C# LLSQL快速查询框架

    介绍一种新类型查询方法,类似linq,lambda语法,类似标准的sql使用习惯,支持匿名类型,泛型,目前支持mssql,mysql, 切换只需要DatabaseConfig.DatabaseType ...

  5. 不可或缺 Windows Native (5) - C 语言: 数组

    [源码下载] 不可或缺 Windows Native (5) - C 语言: 数组 作者:webabcd 介绍不可或缺 Windows Native 之 C 语言 数组 示例cArray.h #ifn ...

  6. kFreeBsd 国内开源镜像站汇总

    从http://bbs.chinaunix.net/archiver/tid-3756178.html这里抽取了debian源中支撑kfreebsd架构的源. 中科大: http://debian.u ...

  7. Protocol Buffers动态消息解析

    http://www.searchtb.com/2012/09/protocol-buffers.html http://www.cnblogs.com/jacksu-tencent/p/344731 ...

  8. MyBatis中的特殊符号[20160713]

    今天中午回到工位已经是12:20多了,没有时间睡觉了,本想着还能提前开始,结果看了点新闻之后,又是12:40了,所以新闻坚决不能看,执行力. 今天主要记录一下MyBatis中的特殊符号的问题,这个问题 ...

  9. jQuery中的事件冒泡

    1.什么是冒泡 eg: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <he ...

  10. 解决客户 IE 浏览器"兼容性视图"设置带来的问题

    最近在给客户开发一个 ASP.NET web 报表时,发现客户的 IE8 浏览器上,看网页总是怪怪的. 调查后发现,客户的 IE8 浏览器,统一被设置成"对本地网络使用兼容性视图" ...