Thursday, March 22, 2012

Working with the "Session" object from a custom class (compiled assembly)

Working with the "Session" object is quite simple when accessing it from a ".aspx" file or associated code-behind ... but when I move that functionality into my compiled assembly I am running into all sorts of issues. I understand that .aspx files have access to "Session" via the inherited Page class which my assembly doesn't inherit from (obviously). With this in mind, I imported "System.Web.SessionState" but was still unable to gain access to the "Session" object.
I am sure I am missing something quite simple here ... any ideas?
Thanks ahead of time for your help.
*** Shortly after posting this I came upon the answer I was looking for:http://forums.asp.net/922164/ShowPost.aspx ***

Have you tried something like:
HttpContext.Current.Session["ID"] = "1";
As an example?
Regards

Well really if you write good business objects the session is not in the same context, perhaps you should be passing the variable from the session into the class rather than reading the session at the class level. That way the class is more reusable and the code is more strongly typed as you know the type being passed to the class.
Make sense?
Alan

Alan,

That's exactly what I ended up doing. Once I figured out how to access the object in my class I realized that it didn't accomplish much other than increased dependancy between the objects.
Thanks for the feedback.

0 comments:

Post a Comment