asynchronous - Is the use of ThreadStatic in .NET framework code a harmful relic of a bygone age? -
[threadstatic]
used in various places in .net framework provide ambient context various features (e.g. transaction.current
, used transactionscope
).
unfortunately, means features thread juggling (asp.net, async keyword code) switch threads, don't copy transactionscope
, features transactionscope
don't work might expect.
there mechanism, callcontext.logicalgetdata
(more here) copy across state during thread switches correctly (at least in .net 4.5). seems me transactionscope
better if used rather [threadstatic]
.
if features using [threadstatic]
written today, rather being existing features requirements of backwards compatability, written using callcontext.(g|s)etlogicaldata
?
in reality have different use cases.
threadstatic
can't transfer value acrossawait
or similar context switch.callcontext
can't retain value per-thread.
so see, 1 can not replace other. threadstatic
low-level primitive. don't think use cases have gotten fewer after callcontext
etc. came along. note, use cases are vanishingly small -- think last time used on 2 years ago.
i characterize things transaction.current
abuse of tls. never designed this, , when tls appeared break async, because never should have been used in first place.
Comments
Post a Comment