开发者

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

开发者 https://www.devze.com 2023-04-07 02:54 出处:网络
this is how I initiate the session protected void Session_Start(object sender, EventArgs e) { HttpContext.Current.Session[\"CustomSessionId\"] = Guid.NewGuid();

this is how I initiate the session

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

in my solution under a class library i am triyng to access it and getting null exception:

string sess = HttpContext.Current.Session["Custom开发者_运维技巧SessionId"] ;

this is my config in web.config and app.config (in my library)

    <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>

(app.config)


According to your comments it seems that you are trying to access the session in a web service. Web services are stateless and that's how they should be. If you want to violate this rule and make them stateful you could enable sessions in a classic ASMX web service like this:

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

This being said, using HttpContext.Current in a class library is a very practice that should be avoided at all price.

0

精彩评论

暂无评论...
验证码 换一张
取 消