我有一个.Net with服务,有2种方法:
[WebMethod(EnableSession = true)]
public void A()
{
HttpSessionState session = Session;
Thread thread = new Thread(B);
thread.Start();
}
[WebMethod(EnableSession = true)]
public void B()
{
HttpSessionState session = Session;
}场景1)当我直接调用B方法时,会话不是null
场景2),但是当我调用A时,在B中,会话和HttpContext.Current都是空的。
为什么?如何在第二个场景中启用B中的会话?我如何访问A中的会话?我应该把它的会话传递给B吗?如果是,怎么做?
方法B不应将会话作为参数。
谢谢,
发布于 2012-07-14 05:04:00
我必须使用一个全局域:
/// <summary>
/// Holds the current session for using in threads.
/// </summary>
private HttpSessionState CurrentSession;
[WebMethod(EnableSession = true)]
public void A()
{
CurrentSession = Session;
Thread thread = new Thread(B);
thread.Start();
}
[WebMethod(EnableSession = true)]
public void B()
{
//for times that method is not called as a thread
CurrentSession = CurrentSession == null ? Session : CurrentSession;
HttpSessionState session = CurrentSession;
}发布于 2012-07-11 12:11:44
这是因为你要用新的线程启动B。
见http://forums.asp.net/t/1276840.aspx或http://forums.asp.net/t/1630651.aspx/1
发布于 2012-07-14 11:02:38
[WebMethod(EnableSession = true)]
public void A()
{
HttpSessionState session = Session;
Action action = () => B_Core(session);
Thread thread = new Thread(action);
thread.Start();
}
[WebMethod(EnableSession = true)]
public void B()
{
HttpSessionState session = Session;
B_Core(session);
}
private void B_Core(HttpSessionState session)
{
// todo
}https://stackoverflow.com/questions/11432267
复制相似问题