Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use ASP.NET Session[] variable in an external DLL

I have two projects, the DLL project which has all my logic and data access stuff, and the ASP.NET project which does my forms etc.

I am a bit confused. I thought if I added the System.Web namespace reference to the DLL project I would be able to reference the Session state information of the ASP.NET page.

I could use each page to get the session info out and pass it to the DLL for the processing but would love to be able to process things directly from the DLL class(s).

Is this possible?

I have fiddled with the System.Web namespace and just seem able to get a reference to the Session variable.

Thanks all.

Jon

like image 470
Jon Avatar asked Jan 17 '09 22:01

Jon


People also ask

Where are ASP.NET session variables stored?

Session variables are stored in a SessionStateItemCollection object that is exposed through the HttpContext. Session property. In an ASP.NET page, the current session variables are exposed through the Session property of the Page object.

Are session variables server side?

Session Variables- The session variables are variables maintained on server side by asp.net runtime. Each user is identified by a a unique number called SessioID.

Can we use session in ASPX page?

You can't, JavasSript is used for client side scripting on the browser, and cannot access a Session object from a server. JavaScript runs in the clients browser, your Session object is exposed by the server. There's disconnected space between them.

Are ASP.NET session variables secure?

It will perform fast because the session is kept on the web server within the ASP.NET Worker Process. Data is stored separately and the data is secure so it is suitable for web applications.


3 Answers

As long as the Assembly is loaded in the scope of the Session, it will have access.

Although this type of tight coupling isn't really recommended.

like image 50
Tom Anderson Avatar answered Oct 06 '22 00:10

Tom Anderson


You should be able to use HttpContext.Current.Session

Edit

While yes I agree you should not tightly couple your Business Logic DAL or etc assemblies to ASP.Net session. There are plenty of valid cases for accessing HTTP Context outside of a web project.

Web Controls is probably one of the best examples, reusable HTTP modules, etc..

Now one option, if you want to have your DLL pull the stuff from Session, is to abstract out session. So you could define an interface like IStorage, that your library will know how to use. Then you can have a SessionStorage or MemoryStorage class and use IoC to inject the appropriate class into your library classes. This gives you the freedom to code it how you wanted it to be coded without tying your code to Session. Oh and one other benefit if done properly can be used to not tie your code to the session on the web either.

like image 33
JoshBerke Avatar answered Oct 05 '22 23:10

JoshBerke


You can always use HttpContext.Current.Session in your DLL but this is considered as bad practice. A better approach would be to pass the values stored in the session dictionary to your DLL instead of it referencing the session. Another benefit you will gain is that the code in your DLL won't be coupled to the ASP.NET runtime meaning it will be easier to test.

like image 45
Darin Dimitrov Avatar answered Oct 06 '22 00:10

Darin Dimitrov