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 across await 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

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -