tag:blogger.com,1999:blog-83115291382254516102024-03-18T19:29:29.866+00:00Learning by bloggingOmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.comBlogger19125tag:blogger.com,1999:blog-8311529138225451610.post-51590226532847365902010-09-15T11:29:00.001+01:002010-09-18T20:59:49.984+01:00Bad news, everyone!<p>If <a href="http://threatpost.com/en_us/blogs/new-crypto-attack-affects-millions-aspnet-apps-091310"><u><font color="#0000ff">it is</font></u></a> true..</p> <p><img src="http://farm1.static.flickr.com/211/462028458_5e9816b80d.jpg" /></p> <p><strong>update</strong> (18/09/2010/): <br />Microsoft has released an advisory to help customers understand the vulnerability and apply workarounds to secure their sites. The advisory is at <a href="http://www.microsoft.com/technet/security/advisory/2416728.mspx"><u>http://www.microsoft.com/technet/security/advisory/2416728.mspx</u></a>.</p> <p>The Microsoft Security Response Center (MSRC) has released a blog at <a href="http://blogs.technet.com/b/msrc/archive/2010/09/17/security-advisory-2416728-released.aspx"><u>http://blogs.technet.com/b/msrc/archive/2010/09/17/security-advisory-2416728-released.aspx</u></a>.</p> <p>The Security Research & Defense (SRD) team at Microsoft has also released a blog that contains a script to help detect vulnerable installations. The blog is located at <a href="http://blogs.technet.com/b/srd/archive/2010/09/17/understanding-the-asp-net-vulnerability.aspx"><u>http://blogs.technet.com/b/srd/archive/2010/09/17/understanding-the-asp-net-vulnerability.aspx</u></a> </p> <p>Scott Guthrie about the vulnerability <br /><a title="http://weblogs.asp.net/scottgu/archive/2010/09/18/important-asp-net-security-vulnerability.aspx" href="http://weblogs.asp.net/scottgu/archive/2010/09/18/important-asp-net-security-vulnerability.aspx"><u>http://weblogs.asp.net/scottgu/archive/2010/09/18/important-asp-net-security-vulnerability.aspx</u></a></p> <p>This is really scaring me.. <a href="http://www.youtube.com/v/yghiC_U2RaM"><u>http://www.youtube.com/v/yghiC_U2RaM</u></a></p> OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com5tag:blogger.com,1999:blog-8311529138225451610.post-13373636562097375622010-03-16T22:30:00.000+00:002010-03-16T22:35:24.762+00:00C# iterators based scheduling<p align="justify">If you ask Google about <a href="http://www.google.com/search?q=asynchronous+iterators"><font color="#0000ff">asynchronous iterators</font></a> you'll see that a lot of people have been using C# iterators for doing things that iterators were not supposed to do in the first place - from effective and convinient IO to microthreading and cooperative multitasking. <br />Several months ago being influenced by CCR iterators and AsyncEnumerator I tried to create microthreading library that could schedule microthreads on many OS threads and be integrated with different async patterns - from APM through EAP to TPL's Tasks. The early prototype looked promising but iterators are really optimzed for generating sequences effectively so there are some restrictions and inconveniences. <br />I hope the implementation of  <a href="http://microsoftpdc.com/Sessions/FT11?type=wmv"><font color="#0000ff">resumable methods that may appear in future C#</font> </a>will let us build on top of it all aforementioned scenarios, and even those which the future methods are not supposed to be used for. <br /> <br />Why iterators were implemented the way they were implemented in <a href="http://blogs.msdn.com/ericlippert/archive/tags/Iterators/default.aspx"><font color="#0000ff">Eric Clipperts' serie on iterator blocks</font></a>.</p> OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com1tag:blogger.com,1999:blog-8311529138225451610.post-49851573138763940312010-03-14T15:37:00.006+00:002010-03-14T15:52:04.204+00:00C# 4.0 events. Small implementation changeC# 4.0 is going to change the way how event handlers addition and removal is implemented. From it birth C# compiler used to add special accessor methods to every event which didn’t have its own accessors declared. For thread safety those auto accessors had MethodImpl(MethodImplOptions.Synchronized) attribute defined on them. Using that attribute on publicly accesible methods is considered harmful because at runtime those methods are wrapped in lock(this) or lock(typeof([TypeDeclaringTheEvent])) for static events which, in its turn,<font color="#0000ff"> </font><a href="http://msdn.microsoft.com/en-us/library/c5kehkcz(VS.80).aspx"><font color="#0000ff">is a bad practice </font></a>as well. <br />
<br />
In C# 4.0 event accessors don't use explicit locking, but implement what can be considered as optimistic locking. Look at the following: <br />
<br />
<pre class="brush:c-sharp" name="code">//pre 4.0
[MethodImpl(MethodImplOptions.Synchronized)]
public void add_Disposed(EventHandler value)
{
this.Disposed = (EventHandler) Delegate.Combine(this.Disposed, value);
}
// 4.0
public void add_Disposed(EventHandler value)
{
EventHandler handler2;
EventHandler disposed = this.Disposed;
do
{
handler2 = disposed;
EventHandler handler3 = (EventHandler) Delegate.Combine(handler2, value);
disposed = Interlocked.CompareExchange<eventhandler>(ref this.Disposed, handler3, handler2);
}
while (disposed!= handler2);
}
</pre><br />
With the new algorithm we don't have to have one private object for each public event just to protect them from lockin issues. Besides that it is a little bit lighter and more performant. Delegate type itself hasn't changed at all. Delegates have been immutable reference types from the very begining. The only question I have: Why they didn't do that in the first place!?<br />
<br />
More details in C# compiler team member's blog:<br />
<a href="http://blogs.msdn.com/cburrows/archive/2008/02/18/field-like-events-considered-harmful.aspx"><font color="#0000ff">Field-like Events Considered Harmful</font><br />
</a><a href="http://blogs.msdn.com/cburrows/archive/2010/03/08/events-get-a-little-overhaul-in-c-4-part-ii-semantic-changes-and.aspx"><font color="#0000ff">Events get a little overhaul in C# 4, Part II: Semantic Changes and +=/-=</font></a>OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com2tag:blogger.com,1999:blog-8311529138225451610.post-74866040637066069632010-03-07T15:59:00.001+00:002010-03-07T15:59:42.421+00:00What I’d like to see in C# 5.0<p><a href="http://msdn.microsoft.com/en-us/devlabs/dd795202.aspx"><u><font color="#0000ff">Axum</font></u></a>, an experimental language, has such a feature as async methods. In such methods interaction with  interaction points and APM based API happens asynchronously. <br />Besides that there are signs that in the future C# will support resumable methods. Luca Bolognese opened up some details at the PDC 2009 session<font color="#0000ff"> </font><a href="http://microsoftpdc.com/Sessions/FT11?type=wmv"><u><font color="#0000ff">Future of C# and VB</font></u></a>. Fast-forward to 50:00 where he shows experimental yield keyword in the method returning Async<T>. <br />That’s great news because with what we have now  if you want to make long running call not keeping your thread waiting/doing nothing/consuming  memory you have to put all your logic in callback methods calling each other. With resumable methods such code would be much  easier to write, read and refactor, and the exception handling would look like  synchronous, stack based. <br /> <br />But being excited about the feature:</p> <ul> <li>I don’t like the idea of introducing a new type (Async<T>) when we already have Task and Task<T> types </li> <li>I’d like C# to recognize not only APM pattern based API but methods and properties that return Task or Task<T> as well. If you compare Task and IAsyncResult you’ll see that a Task based asynchronous API is much simpler, cleaner and easier to implement. Compare the next two variations: <br /> <br />//Task based API <br />public Task  UploadData(string uri, string data)  { … } <br />public Task<string> DownloadData(string uri) { … } <br />public Task<int> TotalNumberOfRows { get { … } } <br /> <br />//APM based API <br />public IAsyncResult BeginUploadData(string uri, string data, AsyncCallback callback, object context)  { … } <br />public void EndUploadData(IAsyncResult ar) <br /> <br />public IAsyncResult BeginDownloadData(string uri, AsyncCallback callback, object context) { … } <br />public string EndDownloadData(IAsyncResult ar) <br /> <br />public IAsyncResult BeginGetTotalNumberOfRows(AsyncCallback callback, object context) { … } <br />public int EndGetTotalNumberOfRows(IAsyncResult ar) <br /> <br /></li> <li>I’d like C# to recognize extension methods that implement APM or Task based API </li> </ul> OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com2tag:blogger.com,1999:blog-8311529138225451610.post-27457296271404354152010-02-27T19:08:00.002+00:002010-07-21T07:44:30.364+01:00EqualityComparer for C# 3.0 Expressions<p>.NET Expression classes let us perform only reference equality checks on them. But sometimes we need to check the true value equality.  As part of my attempt to build autocacher for LINQ TO SQL compiled queries I created an equality comparer that implements IEqualityComparer<Expression> interface. It lets me, for example, to use an Expression as a key in a dictionary. The implementation is .NET 3.5 specific  and based on the <a href="http://msdn.microsoft.com/en-us/library/bb882521.aspx">example ExpressionVisitor</a> class from MSDN. As a bonus  there are extensions to the Expression class in the project so that you can visit and rewrite  expressions without deriving from ExpressionVisitor. It can be very handy in simple scenarios.</p><a href="http://cid-e2f51563c5286879.skydrive.live.com/self.aspx/SharedFiles/ExpressionEqualityComparer.zip">ExpressionEqualityComparer.zip</a> <p>p.s. BTW in .NET 4.0 they made  the <a href="http://msdn.microsoft.com/en-us/library/system.linq.expressions.expressionvisitor(VS.100).aspx">ExpressionVistor class</a> public.  In .NET 3.5 it  was internal. </p>OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com0tag:blogger.com,1999:blog-8311529138225451610.post-31515238552844467092010-02-10T20:36:00.004+00:002010-02-10T20:55:48.035+00:00LINQ TO SQL and asynchronous IO<p>LINQ TO SQL doesn’t inherently support asynchronous IO. It is unfortunate, because non-blocking IO is a way to get great scalability on server side whether you do it directly with <a href="http://msdn.microsoft.com/en-us/library/ms228969.aspx">APM</a>,   <a href="http://msdn.microsoft.com/en-us/library/bb648753.aspx">CCR Iterators</a>  and APM adaptors, Jeffrey Richter's <a href="http://msdn.microsoft.com/en-us/magazine/cc546608.aspx">AsyncEnumerator</a>, <a href="http://blogs.msdn.com/pfxteam/archive/2009/06/30/9809774.aspx">TPL iterators'  integration</a> with APM,  or even <a href="http://blogs.msdn.com/maestroteam/archive/2009/05/23/yielding-with-asynchronous-methods.aspx">Axum</a> which natively recognizes APM methods.</p><p>Well there is actually a small hole through which we can use APM but in a very restrictive way. <br />
<font face="Con">The best  tool to experiment with L2S is <a href="http://www.linqpad.net/">LinqPAD</a>. Point it at Northwind database and try next “statement” query:</font></p><pre name="code" class="brush:c-sharp">var query = from e in Employees
where e.EmployeeID == 1
select e;
System.Data.Common.DbCommand cmd = this.GetCommand(query);
try
{
this.Connection.Open();
System.Data.Common.DbDataReader reader = cmd.ExecuteReader();
IEnumerable<Employees> employees = this.Translate<Employees>(reader);
//using results
Employees employee = employees.First();
employee.Dump();
}
finally
{
this.Connection.Close();
}
</pre><p>Instead of ExecuteReader we can use BeginExecuteReader and EndExecuteReader methods.  Wrap it in an convenient extension method for DataContext class and it seams we solved the problem. No.. Here is the list of problems we still have:</p><ul><li>eager loading of many-to-one and one-to-many relations of the  entity in question doesn’t work with our method</li>
<li>to fulfil some queries L2S has to send more than one SQL commands to the server, but DataContext.GetCommand returns only the first one</li>
<li>L2S doesn’t support asynchronous lazy loading of  many-to-one, one-to-many  and Link<T> based properties of an entity</li>
<li>L2S doesn’t support asynchronous compiled queries</li>
<li>besides querying L2S doesn’t support  submitting changes asynchronously</li>
<li>and don’t forget about transactions</li>
</ul><p>The list looks like a show stopper. But if you really  need to use APM with L2S and are not going to  eager load relations of the entity you want to query and sure that your  query will generate only one SQL command and won’t be surrounded by a transaction then you  should use it because parallelism and asynchronicity  is the way to performance and scalability.</p>OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com2tag:blogger.com,1999:blog-8311529138225451610.post-77541091304153115842010-02-07T03:42:00.001+00:002010-02-07T03:42:42.046+00:00.NET threads and stack memory<p>Normally when an unmanaged thread is created 1 MB of virtual address space is reserved and 4 KB (a page) is committed for its stack. <em>Reserved</em> here is an important word, because reservation allocates no physical storage but only well.. reserves a range of virtual memory.  It guaranties continuousness of the memory required for threads’ stacks. Allocation of the real storage (committing) happens as the stack grows, page by page.  <br />But as address space in a process is usually limited by 2 GB  the more threads you create the less virtual address space is left. For example, a thousand of threads can consume a half of the available  virtual memory of the process.  <br />But recently I have found that the things are different in the managed world. When a managed thread created  1 MB of memory is reserved <strong>and</strong> committed for its stack before it starts. There is no difference between a managed thread and an unmanaged one regarding virtual memory space consuming but the early committing  brings the whole system closer to the commit limit which is “the sum of physical memory and the sizes of the paging files”. <br />It is one more reason not to create threads if you can. Use ThreadPool, Tasks or  async IO instead.</p> <p>You can read more about<font color="#0080ff"> </font><a href="http://www.bluebytesoftware.com/blog/2007/03/10/TheCLRCommitsTheWholeStack.aspx"><font color="#0080ff">the reasons behind the CLR team’s decision</font></a>, <br />what <a href="http://blogs.technet.com/markrussinovich/archive/2008/11/17/3155406.aspx"><font color="#0080c0">Windows memory limits</font></a> are and how  <a href="http://blogs.technet.com/markrussinovich/archive/2009/07/08/3261309.aspx"><font color="#0080ff">Processes and Threads</font></a> consume memory.</p> OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com2tag:blogger.com,1999:blog-8311529138225451610.post-51613411616614506672010-01-27T18:17:00.000+00:002010-01-27T18:17:40.547+00:00Type initialization changes in .NET 4.0Corroborating <a href="http://meta.stackoverflow.com/questions/9134/jon-skeet-facts">his reputation</a> Jon Skeet has found <a href="http://msmvps.com/blogs/jon_skeet/archive/2010/01/26/type-initialization-changes-in-net-4-0.aspx">interesting changes in .NET 4.0 CLR</a>OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com0tag:blogger.com,1999:blog-8311529138225451610.post-60715320892124667442009-11-17T20:10:00.001+00:002009-11-17T20:10:33.720+00:00My God.. It’s Full Of Clouds!<p><a href="http://www.microsoft.com/windowsazure/">Microsoft</a>, <a href="http://aws.amazon.com/">Amazon</a>, <a href="http://code.google.com/appengine/">Google</a></p> <p><img alt="" src="http://farm4.static.flickr.com/3123/2396165921_06c6657f18.jpg" /></img></p> OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com1tag:blogger.com,1999:blog-8311529138225451610.post-63902435006437328372009-11-16T22:56:00.001+00:002009-11-18T14:22:22.477+00:00Spirals<p>In COM times IDispatch and IExpando interfaces were used to enable COM components written in strongly typed languages to enter the world of VisualBasic and JScript. </p> <p>Now DynamicObject and ExpandoObject let C# not only to interoperate with dynamic languages but to be dynamic itself. </p> OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com0tag:blogger.com,1999:blog-8311529138225451610.post-73977927060391343862009-11-11T00:25:00.001+00:002009-11-24T20:12:59.641+00:00Combining ASP.NET AJAX component descriptors<p>If you have worked with ASP.NET AJAX you know that every instance of ScripCompomentDescriptor created on server side generates startup javascript code at the end of the page. Something like this:</p><div style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; DIRECTION: ltr; COLOR: rgb(244,244,244); MAX-HEIGHT: 200px; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4pxfont-size:8pt;" id="codeSnippetWrapper" ><pre style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(244,244,244); MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; DIRECTION: ltr; BORDER-TOP-STYLE: none; BORDER-LEFT-STYLE: none; OVERFLOW: visiblefont-size:8pt;color:black;" id="codeSnippet" >Sys.Application.add_init(<span style="COLOR: rgb(0,0,255)">function</span>() {<br /> $create(AjaxControlToolkit.DynamicPopulateBehavior, {<span style="COLOR: rgb(0,96,128)">"PopulateTriggerID"</span>:<span style="COLOR: rgb(0,96,128)">"ctl00_SampleContent_Label1"</span>,<span style="COLOR: rgb(0,96,128)">"ServiceMethod"</span>:<span style="COLOR: rgb(0,96,128)">"GetHtml"</span>,<span style="COLOR: rgb(0,96,128)">"ServicePath"</span>:<span style="COLOR: rgb(0,96,128)">"/AJAX/AjaxControlToolkit/Samples/DynamicPopulate/DynamicPopulate.aspx"</span>,<span style="COLOR: rgb(0,96,128)">"UpdatingCssClass"</span>:<span style="COLOR: rgb(0,96,128)">"dynamicPopulate_Updating"</span>,<span style="COLOR: rgb(0,96,128)">"id"</span>:<span style="COLOR: rgb(0,96,128)">"dp1"</span>}, <span style="COLOR: rgb(0,0,255)">null</span>, <span style="COLOR: rgb(0,0,255)">null</span>, $get(<span style="COLOR: rgb(0,96,128)">"ctl00_SampleContent_Panel1"</span>));<br />});</pre><br /><br /><br /></div><br /><br /><p>And the more instances you create the bigger your page size. You should not bother if there are only a few components on your page or when you turn on <a href="http://blogs.iis.net/ksingla/archive/2006/06/13/changes-to-compression-in-iis7.aspx">compression for dynamic content</a>. But if dynamic compression is not an option declaration of dozens of ajax script controls or behaviours can easily twice the page size. ASP.NET AJAX doesn’t have a built-in way of combining multiple ajax component declarations so I had to resort to our old friend Reflection.</p><p>All combining logic I’ve put into the class derived from System.Web.UI.ScriptManager. Classes ScriptControlBase and ExtenderControlBase intercept script descriptors registration and register their in our special ScriptManager. The ScriptManager at last possible point, before ASP.NET renders registered startup scripts, combines registered script descriptors, generates necessary javascript code and registers it as startup script. That’s it.</p><br /><br /><p>Combined script controls (or behaviours) creation looks like this:</p><p></p><div style="BORDER-BOTTOM: silver 1px solid; TEXT-ALIGN: left; BORDER-LEFT: silver 1px solid; PADDING-BOTTOM: 4px; LINE-HEIGHT: 12pt; PADDING-LEFT: 4px; WIDTH: 97.5%; PADDING-RIGHT: 4px; DIRECTION: ltr; COLOR: rgb(244,244,244); MAX-HEIGHT: 200px; OVERFLOW: auto; BORDER-TOP: silver 1px solid; CURSOR: text; BORDER-RIGHT: silver 1px solid; PADDING-TOP: 4pxfont-size:8pt;" id="codeSnippetWrapper" ><br /><pre style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: rgb(244,244,244); MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; DIRECTION: ltr; BORDER-TOP-STYLE: none; BORDER-LEFT-STYLE: none; OVERFLOW: visiblefont-size:8pt;color:black;" id="codeSnippet" >Sys.Application.add_init(<span style="COLOR: rgb(0,0,255)">function</span>()<br />{<br /> $createMultiple(TestWeb.UserCard,[<span style="COLOR: rgb(0,96,128)">'ctl03_item'</span>,<span style="COLOR: rgb(0,96,128)">'ctl04_item'</span>,<span style="COLOR: rgb(0,96,128)">'ctl05_item'</span>,<span style="COLOR: rgb(0,96,128)">'ctl06_item'</span>,<span style="COLOR: rgb(0,96,128)">'ctl07_item'</span>,<span style="COLOR: rgb(0,96,128)">'ctl08_item'</span>,<span style="COLOR: rgb(0,96,128)">'ctl09_item'</span>,<span style="COLOR: rgb(0,96,128)">'ctl10_item'</span>],<br /> {serviceMethod:[<span style="COLOR: rgb(0,96,128)">"GetUserInfo"</span>],servicePath:[<span style="COLOR: rgb(0,96,128)">"/CombinedDescriptorsExample/Services/Users/GetUserInfo.svc"</span>],userId:[123,0,1,[1,6],2,[2,7],3,3,4,4,0,5]},<br /> <span style="COLOR: rgb(0,0,255)">null</span>,<br /> <span style="COLOR: rgb(0,0,255)">null</span>);<br />});<br /><br /></pre><br /><br /><br /></div><p></p><p><br />$createMultiple is a function similar to $create, but it creates.. multiple components of a script control/behaviour type.<br /><br />It takes 5 arguments: name of a script control type to create, array of html elements’ ids, object describing properties setters, events setter and component references setters. Each setter has the name of property (event, component ref) and the value which is always an array. If all script control instances have the same value for the property then the value-array will have only one element, otherwise it is an array where each odd elements is a value and it’s next element is an index (or array of indexes) in html elements array.</p><p>Combining is controlled by ScriptManager’s property CombineScriptDescriptors.</p><p>A few steps and you can use it with AjaxControlToolkit:</p><ul><li>Add reference to CombinedDescriptors.dll (or whatever dll you’ve put the classes in) to AjaxControlToolkit project </li><br /><li>Derive ExtenderControlBase class (AjaxControlToolkit\ExtenderBase\ExtenderControlBase.cs) from my ExtenderControlBase class </li><br /><li>Derive ScriptControlBase class (AjaxControlToolkit\ExtenderBase\ScriptControlBase .cs) from my ScriptControlBase class </li><br /><li>Derive ToolkitScriptManager class (AjaxControlToolkit\ToolkitScriptManager\ToolkitScriptManager.cs) from my ToolkitScriptManager class </li></ul><p>If you want to test it with AjaxControlToolkit SampleWebSite , change the web site’s trust level to Full (<system.web><trust level="Full"/>), add CombineScriptDescriptors=”true” to the declaration of the ajaxToolkit:ToolkitScriptManager in \App_Themes\SampleSiteTheme\ToolkitScriptManager.skinfile and add the code for $createMultiple function to Common.js in AjaxControlToolkit project.<br /></p><p>As Reflection to private types and members is used it may not work in next versions of ASP.NET.<br /><br />Now it works in .NET 3.5 and .NET 4 Beta 2.</p><a href="http://cid-e2f51563c5286879.skydrive.live.com/self.aspx/SharedFiles/CombinedDescriptors.zip"><span style="COLOR: rgb(0,128,255)">CombinedScriptDescriptors.zip</span></a>OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com1tag:blogger.com,1999:blog-8311529138225451610.post-49267366142277711412009-10-08T18:57:00.001+01:002009-10-08T19:04:42.491+01:00HttpApplication.Init method<p>HttpApplication class has a method  Init. It is virtual and called by ASP.NET runtime when initializing of a HttpApplication is almost finished i.e. all modules have been initialized and all event handlers in the Global.asax have been registered. It is also lat time when you can attach handlers to the HttpApplication events. The method is quite handy if you want to place your custom HttpApplication in a separate  class library project.</p> <p>I’ve used it to see which modules are registered for an application and subscribed for the application events,  and how much time each stage of the request processing takes. The output looks like the following. </p> <p>See <a href="http://cid-e2f51563c5286879.skydrive.live.com/self.aspx/SharedFiles/PipelineInfo.zip"><u>attached file</u></a></p> <table style="border-collapse: collapse; font-family: tahoma; font-size: 12px" border="1" cellspacing="5"><tbody> <tr> <th><font size="1">Modules</font></th> <th><font size="1">HttpApplication event handlers</font></th> <th><font size="1">Timing</font></th> </tr> <tr valign="top"> <td> <div title="System.Web.Caching.OutputCacheModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"><font size="1">OutputCache</font></div> <div title="System.Web.SessionState.SessionStateModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"><font size="1">Session</font></div> <div title="System.Web.Security.WindowsAuthenticationModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"><font size="1">WindowsAuthentication</font></div> <div title="System.Web.Security.FormsAuthenticationModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"><font size="1">FormsAuthentication</font></div> <div title="System.Web.Security.PassportAuthenticationModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"><font size="1">PassportAuthentication</font></div> <div title="System.Web.Security.RoleManagerModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"><font size="1">RoleManager</font></div> <div title="System.Web.Security.UrlAuthorizationModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"><font size="1">UrlAuthorization</font></div> <div title="System.Web.Security.FileAuthorizationModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"><font size="1">FileAuthorization</font></div> <div title="System.Web.Security.AnonymousIdentificationModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"><font size="1">AnonymousIdentification</font></div> <div title="System.Web.Profile.ProfileModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"><font size="1">Profile</font></div> <div title="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"><font size="1">ErrorHandlerModule</font></div> <div title="System.ServiceModel.Activation.HttpModule, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"><font size="1">ServiceModel</font></div> <div title="System.Web.Security.DefaultAuthenticationModule, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"><font size="1">DefaultAuthentication</font></div> </td> <td><font size="1"><b>BeginRequest</b> <br />  ASP.<span style="color: blue">global_asax</span>.<i>Application_BeginRequest</i> <br /> <br /><b>AuthenticateRequest</b> <br />  System.Web.Security.<span style="color: blue">WindowsAuthenticationModule</span>.<i>OnEnter</i> <br />  System.Web.Security.<span style="color: blue">PassportAuthenticationModule</span>.<i>OnEnter</i> <br /> <br /><b>DefaultAuthentication</b> <br />  System.Web.Security.<span style="color: blue">DefaultAuthenticationModule</span>.<i>OnEnter</i> <br /> <br /><b>PostAuthenticateRequest</b> <br />  System.ServiceModel.Activation.<span style="color: blue">HttpModule</span>.<i>ProcessRequest</i> <br /> <br /><b>AuthorizeRequest</b> <br />  System.Web.Security.<span style="color: blue">UrlAuthorizationModule</span>.<i>OnEnter</i> <br />  System.Web.Security.<span style="color: blue">FileAuthorizationModule</span>.<i>OnEnter</i> <br /> <br /><b>ResolveRequestCache</b> <br />  System.Web.Caching.<span style="color: blue">OutputCacheModule</span>.<i>OnEnter</i> <br /> <br /><b>AcquireRequestState</b> <br />  System.Web.Profile.<span style="color: blue">ProfileModule</span>.<i>OnEnter</i> <br /> <br /><b>AcquireRequestStateAsync</b>  <br />   System.Web.SessionState.<span style="color: blue">SessionStateModule</span>.<i>BeginAcquireState</i>  <br />   System.Web.SessionState.<span style="color: blue">SessionStateModule</span>.<i>EndAcquireState</i> <br /> <br /><b>ReleaseRequestState</b> <br />  System.Web.SessionState.<span style="color: blue">SessionStateModule</span>.<i>OnReleaseState</i> <br /> <br /><b>UpdateRequestCache</b> <br />  System.Web.Caching.<span style="color: blue">OutputCacheModule</span>.<i>OnLeave</i> <br /> <br /><b>EndRequest</b> <br />  System.Web.SessionState.<span style="color: blue">SessionStateModule</span>.<i>OnEndRequest</i> <br />  System.Web.Security.<span style="color: blue">PassportAuthenticationModule</span>.<i>OnLeave</i> <br />  System.Web.Profile.<span style="color: blue">ProfileModule</span>.<i>OnLeave</i> </font></td> <td> <table style="border-collapse: collapse; font-family: tahoma; font-size: 12px" border="1" cellspacing="5"><tbody> <tr> <th rowspan="2"><font size="1">Stage</font></th> <th colspan="2"><font size="1">From BeginRequest</font></th> <th colspan="2"><font size="1">From Last Stage</font></th> </tr> <tr> <th><font size="1">ticks</font></th> <th><font size="1">ms</font></th> <th><font size="1">ticks</font></th> <th><font size="1">ms</font></th> </tr> <tr> <td><font size="1">BeginRequest</font></td> <td><font size="1"></font></td> <td><font size="1"></font></td> <td><font size="1"></font></td> <td><font size="1"></font></td> </tr> <tr> <td><font size="1">AuthenticateRequest</font></td> <td><font size="1">13</font></td> <td><font size="1">.01</font></td> <td><font size="1">13</font></td> <td><font size="1">.01</font></td> </tr> <tr> <td><font size="1">DefaultAuthentication</font></td> <td><font size="1">20</font></td> <td><font size="1">.01</font></td> <td><font size="1">7</font></td> <td><font size="1"></font></td> </tr> <tr> <td><font size="1">PostAuthenticateRequest</font></td> <td><font size="1">45</font></td> <td><font size="1">.02</font></td> <td><font size="1">25</font></td> <td><font size="1">.01</font></td> </tr> <tr> <td><font size="1">AuthorizeRequest</font></td> <td><font size="1">79</font></td> <td><font size="1">.04</font></td> <td><font size="1">34</font></td> <td><font size="1">.02</font></td> </tr> <tr> <td><font size="1">PostAuthorizeRequest</font></td> <td><font size="1">84</font></td> <td><font size="1">.04</font></td> <td><font size="1">5</font></td> <td><font size="1"></font></td> </tr> <tr> <td><font size="1">ResolveRequestCache</font></td> <td><font size="1">91</font></td> <td><font size="1">.04</font></td> <td><font size="1">7</font></td> <td><font size="1"></font></td> </tr> <tr> <td><font size="1">PostResolveRequestCache</font></td> <td><font size="1">96</font></td> <td><font size="1">.05</font></td> <td><font size="1">5</font></td> <td><font size="1"></font></td> </tr> <tr> <td><font size="1">PostMapRequestHandler</font></td> <td><font size="1">179</font></td> <td><font size="1">.08</font></td> <td><font size="1">83</font></td> <td><font size="1">.04</font></td> </tr> <tr> <td><font size="1">AcquireRequestState</font></td> <td><font size="1">214</font></td> <td><font size="1">.1</font></td> <td><font size="1">35</font></td> <td><font size="1">.02</font></td> </tr> <tr> <td><font size="1">PostAcquireRequestState</font></td> <td><font size="1">220</font></td> <td><font size="1">.1</font></td> <td><font size="1">6</font></td> <td><font size="1"></font></td> </tr> <tr> <td><font size="1">PreRequestHandlerExecute</font></td> <td><font size="1">225</font></td> <td><font size="1">.11</font></td> <td><font size="1">5</font></td> <td><font size="1"></font></td> </tr> <tr> <td><font size="1">PostRequestHandlerExecute</font></td> <td><font size="1">309</font></td> <td><font size="1">.15</font></td> <td><font size="1">84</font></td> <td style="background-color: yellow"><font size="1">.04</font></td> </tr> <tr> <td><font size="1">ReleaseRequestState</font></td> <td><font size="1">318</font></td> <td><font size="1">.15</font></td> <td><font size="1">9</font></td> <td><font size="1"></font></td> </tr> <tr> <td><font size="1">PostReleaseRequestState</font></td> <td><font size="1">323</font></td> <td><font size="1">.15</font></td> <td><font size="1">5</font></td> <td><font size="1"></font></td> </tr> <tr> <td><font size="1">UpdateRequestCache</font></td> <td><font size="1">331</font></td> <td><font size="1">.16</font></td> <td><font size="1">8</font></td> <td><font size="1"></font></td> </tr> <tr> <td><font size="1">PostUpdateRequestCache</font></td> <td><font size="1">336</font></td> <td><font size="1">.16</font></td> <td><font size="1">5</font></td> <td><font size="1"></font></td> </tr> <tr> <td><font size="1">EndRequest</font></td> <td><font size="1">344</font></td> <td><font size="1">.16</font></td> <td><font size="1">8</font></td> <td><font size="1"></font></td> </tr> </tbody></table> </td> </tr> </tbody></table> OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com0tag:blogger.com,1999:blog-8311529138225451610.post-68132354069605534492009-09-29T18:38:00.001+01:002009-09-29T19:02:55.278+01:00Perfomant Eval<p>Add the following code to your base page and base user control.</p> <div> <pre style="border-style: none; margin: 0em; padding: 0px; overflow: visible; text-align: left; line-height: 12pt; background-color: rgb(244, 244, 244); width: 100%; direction: ltr;font-family:'Courier New',courier,monospace;font-size:8pt;color:black;" id="codeSnippet"><span style="color: rgb(0, 0, 255);">protected</span> TItem Eval<TItem>()<br />{<br /> <span style="color: rgb(0, 0, 255);">return</span> (TItem)GetDataItem();<br />}</pre></div><br /><div>Register the namespace of your entities in the import directive or in web.config an use the new Eval instead of the standart one. You get speed and intellisense. </div><br /><div><%= Eval<Person>().Name %></div><br /><br /><div></div>OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com0tag:blogger.com,1999:blog-8311529138225451610.post-73377849110814941822009-09-29T17:58:00.006+01:002011-05-04T15:33:50.314+01:00ASP.NET white space cleaning with no runtime cost<p>I think many asp.net developer looking at the markup generated by a ASP.NET pages wanted to get rid of those white space without necessity of changing nice indents of their pages and user controls.</p> <p>Existing solutions of the problem based on one of the two methods: implementing a cleaning <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.htmltextwriter.aspx">HtmlTextWriter</a> and returning an instance of it in <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.page.createhtmltextwriter.aspx">Page.CreateHtmlTextWriter</a>  override, or implementing a cleaning Stream and setting the <a href="http://msdn.microsoft.com/en-us/library/system.web.httpresponse.filter.aspx">HttpResponse.Filter</a> property to an instance of the Stream.</p> <p>But both methods incur runtime performance costs (I am going to show in a next post why that happens). <br />In my solution white space in generated markup removed at pages’ compile time, so that at runtime it looks as if white space was removed manually by the developers.</p> <p>One would say that it is not actually a solution because it relies on Reflection and depends on implementation details of the particular version of ASP.NET. I would answer – Thanks God that I haven’t had to resort to the help of unsafe code and pointer arithmetic :)</p> <p>Let’s start with the beginning. When ASP.NET generates C#/VB code for an ASP.NET page it uses <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.controlbuilder.aspx">ControlBuilders</a> representing almost every entity (except text) in the page tree. Besides holding information about the part of the page it represents, they let controls creators change different aspects of generated code (in .NET 3.5 SP1 including ability to change generated CodeDom). For regular pages control builders exist at compile time only, but in “no compile” pages they actually create at runtime the parts they are responsible for. <br />As a page at runtime time is a tree of controls, server side code and text , the page at compile time is a tree of control builders. Each builder holds a collection of subbuilders in an ArrayList. Untyped collection is used because literal strings are placed in subbuilders collection not being wrapped in control builders.</p> <p>Knowing all that we can traverse control builder tree from the root, iterate over the each builder’s subbuilders collection, find instances of strings and replace them with cleaned versions.</p> <p>Getting access to the root control builder at right time was not easy. I tried different ways but all had shortcomings or inconveniences. But then examining .NET sources by Reflector I found the perfect way - <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.pageparserfilter.aspx">PageParserFilter</a>. PageParserFilter “provides an abstract base class for a page parser filter that is used by the ASP.NET parser to determine whether an item is allowed in the page at parse time”. This ability (among other useful things) was added to ASP.NET 2.0 as a result of cooperation between ASP.NET team and SharePoint team poring their product to ASP.NET. The only method of interest now is <a href="http://msdn.microsoft.com/ru-ru/library/system.web.ui.pageparserfilter.parsecomplete.aspx">PageParserFilter.ParseComplete(ControlBuilder rootBuilder)</a> and it is the right place and time for accessing the root builder. The remaining part is easy. See the attached web site project. Compile the classes in a dll, drop it in  Bin folder, small change to web.config and voilà– ASP.NET white space cleaning with no runtime cost!</p> <p>BTW the code has been working for a few projects in the wild with no problems.</p> <p><strong>UPDATE (17 Aug 2010):  </strong>I’ve <strong> </strong>updated the sample to work with MVC 2  and MVC 3 Preview 1 (including support of Razor view engine).</p> <p><strong>UPDATE (9 Oct 2010):  </strong>Now it works with MVC 3 Beta.</p> <p><strong>UPDATE (4 May 2011):  </strong>Finally with MVC 3 RTW</p> <p><iframe style="padding-bottom: 0px; background-color: #fcfcfc; padding-left: 0px; width: 98px; padding-right: 0px; height: 115px; padding-top: 0px" title="Preview" marginheight="0" src="http://cid-e2f51563c5286879.office.live.com/embedicon.aspx/SharedFiles/WhiteSpaceCleaning.zip" frameborder="0" marginwidth="0" scrolling="no"></iframe></p> OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com372tag:blogger.com,1999:blog-8311529138225451610.post-14735049716008031612009-09-19T16:40:00.001+01:002009-09-19T16:58:52.892+01:00Tuple as value type<p>There is new  type in .NET BCL - Tuple.  What is it is explained in the article <a href="http://msdn.microsoft.com/en-us/magazine/dd942829.aspx">CLR Inside Out: Building Tuple</a>. The BCL team has decided it to be a reference  type. The reason is well motivated but I think there are cases when having a tuple as a valuetype is beneficial. One of the such case is when you use a tuple as a key in a dictionary. Creating an object in the heap just to get a value from the dictionary or to find out whether it exists would be.. at least not proper use of GC. Especialy when you do it in a tight loop.</p> <p>Honestly, using keys in dictionaries were the only reason I have used tuples before. Now not to interfere with with Tuple from BCL and to conform to it I have renamed my value type Tuple to TupleValue, properties Value1, Value to Item1, Item2, factory method New to Create.</p> <p>Implementation is trivial. The only important detail is IEquatable<T>.  A value type not implementing this interface is boxed when used as a key in a dictionary.</p> <p> </p> <div id="codeSnippetWrapper"> <pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">using</span> System;<br /><span style="color: #0000ff">using</span> System.Collections.Generic;<br /><br /><span style="color: #0000ff">namespace</span> Omari<br />{<br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">class</span> TupleValue<br /> {<br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> TupleValue<TItem1, TItem2> Create<TItem1, TItem2>(TItem1 item1, TItem2 item2)<br /> {<br /> <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> TupleValue<TItem1, TItem2>(item1, item2);<br /> }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> TupleValue<TItem1, TItem2, TItem3> Create<TItem1, TItem2, TItem3>(TItem1 item1, TItem2 item2, TItem3 item3)<br /> {<br /> <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> TupleValue<TItem1, TItem2, TItem3>(item1, item2, item3);<br /> }<br /> }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">struct</span> TupleValue<TItem1, TItem2> : IEquatable<TupleValue<TItem1, TItem2>><br /> {<br /> TItem1 _item1;<br /> TItem2 _item2;<br /><br /> <span style="color: #0000ff">public</span> TItem1 Item1 { get { <span style="color: #0000ff">return</span> _item1; } }<br /> <span style="color: #0000ff">public</span> TItem2 Item2 { get { <span style="color: #0000ff">return</span> _item2; } }<br /><br /> <span style="color: #0000ff">public</span> TupleValue(TItem1 item1, TItem2 item2)<br /> {<br /> _item1 = item1;<br /> _item2 = item2;<br /> }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> Equals(TupleValue<TItem1, TItem2> other)<br /> {<br /> <span style="color: #0000ff">return</span> EqualityComparer<TItem1>.Default.Equals(_item1, other.Item1) &&<br /> EqualityComparer<TItem2>.Default.Equals(_item2, other.Item2);<br /> }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> Equals(<span style="color: #0000ff">object</span> obj)<br /> {<br /> <span style="color: #0000ff">if</span> (obj.GetType() != <span style="color: #0000ff">typeof</span>(TupleValue<TItem1, TItem2>))<br /> <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;<br /><br /> <span style="color: #0000ff">return</span> Equals((TupleValue<TItem1, TItem2>)obj);<br /> }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span> GetHashCode()<br /> {<br /> <span style="color: #0000ff">return</span> EqualityComparer<TItem1>.Default.GetHashCode(_item1) ^<br /> EqualityComparer<TItem2>.Default.GetHashCode(_item2);<br /> }<br /> }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">struct</span> TupleValue<TItem1, TItem2, TItem3> : IEquatable<TupleValue<TItem1, TItem2, TItem3>><br /> {<br /> TItem1 _item1;<br /> TItem2 _item2;<br /> TItem3 _item3;<br /><br /> <span style="color: #0000ff">public</span> TItem1 Item1 { get { <span style="color: #0000ff">return</span> _item1; } }<br /> <span style="color: #0000ff">public</span> TItem2 Item2 { get { <span style="color: #0000ff">return</span> _item2; } }<br /> <span style="color: #0000ff">public</span> TItem3 Item3 { get { <span style="color: #0000ff">return</span> _item3; } }<br /><br /> <span style="color: #0000ff">public</span> TupleValue(TItem1 item1, TItem2 item2, TItem3 item3)<br /> {<br /> _item1 = item1;<br /> _item2 = item2;<br /> _item3 = item3;<br /> }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> Equals(TupleValue<TItem1, TItem2, TItem3> other)<br /> {<br /> <span style="color: #0000ff">return</span> EqualityComparer<TItem1>.Default.Equals(_item1, other.Item1) &&<br /> EqualityComparer<TItem2>.Default.Equals(_item2, other.Item2) &&<br /> EqualityComparer<TItem3>.Default.Equals(_item3, other.Item3);<br /> }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> Equals(<span style="color: #0000ff">object</span> obj)<br /> {<br /> <span style="color: #0000ff">if</span> (obj.GetType() != <span style="color: #0000ff">typeof</span>(TupleValue<TItem1, TItem2, TItem3>))<br /> <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;<br /><br /> <span style="color: #0000ff">return</span> Equals((TupleValue<TItem1, TItem2, TItem3>)obj);<br /> }<br /><br /> <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span> GetHashCode()<br /> {<br /> <span style="color: #0000ff">return</span> EqualityComparer<TItem1>.Default.GetHashCode(_item1) ^<br /> EqualityComparer<TItem2>.Default.GetHashCode(_item2) ^<br /> EqualityComparer<TItem3>.Default.GetHashCode(_item3);<br /> }<br /> }<br />}</pre><br /><br /> <br /></div><br /><a href="http://11011.net/software/vspaste"></a> OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com2tag:blogger.com,1999:blog-8311529138225451610.post-92144837050462539952009-07-27T19:21:00.000+01:002009-07-27T19:53:34.405+01:00overVARingI hate such code:<br /><br /><span style="color: rgb(0, 0, 255);">var</span> commandLine = SomeClass.SomeProperty;<br /><span style="color: rgb(0, 0, 255);">var</span> splitCommandLine = SomeClass.SomeMethod();<br /><br />It tells nothing about the types of the variables.<br />Using <span style="color: rgb(51, 102, 255);">var </span>keyword is tolerable when you explicitly pronounce the type of the expression on the right side of the assignment or when the contract (your expectations) of the variable is important, but not its exact type.<br /><br />var logger = new FastLogger();<br />var amout = Convert.ToInt32(input);<br />var logger = (ILogger)state; //or <span style="font-style: italic;">as ILogger</span><br />var query = from person in db.Persons where person.Age > 65 select person;OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com0tag:blogger.com,1999:blog-8311529138225451610.post-65318120048831901422009-06-14T13:35:00.001+01:002009-06-14T13:44:31.214+01:00C# 4.0 lock statement implementation<p>We all know what C# <span style="color:#0000ff;">lock</span> statement turns into.<br /></p><p>Giving</p><pre style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; DIRECTION: ltr; BORDER-TOP-STYLE: none; BORDER-LEFT-STYLE: none; OVERFLOW: visiblefont-size:8pt;color:black;" id="codeSnippet" ><span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)<br />{<br /> <span style="color:#0000ff;">lock</span> (s_lock)<br /> {<br /> }<br />}</pre><br />We get<br /><br /><pre style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; DIRECTION: ltr; BORDER-TOP-STYLE: none; BORDER-LEFT-STYLE: none; OVERFLOW: visiblefont-size:8pt;color:black;" id="codeSnippet" ><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)<br />{<br /> <span style="color:#0000ff;">object</span> CS$2$0000;<br /> Monitor.Enter(CS$2$0000 = s_lock);<br /> <span style="color:#0000ff;">try</span><br /> {<br /> }<br /> <span style="color:#0000ff;">finally</span><br /> {<br /> Monitor.Exit(CS$2$0000);<br /> }<br />}</pre><br /><br />But in C# 4 <span style="color:#0000ff;">lock</span> statement uses <a href="http://msdn.microsoft.com/en-us/library/dd289498(VS.100).aspx">new overload of Monitor.Enter</a> and enters guarded region <u>inside</u> try block<u>.</u><br /><br /><pre style="BORDER-BOTTOM-STYLE: none; TEXT-ALIGN: left; PADDING-BOTTOM: 0px; LINE-HEIGHT: 12pt; BORDER-RIGHT-STYLE: none; BACKGROUND-COLOR: #f4f4f4; MARGIN: 0em; PADDING-LEFT: 0px; WIDTH: 100%; PADDING-RIGHT: 0px; DIRECTION: ltr; BORDER-TOP-STYLE: none; BORDER-LEFT-STYLE: none; OVERFLOW: visiblefont-size:8pt;color:black;" id="codeSnippet" ><span style="color:#0000ff;">private</span> <span style="color:#0000ff;">static</span> <span style="color:#0000ff;">void</span> Main(<span style="color:#0000ff;">string</span>[] args)<br />{<br /> <span style="color:#0000ff;">object</span> CS$2$0000;<br /> <span style="color:#0000ff;">bool</span> <>s__LockTaken0 = <span style="color:#0000ff;">false</span>;<br /> <span style="color:#0000ff;">try</span><br /> {<br /> Monitor.Enter(CS$2$0000 = s_lock, <span style="color:#0000ff;">ref</span> <>s__LockTaken0);<br /> }<br /> <span style="color:#0000ff;">finally</span><br /> {<br /> <span style="color:#0000ff;">if</span> (<>s__LockTaken0)<br /> {<br /> Monitor.Exit(CS$2$0000);<br /> }<br /> }<br />}</pre><br />If you want to know what it was made for read <a href="http://www.bluebytesoftware.com/blog/PermaLink,guid,d9ff204a-a8a5-400e-bcbc-dedb90a7d11a.aspx">this exhaustive Joe Duffy’s explanation</a>.<br />If you have thought that the window between Monitor.Enter and try block is too short to be considered read <a href="http://blogs.msdn.com/junfeng/archive/2007/07/30/lock-object-and-threadabortexception.aspx">this case</a>.OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com0tag:blogger.com,1999:blog-8311529138225451610.post-91011965321578027292009-06-05T15:32:00.001+01:002009-07-05T18:50:26.098+01:00New SOS commands in CLR 4.0 Beta1<p>I seems that SOS will have several new commands in CLR 4.0.</p> <p>How did I find that out?  I just retrieved SOS help file (it’s just a binary resource in SOS.dll) from .NET 2.0 SOS   and .NET 4.0  SOS and fed the files to WinDiff.  And that is what we have.</p> <p><strong>New commands</strong>: <br /><em><u>Examining code and stacks</u> <br /></em>ThreadState</p> <p><em><u>Diagnostic Utilities <br /></u></em>VerifyObj <br />FindRoots <br />HeapStat <br />GCWhere <br />ListNearObj <br />FinalizeQueue <br />AnalyzeOOM</p> <p><em><u>Examining the GC history <br /></u></em>HistInit <br />HistStats <br />HistRoot <br />HistObj <br />HistObjFind <br />HistClear</p> <p><strong>New ar</strong><strong>guments for two commands:</strong> <br />!ObjSize [<Object address>] | <strong>[-aggregate] [-stat] </object> <br /></strong>!FinalizeQueue [-detail]<strong> | [-allReady] [-short] </strong></p> <p><strong>Supporting DML: <br /></strong>>> Does SOS support DML? <br />Yes.  SOS respects the .prefer_dml option in the debugger.  If this setting is <br />turned on, then SOS will output DML by default.  Alternatively, you may leave <br />it off and add /D to the beginning of a command to get DML based output for it. <br />Not all SOS commands support DML output.</p> <hr /> <p><a href="http://cid-e2f51563c5286879.skydrive.live.com/self.aspx/SharedFiles/SOS|_2|_help.txt">SOS_2_help.txt</a> <br /><a href="http://cid-e2f51563c5286879.skydrive.live.com/self.aspx/SharedFiles/SOS|_4|_help.txt">SOS_4_help.txt</a></p> OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com0tag:blogger.com,1999:blog-8311529138225451610.post-9280654612577102782009-06-04T21:49:00.002+01:002012-01-06T00:33:32.172+00:00Introduction<p>Hi everybody. I created this blog to share my .NET findings and thoughts. It is also a way to learn English,  so if you find my English really weird remember what  the blog's name  is stand for :)</p>OmariOhttp://www.blogger.com/profile/15742544005513676789noreply@blogger.com0