Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way I could set context for a session (ie, one call to my API) and not for the entire app domain? (ASP.NET)

Tags:

c#

.net

asp.net

I have created a large application and I have now run into a problem.

I have separated customers by virdirs so they have always been in different application pools. I took advantage of this and set static variables for db connection string and other context stuff on session_start and had it available to me throughout my app.

Now I have been overloaded with the amount of virdirs I have had to create (over 500 and quickly growing) and I feel I need to move these to one (or several) application pools. The problem is I don't pass the "session context" I get from the URL, throughout the app. Changing to the app to pass the context down would basically mean I would need to rewrite the app.

Is there a way I could set this context for a session (ie, one call to my API) and not for the entire app domain? Your help is greatly appreciated!

context example - db con str - customer log folder

EDIT: I was thinking I could maybe have a table that links the context information to the thread id (System.Threading.Thread.CurrentThread.ManagedThreadId)?

like image 337
SolarFlair12 Avatar asked Dec 07 '25 08:12

SolarFlair12


1 Answers

What do you mean by 'context'? Do you mean the request/response/session information? You do not need to pass them manually. For the duration of processing of a http request, the ASP.Net framework exposes all that in a static way:

var ctx = System.Web.HttpContext.Current;
var req = ctx.Request;
var rsp = ctx.Response;
var sess = ctx.Session;
var cache = ctx.Cache;

var myOtherFoos = ctx.Items;

In an ASP.Net application, you can access the static Current-Context from just anywhere, provided you have added the reference to System.Web assembly.

If you do not mean that "context", but if you mean some your own additional information that you need to pass along with the request-processing, then the Items of HttpContext is just for that! It is a fresh collection created at each new request and you can use it as a lightweight scratchpad to keep your things during single-request processing. Unlike Cache or Session, the "Items" evaporate at the moment the request processing ends, so there's no worry about leaks or mixing data with other requests. Just be careful in what keys you pick, try to not collide with the framework thingies that sit there :)

like image 148
quetzalcoatl Avatar answered Dec 08 '25 20:12

quetzalcoatl