threadstatic

Is there any way to imitate ThreadStatic for use with HttpContext.Current.Items?

主宰稳场 提交于 2020-01-23 02:46:11
问题 Because of Thread Agility in ASP.Net, ThreadStatic is not an appropriate mechanism to use in web applications for segregating static property access from one request to the next. In order to avoid lots of calls to HttpContext.Current.Items and the associated null checks and so forth, is there any trickery offered by the .Net framework whereby I could create an attribute which works sort of like ThreadStatic , but utilises HttpContext.Current.Items if the current code is being executed within

Is there any way to imitate ThreadStatic for use with HttpContext.Current.Items?

半腔热情 提交于 2020-01-23 02:45:06
问题 Because of Thread Agility in ASP.Net, ThreadStatic is not an appropriate mechanism to use in web applications for segregating static property access from one request to the next. In order to avoid lots of calls to HttpContext.Current.Items and the associated null checks and so forth, is there any trickery offered by the .Net framework whereby I could create an attribute which works sort of like ThreadStatic , but utilises HttpContext.Current.Items if the current code is being executed within

Using ThreadStatic to replace expensive locals — good idea?

…衆ロ難τιáo~ 提交于 2020-01-22 08:57:24
问题 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

Is the use of [ThreadStatic] at odds with asynchronous code?

旧街凉风 提交于 2020-01-14 19:34:05
问题 We have a fairly large existing code base for various webservices built on top of ASP.NET and that code makes heavy use of accessing HttpContext.Current.User (wrapped as Client.User ) which I'm fairly sure internally uses [ThreadStatic] to give you that ambient scoping. I'm currently looking into if it's possible that we start to use more asynchronous code in the form of async/await but I'm having a hard time finding how the use of [ThreadStatic] fits into this. Removing the reliance on

Can a ThreadStatic IDisposable be automatically disposed?

一个人想着一个人 提交于 2020-01-10 19:53:08
问题 This is not a question of how to automatically call dispose - my problem is the opposite: I have a thread pool where each thread has a ThreadStatic Graphics (which was created from an Image) to perform text size measuring. Now I've ran into the problem that from time to time the graphics seems to be disposed as even reading the TextRenderingHint property fails(causes an ArgumentException). Is there some mechanism which disposes the Graphics e.g. if the thread is idle for a long period? 回答1:

C# ThreadStatic + volatile members not working as expected

人盡茶涼 提交于 2019-12-23 09:39:12
问题 I was reading through the tips and tricks post and I thought I'd try out some of the C# stuff that I'd never done before. Therefore, the following code serves no actual purpose, but is just a 'test function' to see what happens. Anyway, I have two static private fields: private static volatile string staticVolatileTestString = ""; [ThreadStatic] private static int threadInt = 0; As you can see, I'm testing ThreadStaticAttribute and the volatile keyword. Anyway, I have a test method that looks

Are WCF request handling Thread Agile?

泪湿孤枕 提交于 2019-12-22 07:58:56
问题 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? 回答1: When creating a WCF service you can set the threading and service

ThreadStatic v.s. ThreadLocal<T>: is generic better than attribute?

北城以北 提交于 2019-12-17 07:15:47
问题 [ThreadStatic] is defined using attribute while ThreadLocal<T> uses generic. Why different design solutions were chosen? What are the advantages and disadvantages of using generic over attributes in this case? 回答1: Something the blog post noted in the comments doesn't make explicit, but I find to be very important, is that [ThreadStatic] doesn't automatically initialize things for every thread. For example, say you have this: [ThreadStatic] private static int Foo = 42; The first thread that