【发布时间】:2012-10-24 07:51:37
【问题描述】:
环境:IIS 7.5、.Net 4.0
我在多服务器环境中读取 HttpContext.Current.Session 时遇到问题。
首先在我的代码中,我将一个对象存储在 HttpContext.Current.Session 中,然后尝试再次读取它。读取(执行多次)随机失败,我怀疑它与调用命中的服务器有关。对象的存储和读取是通过 ajax 调用完成的,一位同事告诉我将对象存储在 Page_Load 中。我相当怀疑,事实证明,使用这种方法并没有解决问题。
存储:
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public Stream GetHierarchy(string invId, string zoomLevel)
{
Hierarchy hierarchy = new Hierarchy();
try
{
hierarchy = businessLogic.GetHierarchy(invId);
HttpContext.Current.Session["hierarchy"] = hierarchy;
}
catch (Exception ex)
{
throw ex;
}
return new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(hierarchy)));
}
阅读:
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public Stream GetCustomer(string invId, string includeDetails, string zoomLevel)
{
Hierarchy hierarchy = (Hierarchy)HttpContext.Current.Session["hierarchy"];
Customers customers = null;
Customer customer = null;
if (hierarchy != null) {
customers = hierarchy.Customers;
if (customers != null)
{
try
{
customer = (from e in customers.DiagramCustomers where e.InvId == invId select e).ToList()[0];
}
catch (Exception ex)
{
}
}
}
return new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(customer)));
}
在单个服务器环境中一切正常...
谁能解释一下我在这里面临什么样的问题?最好是如何解决它:-)
./CJ
【问题讨论】:
-
您是否可以选择将所需的值存储在所有服务器都可以访问的数据库中?
-
您可能需要使用某种内存缓存,然后多台机器才能访问。
-
@awright18:但是拥有一个会话仅在其中一台服务器上可用的服务器集群有意义吗?
-
您通常会与一个逻辑数据库端点进行交互。即使它被复制了,你仍然会击中一个单位。
-
事实证明,后台终于告诉我,我部署到的服务器集群没有会话数据库(甚至不知道这是可能的),我必须部署到不同的环境。希望这能解决我的问题。不然我就哭着回来……