环境: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