Search code examples
.netmultithreadinggarbage-collectionwcf-data-servicesthreadstatic

Can I (safely) use the ThreadStatic attribute in ADO.NET Data Services?


I want to store per-thread data in an ADO.NET Data Service. Is it safe to use the ThreadStatic attribute on my thread-specific static variable, or will I run into problems? My concern is that my ThreadStatic variable(s) won't be garbage collected after the request is completed and the thread dies.

If there's a better way to do what I'm trying to do, please let me know. This just seems like the simplest solution.

Any information would be very helpful, thanks!


Solution

  • I've found that the DataService object that's created for each request is on a thread marked with IsThreadPoolThread = true, so using the [ThreadStatic] attribute is not appropriate in this case, as information from previous requests could be available by later ones (not desirable).