I have created a library which is using threadstatic variables to be used by various classes of the same library. Once initialized for a thread, all these classes work together to achieve a task using these variables.
Now i need to use this same library in an ASP.NET application and i have come to know that threadstatic won't make my variables global for a single thread but these will be shared across threads.
I am avoiding passing variables to functions because it does not sound good approach. So I need a solution to make my library usable for both type of applications (winforms + webforms) i.e. an alternate for threadstatic and session.
How about implementing my own management using dictionary in the library? Knowing who has called me (library) i.e. desktop application or asp.net and after that using thread id or session id respectively to grab variables from the dictionary. Right now, i don't know whether it is possible or not.
And what if a web service is created for this library which will be hosted by an exe instead of IIS to avoid thread sharing by ASP.NET causing threadstatic to malfunction ?
Please suggest a solution.
The problem is not that they will be shared across threads, but across requests. In ASP.NET, threads are shared between requests, and requests can even be handled by more than one thread (though I believe there are only certain places/circumstances this would happen).
I'm not sure why you think this is a bad approach - it sounds a lot better than using ThreadStatic :)
This sounds like a lot of work to avoid passing data around, but it should be possible. There may be some built-in classes to make this a little easier, such as using
HttpContext.Items
, which is a cache that exists per-request, orCallContext
, but it depends on exactly how your code is being used.