ASP.NET + C#HttpContext.Current.Session为null(Inside WebService)

Sex*_*yMF 12 c# asp.net session null

这是我发起会议的方式

 protected void Session_Start(object sender, EventArgs e)
    {
        HttpContext.Current.Session["CustomSessionId"] = Guid.NewGuid();
    }
Run Code Online (Sandbox Code Playgroud)

在我的类库中的解决方案中,我正在访问它并获得null异常:

string sess = HttpContext.Current.Session["CustomSessionId"] ;
Run Code Online (Sandbox Code Playgroud)

这是我在web.config和app.config中的配置(在我的库中)

    <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
  </system.serviceModel>
  <system.web>
      <pages enableSessionState = "true" />
      <httpModules>
        <add type="System.Web.SessionState.SessionStateModule" name="Session"/>
      </httpModules>
      <compilation debug="true" targetFramework="4.0" />
    </system.web>
Run Code Online (Sandbox Code Playgroud)

(app.config)中

Dar*_*rov 30

根据您的评论,您似乎正在尝试访问Web服务中的会话.Web服务是无状态的,它们应该是这样的.如果您想违反此规则并使其成为有状态,您可以在经典的ASMX Web服务中启用会话,如下所示:

[WebMethod(EnableSession = true)]
public void SomeMethod()
{
    ... invoke the method in your class library that uses Session
}
Run Code Online (Sandbox Code Playgroud)

话虽这么说,HttpContext.Current在课堂图书馆中使用是一种应该不惜一切代价避免的做法.

  • 你能指点我一个技术的例子,在会话中消除需要吗?谢谢 (4认同)