threadstatic

ThreadStatic in asynchronous ASP.NET Web API

跟風遠走 提交于 2019-12-10 16:48:05
问题 Is there a possibility to use thread static like variables within a single request? The current code uses a thread static variable for logging purposes and now we want to use async controller methods (with async and await pattern) which results in problems because the variable is null when a new thread is opened. 回答1: await can cause thread jumps, so thread static variables will naturally cause problems. To work around this, you can either use AsyncLocal<T> (available in .NET 4.6), or (if you

is thread switching possible during request processing?

一曲冷凌霜 提交于 2019-12-08 05:43:56
问题 I have an MVC application, which also uses EF and a simple Unit of work pattern implementation. Here's what my UnitOfWork looks like: public class UnitOfWork : IUnitOfWork { [ThreadStatic] private static UnitOfWork _current; private MyContext _context; public static UnitOfWork Current { get { return _current; } } public UnitOfWork() { _current = this; } public MyContext GetContext() { if(_context == null) _context = new MyContext(); return _context; } public int Commit() { return _context ==

C# Singleton Pattern Designs for ThreadStatic

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 04:21:42
问题 I want to figure out about singleton pattern designs. I want to create seperated instances for per thread from my singleton class. So I provided two designs below. It is Working class Program { static void Main(string[] args) { Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode())); Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode())); Console.ReadLine(); } } public sealed class SingletonClass { [ThreadStatic] private static

Are WCF request handling Thread Agile?

扶醉桌前 提交于 2019-12-05 16:34:05
I have seen lots of documentation on how Agile Asp.Net Request handling is? I want to know is the case same with WCF Request handling. Can we rely on the fact that the Thread that starts Wcf request handling will finish it? I am maintaining a Wcf Application where at lots of places ThreadStatic variables are used. Although the code is working but is it reliable? Is it worth changing it or should I keep it as it is? Anders Abel When creating a WCF service you can set the threading and service instantiating behaviour, by decorating the service implementation class with a ServiceBehavior

What's the effect of AsyncLocal<T> in non async/await code?

放肆的年华 提交于 2019-12-03 11:40:33
问题 I'm working on a very large and old code base of a desktop winform application. In this code base there are lots of operations performed in background threads, mainly using BackgroundWorker . A common pattern in this code base, is to hide complexity by binding artifacts to the thread being executed. For instance, the database connection and transaction are stored in [ThreadStatic] fields. I'm trying to change this, and start using async/await code, and benefit from running the task in any

Using ThreadStatic to replace expensive locals — good idea?

天涯浪子 提交于 2019-12-03 02:01:50
Update : as I should have expected, the community's sound advice in response to this question was to "measure it and see." chibacity posted an answer with some really nice tests that did this for me; meanwhile, I wrote a test of my own; and the performance difference I saw was actually so huge that I felt compelled to write a blog post about it. However, I should also acknowledge Hans's explanation that the ThreadStatic attribute is indeed not free and in fact relies on a CLR helper method to work its magic. This makes it far from obvious whether it would be an appropriate optimization to

asp.net mvc3 request thread affinity

感情迁移 提交于 2019-11-30 18:35:41
I am using a proprietary IoC mechanism in my asp.net mvc3 application (on IIS7) that saves state in [ThreadStatic] fields and therefore relies on an assumption that HttpApplication.BeginRequest, HttpApplication.EndRequest and the whole synchronous execution of the (single) request they relate to are executed on the same thread. Is that assumption correct? Is that assumption correct? No, this assumption is not correct and there's evidence for it. The only reliable per request storage mechanism in ASP.NET is HttpContext.Items . Never use [ThreadStatic] fields to store per-request values in an

asp.net mvc3 request thread affinity

霸气de小男生 提交于 2019-11-30 02:46:58
问题 I am using a proprietary IoC mechanism in my asp.net mvc3 application (on IIS7) that saves state in [ThreadStatic] fields and therefore relies on an assumption that HttpApplication.BeginRequest, HttpApplication.EndRequest and the whole synchronous execution of the (single) request they relate to are executed on the same thread. Is that assumption correct? 回答1: Is that assumption correct? No, this assumption is not correct and there's evidence for it. The only reliable per request storage

Initializing ThreadStatic field still causes NullReferenceException

牧云@^-^@ 提交于 2019-11-28 20:06:56
I've written myself a multi-threaded random generator public static class MyRandGen { private static Random GlobalRandom = new Random(); [ThreadStatic] private static Random ThreadRandom = new Random(SeedInitializer()); private static int SeedInitializer() { lock (GlobalRandom) return GlobalRandom.Next(); } public static int Next() { return ThreadRandom.Next(); } } However, it throws me a NullReferenceException on firing Next(), which I don't understand. Is that kind of initializing ThreadStatic fields forbidden somehow? I know I could just check if the field's initialized every time, but that

ThreadStaticAttribute in ASP.NET

痴心易碎 提交于 2019-11-27 13:59:05
I have a component that needs to store static values fore each thread. It's a general component that can be used in many scenarios and not only in ASP.NET. I was thinking to use the [ThreadStatic] attribute to achieve my goal. Supposing that it would also work fine in ASP.NET scenarios, because i was assuming that every Request is called in a own thread. After some research i found this Blog Post from Scott Hanselman saying to be careful when using [ThreadStatic] in ASP.NET. However most of the comments (below the Post) do not agree with that was Scott wrote, saying that a Request always run