[ThreadStatic]
is used in various places in the .NET framework to provide an ambient context for various features (e.g. Transaction.Current
, which is used for TransactionScope
).
Unfortunately, this means that features which do some thread juggling (ASP.NET, async keyword code) switch threads, but don't copy the TransactionScope
, so features like TransactionScope
don't work as you might expect.
There is another mechanism, CallContext.LogicalGetData
(more here) which does copy across state during thread switches correctly (at least in .NET 4.5). It seems to me that TransactionScope
would be better if it used this rather than [ThreadStatic]
.
If the features that are using [ThreadStatic]
were written today, rather than being existing features with requirements of backwards compatability, would they be written using CallContext.(G|S)etLogicalData
?