0

I have a View which is using

Session["Something"] = 1;

And I have a class inside a class library project that tries to get this value from the session using

HttpContext.Current.Session["Something"]

But it retuns null.

public ActionResult Index()
{
    Session["Something"] = 1;
    var fromLib = (new MyLibClass()).GetSession("Something"); // null
    var fromHere = Session["Something"]; // 1
}

// Class library project
public object GetSession(string key)
{
    return HttpContext.Current.Session[key];
}

How can I get the value from the session? Or is there a way to save a value and retrieve in another place (without database or system files)

BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • 2
    can I ask what the point of the `GetSession()` actually is? If you can access directly from `HttpContext.Current.Session()` then there's no need for a custom method to access it. – Chase Florell May 08 '11 at 21:33
  • @rockin The point is that it is a very simple example to show what the problem is. – BrunoLM May 08 '11 at 22:07
  • it's bad practice to use session outside the web app, mainly because it makes unit testing extremely difficult. If this is just an example, consider stating what your actually trying to do, so we can suggest an alternative, such as caching in your class library, or moving that "session" logic to somewhere in the web tier. – RPM1984 May 08 '11 at 23:45
  • @RPM When I login on Facebook/Twitter using oAuth I am storing the token in the session, and I made a property to access the session and return the token. Do you have an alternative for this? For Twitter I could save on the database since it never expires, but for Facebook I need to store on the session or in a cookie, I guess... – BrunoLM May 09 '11 at 00:50
  • funny that. I'm also using Facebook OAuth and store the token. But i use cookies (because i have some client-side stuff that needs to access it). But the trick is, this code is in action filters in the web application. No class library. I use the Facebook C# SDK but the code that stores the token in the cookies is an action method which handles the PostAuthorize URL (Oauth exchange). Can't you do the same? Since session/cookies is static to a http context, create a public static class in your web app that has a getter/setter for the token. Why does it need to be in your class lib? – RPM1984 May 09 '11 at 00:54

2 Answers2

1

You don't need Session object.

Although Session is good for holding variables inside an asp.net application you can implement your own session object for using inside your class libraries which is light and fast and internal to your code:

Dictionary<string, object> MySession = new Dictionary<string, object>();
MySession.Add("VariableName", myObject);
MyLib.Pass(MySession);

Try to keep it more specific if possible i.e. if you just pass MyClass to your library then:

Dictionary<string, MyClass> MySession = new Dictionary<string, MyClass>();
Xaqron
  • 29,931
  • 42
  • 140
  • 205
  • But how that can that be shared between the web app and the class library? You making it public? What about thread safety? – RPM1984 May 08 '11 at 23:39
  • Inside web application, you should continue using session object as the container, but when passing parameters to your libraries, use your own. You need to change some lines of your libraries to recognize this data container. If you need to make it thread-safe, use locks around internal `Dictionary` when changing and iterating it. – Xaqron May 09 '11 at 11:38
1

There are two sessions

Session (refers to the view session)
HttpContext.Current.Session (refers to context session)

Using HttpContext.Current.Session everywhere works as they are the same. I made a class to help me access the session values the way I want, so I can just call it without worrying about the correct session.

BrunoLM
  • 97,872
  • 84
  • 296
  • 452