While troubleshooting an existing C# WCF web service, I traced an issue to a [ThreadStatic]
variable. The variable is modified across many layers and functions. When the variable got corrupted, an exception would occur on all consecutive HTTP calls to run on the thread. Recycling the application pool resolves the problem until the variable gets corrupted again.
Now I'm looking for an alternative. Ideally a property bag with the same lifetime as an HTTP request, so that we can be sure future HTTP calls are not affected by earlier HTTP calls. I came across OperationContext.Current.IncomingMessageProperties
which seems to work fine during tests, but the name suggests it's meant for message properties, and not an application variable.
How can I store a variable during the lifetime of an HTTP request?