<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>ASP.NET Tips: Careful Use of Static's</title><link>http://blogs.msdn.com/b/tom/archive/2008/09/18/asp-net-tips-careful-use-of-static-s.aspx</link><description>Not sure how many people realize it, but when you start dealing with multiple users access a system (in this case a web server), you have to be very careful what you store in static's (global variables).&amp;#160; This is because all the users will have access</description><dc:language>en-US</dc:language><generator>Telligent Evolution Platform Developer Build (Build: 5.6.50428.7875)</generator><item><title>re: ASP.NET Tips: Careful Use of Static's</title><link>http://blogs.msdn.com/b/tom/archive/2008/09/18/asp-net-tips-careful-use-of-static-s.aspx#9454479</link><pubDate>Mon, 02 Mar 2009 17:42:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9454479</guid><dc:creator>Raj</dc:creator><description>&lt;p&gt;Here is a sample webpage that shows the dangers of using static variables in an ASP.Net website without understanding its consequences:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://www.aggregatedintelligence.com/Samples/StaticsDemo/default.aspx"&gt;http://www.aggregatedintelligence.com/Samples/StaticsDemo/default.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;And my explanation:&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://blog.aggregatedintelligence.com/2009/01/static-variables-and-their-implications.html"&gt;http://blog.aggregatedintelligence.com/2009/01/static-variables-and-their-implications.html&lt;/a&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9454479" width="1" height="1"&gt;</description></item><item><title>re: ASP.NET Tips: Careful Use of Static's</title><link>http://blogs.msdn.com/b/tom/archive/2008/09/18/asp-net-tips-careful-use-of-static-s.aspx#8958988</link><pubDate>Fri, 19 Sep 2008 18:59:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8958988</guid><dc:creator>Dave Black</dc:creator><description>&lt;P&gt;Just to underscore some things (Tom already answered a few):&lt;/P&gt;
&lt;P&gt;1. User-specific values should really be stored in Session. &amp;nbsp;You could use client-side cookies if there is not any security sensitive info in there (e.g. username, password, etc.). &amp;nbsp; Also, client-side cookies are limited to 4K in length. &amp;nbsp;ASP.NET Personalization and WebParts is an option for WebPart kind of things. &amp;nbsp;If the values are meant only to be held for the life of the ASP.NET page, you could also use Viewstate (again don't place security-sensitive info here since ViewState is only Base64 Encoded and not encrypted).&lt;/P&gt;
&lt;P&gt;***&amp;lt;b&amp;gt;As a security note, you should *not* store password in Session or in Cookies in case the Session or Cookie is hijacked (which is trivial to do).&amp;lt;/b&amp;gt;***&lt;/P&gt;
&lt;P&gt;2. Application-wide or "shared" values should really be stored in the Cache. &amp;nbsp;These values can be seen and accessed by anyone in the AppDomain, so be careful not to put sensitive or user-specific values in there. &amp;nbsp;Another option is to use Application variables. &amp;nbsp;However, it is recommended to use the Cache.&lt;/P&gt;
&lt;P&gt;3. Initialization of Static variables in Thread-safe in all versions of .NET. &amp;nbsp;This is why the Singleton Pattern uses static variables for its implementation.&lt;/P&gt;
&lt;P&gt;4. The use of Static Constructors should be carefully considered. &amp;nbsp;You should use Static Initializers (or inline initialization of a static variable - i.e. inline where it is declared) when possible. &amp;nbsp;The CLR is able to optimize the performance of types that have not explicitly defined a static constructor. &amp;nbsp;One case of this is with the use of NGEN. &amp;nbsp;The biggest difference of use of a static constructor vs. inline static variable initialization is the &amp;lt;b&amp;gt;timing&amp;lt;/b&amp;gt; of when each happens. &amp;nbsp;The majority of the time you shouldn't, and don't, care about when your static variables are initialized. &amp;nbsp;However, if it is of importance, the CLR will initialiaize inline static vars. whenever it wants to. &amp;nbsp;On the other hand, the firing ofa static constructor is guaranteed to happen right &amp;lt;b&amp;gt;before the first static member is accessed.&amp;lt;/b&amp;gt; &amp;nbsp;Note that *all* static members who are not explicitly initialized inline are initialized once the static constructor fires. &amp;nbsp;Obviously if you needed to do additional things with a static variable after it has been initialized, you would need to use a static constructor instead of inline initialization. &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;HTH&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8958988" width="1" height="1"&gt;</description></item><item><title>re: ASP.NET Tips: Careful Use of Static's</title><link>http://blogs.msdn.com/b/tom/archive/2008/09/18/asp-net-tips-careful-use-of-static-s.aspx#8958651</link><pubDate>Fri, 19 Sep 2008 15:25:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8958651</guid><dc:creator>ASP.NET Debugging</dc:creator><description>&lt;p&gt;rtpHarry,&lt;/p&gt;
&lt;p&gt;No that shouldn't cause any problems. &amp;nbsp;The application is meant to handle multiple threads all calling a static function at the same time. &amp;nbsp;No problem with that.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8958651" width="1" height="1"&gt;</description></item><item><title>re: ASP.NET Tips: Careful Use of Static's</title><link>http://blogs.msdn.com/b/tom/archive/2008/09/18/asp-net-tips-careful-use-of-static-s.aspx#8958650</link><pubDate>Fri, 19 Sep 2008 15:24:30 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8958650</guid><dc:creator>ASP.NET Debugging</dc:creator><description>&lt;p&gt;bobh,&lt;/p&gt;
&lt;p&gt;Yes, if the data is private to each user, session is a good place to store it.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8958650" width="1" height="1"&gt;</description></item><item><title>re: ASP.NET Tips: Careful Use of Static's</title><link>http://blogs.msdn.com/b/tom/archive/2008/09/18/asp-net-tips-careful-use-of-static-s.aspx#8958520</link><pubDate>Fri, 19 Sep 2008 13:02:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8958520</guid><dc:creator>rtpHarry</dc:creator><description>&lt;p&gt;If my data layer has static functions such as update and two requests come in at the same time could this potentially cause problems?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8958520" width="1" height="1"&gt;</description></item><item><title>re: ASP.NET Tips: Careful Use of Static's</title><link>http://blogs.msdn.com/b/tom/archive/2008/09/18/asp-net-tips-careful-use-of-static-s.aspx#8958441</link><pubDate>Fri, 19 Sep 2008 11:46:24 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8958441</guid><dc:creator>bobh</dc:creator><description>&lt;p&gt;So what would the solution be to persist data for only the current user? Store it in the session?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8958441" width="1" height="1"&gt;</description></item><item><title>re: ASP.NET Tips: Careful Use of Static's</title><link>http://blogs.msdn.com/b/tom/archive/2008/09/18/asp-net-tips-careful-use-of-static-s.aspx#8958155</link><pubDate>Fri, 19 Sep 2008 06:53:39 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8958155</guid><dc:creator>Dave Black</dc:creator><description>&lt;p&gt;The scope of a static variable is limited to an Application Domain.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8958155" width="1" height="1"&gt;</description></item><item><title>re: ASP.NET Tips: Careful Use of Static's</title><link>http://blogs.msdn.com/b/tom/archive/2008/09/18/asp-net-tips-careful-use-of-static-s.aspx#8957627</link><pubDate>Thu, 18 Sep 2008 20:50:55 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8957627</guid><dc:creator>Muhammad Mosa</dc:creator><description>&lt;p&gt;Yes this is a good point talk about, last month someone raised a post regarding this in ASP.NET Forums&lt;/p&gt;
&lt;p&gt;&lt;a rel="nofollow" target="_new" href="http://forums.asp.net/t/1302762.aspx"&gt;http://forums.asp.net/t/1302762.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;thanks for sharing&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8957627" width="1" height="1"&gt;</description></item><item><title>re: ASP.NET Tips: Careful Use of Static's</title><link>http://blogs.msdn.com/b/tom/archive/2008/09/18/asp-net-tips-careful-use-of-static-s.aspx#8957512</link><pubDate>Thu, 18 Sep 2008 19:44:11 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8957512</guid><dc:creator>ASP.NET Debugging</dc:creator><description>&lt;p&gt;Nick,&lt;/p&gt;
&lt;p&gt;the ' was just cause of spellcheck. &amp;nbsp;And I didn't catch it to change it back. &amp;nbsp;It depends on what you mean by global, but if you mean that other can access that bit of memory, that it is global. &amp;nbsp;If you mean any part of the application can access it, then you are right, it is not.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8957512" width="1" height="1"&gt;</description></item><item><title>re: ASP.NET Tips: Careful Use of Static's</title><link>http://blogs.msdn.com/b/tom/archive/2008/09/18/asp-net-tips-careful-use-of-static-s.aspx#8957498</link><pubDate>Thu, 18 Sep 2008 19:36:52 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8957498</guid><dc:creator>Sumit</dc:creator><description>&lt;p&gt;Absolutely rite Torn.....we faced that kinna prblm in past because of static global variables in a multiple server environment.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8957498" width="1" height="1"&gt;</description></item></channel></rss>