14

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["CustomSessionId"] ;

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)

5
  • At what stage of the execution of the HTTP request are you calling the method in your class library that tries to access the session? Commented Sep 25, 2011 at 13:15
  • it's hard to tell but I think it's not your Session that is null but the HttpContext.Current - and BTW: do you know that you try to convert a Guid to a string even if this works? Commented Sep 25, 2011 at 13:16
  • Actually it is inside a webservice that i did not mention (ill edit my post) Commented Sep 25, 2011 at 13:17
  • Just check the HttpContext.Current Commented Sep 25, 2011 at 13:18
  • I added the [WebMethod(EnableSession = true)] and it is working, thanks Commented Sep 25, 2011 at 13:26

1 Answer 1

35

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Till I can tick the "V" for you, can you explain why it is not reccomended to do so? thanks
@putin, there are many reasons. For example HttpContext.Current is a static method which works only in a web context. This means that you will never be able to unit test your class library functions in isolation if they were relying on it. Another drawback is that you are actively coupling your class library to an ASP.NET environment and class libraries are meant to be reusable components. As far as the Session part is concerned, sessions are by default stored in memory of the web server. So for example if you run in a web farm, the session must be distributed ...
... across all the nodes of this web farm or a client that had his session created on node1 won't find it on node2. Sessions make web applications stateful and more difficult to scale for that reason. Avoid them at all cost. There are so many other techniques in the HTTP world that would allow you to achieve stateless applications that you should benefit from.
can you point me for an example of technology the remove the need in session? thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.