Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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)

like image 470
SexyMF Avatar asked Sep 25 '11 13:09

SexyMF


People also ask

What is ASP.NET C?

ASP.NET is a web application framework developed and marketed by Microsoft to allow programmers to build dynamic web sites. It allows you to use a full featured programming language such as C# or VB.NET to build web applications easily.

Is ASP.NET like C#?

ASP.NET is a web application development framework used to develop web applications using different back-end programming languages like C# where C# is used as an object-oriented programming language to develop web applications along with ASP.NET.

Can I use .NET with C?

. NET Framework is an object oriented programming framework meant to be used with languages that it provides bindings for. Since C is not an object oriented language it wouldn't make sense to use it with the framework.

What is .NET ASP.NET and C#?

Basically, ASP.NET is a web delivery mechanism that runs either C# or VB.NET in the background. C# is a programming language that runs ASP.NET as well as Winforms, WPF, and Silverlight.


1 Answers

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.

like image 116
Darin Dimitrov Avatar answered Sep 19 '22 16:09

Darin Dimitrov