<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7160136240184027073</id><updated>2011-12-26T04:02:38.206-08:00</updated><title type='text'>Vitaliy Liptchinsky's .NET Framework blog</title><subtitle type='html'>This blog is dedicated to .NET Framework technologies.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>13</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7160136240184027073.post-5069374318627293165</id><published>2010-09-15T12:30:00.000-07:00</published><updated>2010-09-15T12:38:51.769-07:00</updated><title type='text'>Dataflow programming in F# and C#</title><content type='html'>&lt;div&gt;&lt;br /&gt;&lt;!-- Start Article --&gt;&lt;br /&gt;&lt;span id="ArticleContent"&gt;&lt;br /&gt;&lt;h2&gt;Introduction into dataflow programming &lt;/h2&gt;&lt;p&gt;&lt;br /&gt;&lt;br /&gt;What is dataflow programming all about? In classical imperative programming a program is basically set of&lt;br /&gt;operations working with mutable state thus effectively hiding data paths. Dataflow programming is more like a&lt;br /&gt;series of workers/robots on an assembly line, who execute only when input material arrives. Imperative programming&lt;br /&gt;style can introduce non-determinism in case of concurrent execution (multithreading) without proper&lt;br /&gt;synchronization. In dataflow programming program execution depends on the actual data, or on the data availability&lt;br /&gt;to be precise. Dataflow programming yields completely deterministic programs.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Let’s introduce the concept of dataflow variable which is one of the main concepts of dataflow programming.&lt;br /&gt;Dataflow variable can have two possible states: bound (assigned a value) or unbound (no value has been yet&lt;br /&gt;assigned). Whenever a thread tries to read the value of unbound dataflow variable it gets blocked until some other&lt;br /&gt;thread bounds the variable. Dataflow variable can be bound only once, successive tries to bind the variable will&lt;br /&gt;fail. So, what is dataflow programming? With dataflow variable one can also build blocking queues and streams.&lt;br /&gt;Actor model can be implemented using such blocking queues.&lt;br /&gt;&lt;br /&gt;Basically, you can get more information on dataflow programming from this Wikipedia &lt;a href="http://en.wikipedia.org/wiki/Dataflow_programming"&gt;article&lt;/a&gt;. Also there is nice article in Groovy &lt;a href="http://www.gpars.org/guide/guide/7.%20Dataflow%20Concurrency.html"&gt;GPars guide&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;h2&gt;Overview of the article&lt;/h2&gt;&lt;br /&gt;This article presents basic implementations of dataflow variable in both C# and F#. Also article demonstrates&lt;br /&gt;examples of dataflow programming in C# using futures. The best effect of dataflow programming is achieved in&lt;br /&gt;programming languages that follow declarative model principles. In our case C# is imperative language and&lt;br /&gt;programming in a dataflow style requires developers to be self-disciplined. Surprisingly, but F# being considered&lt;br /&gt;to be a functional programming language, and therefore following declarative programming paradigm, also enables&lt;br /&gt;developers to program in an imperative programming way (via mutable keyword). Adding dataflow variables to C# and&lt;br /&gt;F# does not make them automatically dataflow programming languages, because there is still no necessary syntactic&lt;br /&gt;sugar and language support.&lt;br /&gt;&lt;br /&gt;Clojure is one of the most popular modern languages that enable dataflow programming. Clojure supports dataflow&lt;br /&gt;programming through premises. It is also possible to do a dataflow programming in other popular languages like&lt;br /&gt;Groovy, Scala, Ruby using open-source libraries like GPars for Groovy, but all those languages provide no syntactic&lt;br /&gt;support for dataflow variables. As a genuine dataflow programming language I would distinguish Oz programming&lt;br /&gt;language which treats all variables as dataflow variables: reader trying to read an unbound/uninitialized variable&lt;br /&gt;will be blocked until variable is bound/initialized. One one hand it saves us from fameous NullReferenceException&lt;br /&gt;exceptions, but on the other hand it can introduce program hangs.&lt;br /&gt;&lt;br /&gt;First I will present implementations in C# and F# and later will dig into the thread synchronization details.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Dataflow variables in C#&lt;/h2&gt;&lt;br /&gt;Let’s start with the simple example of how to use a dataflow variable in C#.&lt;br /&gt;&lt;code&gt;&lt;/code&gt;&lt;pre&gt;var variable = new DataflowVariable&lt;int&gt;(); //create variable&lt;br /&gt;variable.Bind(value); //bind variable&lt;br /&gt;int value = 1000 + variable;//read variable&lt;br /&gt;&lt;/int&gt;&lt;/pre&gt;&lt;br /&gt;C# is not very extensible when it comes to operator overloading (as you later see in F# implementation) and this is&lt;br /&gt;the reason we are using Bind method here. Actually this is a matter of taste – whether to use operators when&lt;br /&gt;working with dataflow variables or simply properties/functions, but as per me operators look more naturally. What I&lt;br /&gt;love about C# is implicit conversion operators.&lt;br /&gt;Now the code itself:&lt;br /&gt;&lt;code&gt;&lt;/code&gt;&lt;pre&gt;public class DataflowVariable&lt;t&gt;&lt;br /&gt;  {&lt;br /&gt;      private readonly object syncLock = new object();&lt;br /&gt;      private volatile bool isInitialized = false;&lt;br /&gt;      private volatile object value;&lt;br /&gt;&lt;br /&gt;      private T Value&lt;br /&gt;      {&lt;br /&gt;          get&lt;br /&gt;&lt;br /&gt;          {&lt;br /&gt;              if(!isInitialized)&lt;br /&gt;              {&lt;br /&gt;                  lock(syncLock)&lt;br /&gt;                  {&lt;br /&gt;                      while(!isInitialized)&lt;br /&gt;                          Monitor.Wait(syncLock);&lt;br /&gt;                  }&lt;br /&gt;              }&lt;br /&gt;              return (T)value;&lt;br /&gt;          }&lt;br /&gt;          set&lt;br /&gt;          {&lt;br /&gt;              lock (syncLock)&lt;br /&gt;              {&lt;br /&gt;                  if (isInitialized)&lt;br /&gt;                      throw new System.InvalidOperationException("Dataflow variable can be set only&lt;br /&gt;&lt;br /&gt;once.");&lt;br /&gt;                  else&lt;br /&gt;&lt;br /&gt;                  {&lt;br /&gt;                      this.value = value;&lt;br /&gt;                      isInitialized = true;&lt;br /&gt;                      Monitor.PulseAll(syncLock);&lt;br /&gt;                  }&lt;br /&gt;              }&lt;br /&gt;          }&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      public void Bind(T newValue)&lt;br /&gt;      {&lt;br /&gt;          this.Value = newValue;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      public static implicit operator T(DataflowVariable&lt;t&gt; myVar)&lt;br /&gt;      {&lt;br /&gt;          return myVar.Value;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;&lt;/t&gt;&lt;/t&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Dataflow variables in F#&lt;/h2&gt;&lt;br /&gt;Let’s start with the simple example of how to use a dataflow variable in F#.&lt;br /&gt;&lt;code&gt;&lt;/code&gt;&lt;pre&gt;let myVar = new DataflowVariable&lt;int&gt;() // create variable&lt;br /&gt;myVar  &amp;lt;~ value //bind variable&lt;br /&gt;&lt;br /&gt;let value = (1000 + !!myVar) //read variable&lt;br /&gt;&lt;/int&gt;&lt;/pre&gt;&lt;br /&gt;Here we use operator (&amp;lt;~) to bind the dataflow variable and operator (!!) to read its value.&lt;br /&gt;&lt;br /&gt;Now the code itself:&lt;br /&gt;&lt;code&gt;&lt;/code&gt;&lt;pre&gt;type public DataflowVariable&amp;lt;'T&amp;gt; () =&lt;br /&gt;      class&lt;br /&gt;        &lt;br /&gt;          [&amp;lt;volatilefield&amp;gt;]&lt;br /&gt;          let mutable value : option&amp;lt;'T&amp;gt; = None&lt;br /&gt;&lt;br /&gt;          let syncLock = new System.Object()&lt;br /&gt;&lt;br /&gt;          member private this.Value&lt;br /&gt;              with get() : 'T =&lt;br /&gt;                  match value with&lt;br /&gt;                  | Some(initializedVal) -&amp;gt; initializedVal&lt;br /&gt;                  | None -&amp;gt;&lt;br /&gt;                      lock syncLock (fun () -&amp;gt;&lt;br /&gt;                                          while (value.IsNone) do&lt;br /&gt;                                              ignore (System.Threading.Monitor.Wait(syncLock))&lt;br /&gt;                                          value.Value)&lt;br /&gt;              and set(newVal : 'T) =&lt;br /&gt;                  lock syncLock (fun () -&amp;gt;&lt;br /&gt;&lt;br /&gt;                                  match value with&lt;br /&gt;                                  | Some(_) -&amp;gt; invalidOp "Dataflow variable can be set only once."&lt;br /&gt;                                  | None -&amp;gt;&lt;br /&gt;                                      value &amp;lt;- Some(newVal)&lt;br /&gt;                                      System.Threading.Monitor.PulseAll(syncLock))&lt;br /&gt;        &lt;br /&gt;          static member public (&amp;lt;~) (var:DataflowVariable&amp;lt;'T&amp;gt;, initValue:'T) =&lt;br /&gt;              var.Value &amp;lt;- initValue&lt;br /&gt;&lt;br /&gt;          static member public (!!) (var:DataflowVariable&amp;lt;'T&amp;gt;) : 'T =&lt;br /&gt;              var.Value&lt;br /&gt;      end&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;You may have noticed [&amp;lt;volatilefield&amp;gt;] attribute. As per pretty stingy documentation this attribute&lt;br /&gt;effectively replaces volatile keyword in C#, but I haven’t performed thorough testing to verify it. What? F# has no&lt;br /&gt;keyword for volatile fields? And this is as it has to be. Volatile fields belong to the domain of imperative&lt;br /&gt;programming and F#, being first of all functional programming language (which is implementation of declarative&lt;br /&gt;model), tries to avoid shared state (remember mutable keyword?). F# does not support overloading of implicit&lt;br /&gt;conversion operators, that’s why we need some kind of dereferencing prefix operator (!!).&lt;br /&gt;F# implementation is more elegant, because we expose Option type here and thus do not have to deal with&lt;br /&gt;isInitialized field as in case of C# implementation.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Implementation details and some thoughts on thread synchronization&lt;/h2&gt;&lt;br /&gt;For synchronization in both implementations I have used volatile fields in conjunction with a simple pattern for&lt;br /&gt;Monitor.Wait/Monitor.Pulse. More information regarding Monitor.Pulse/Monitor.Wait you can get in this very nice&lt;br /&gt;article by &lt;a href="http://www.blogger.com/%E2%80%9Dhttp://www.albahari.com/threading/part4.aspx#_How_to_Use_Wait_and_Pulse%E2%80%9D"&gt;Joe Albahari&lt;/a&gt;.&lt;br /&gt;Volatile fields here are used to prevent instruction reordering and ensure CPU cache synchronization.&lt;br /&gt;Also as an option, instead of using volatile field, we could use here Thread.VolatileRead method (we do not need to&lt;br /&gt;use also Thread.VolatileWrite because actual write is done in within the lock statement which effectively prevents&lt;br /&gt;reordering and flushes and invalidates CPU cache, and anyway Thread.VolatileWrite only flushes the CPU cache but&lt;br /&gt;does not invalidate it). Basically, the static VolatileRead and VolatileWrite methods in the Thread class&lt;br /&gt;read/write a variable while enforcing (technically, a superset of) the guarantees made by the volatile keyword.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Dataflow programming examples in C# and F#&lt;/h2&gt;&lt;br /&gt;In C# I will demonstrate a simple example of dataflow prorgamming with Parallel extensions library (futures and&lt;br /&gt;continuations). Basically using Task.Factory.ContinueWhenAll one can achieve similar results as with dataflow&lt;br /&gt;variables, but dataflow variables provide developers with much more flexibility.&lt;br /&gt;&lt;code&gt;&lt;/code&gt;&lt;pre&gt;var input1 = new DataflowVariable&amp;lt;int&amp;gt;();&lt;br /&gt;var input2 = new DataflowVariable&amp;lt;int&amp;gt;();&lt;br /&gt;var output1 = new DataflowVariable&amp;lt;int&amp;gt;();&lt;br /&gt;var output2 = new DataflowVariable&amp;lt;int&amp;gt;();&lt;br /&gt;&lt;br /&gt;Task&amp;lt;int&amp;gt; task1 = Task.Factory.StartNew&amp;lt;int&amp;gt;(&lt;br /&gt;              () =&amp;gt;&lt;br /&gt;&lt;br /&gt;                  {&lt;br /&gt;                      output1.Bind(input1 + input2);&lt;br /&gt;                      return output1*10;&lt;br /&gt;                  });&lt;br /&gt;Task&amp;lt;int&amp;gt; task = Task.Factory.StartNew&amp;lt;int&amp;gt;(() =&amp;gt;&lt;br /&gt;                                             {&lt;br /&gt;                                                   output2.Bind(input1 + output1);&lt;br /&gt;                                                   return input1;&lt;br /&gt;                                             });&lt;br /&gt;&lt;br /&gt;input1.Bind(333);&lt;br /&gt;input2.Bind(888);&lt;br /&gt;&lt;br /&gt;Console.WriteLine(10 + output1 + output2);&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Conclusion&lt;/h2&gt;&lt;br /&gt;Article describes basic implementation of dataflow variables in C# and F# programming languages and basic examples&lt;br /&gt;of dataflow programming using continuations/futures. Please, consider this article as an starting point in a&lt;br /&gt;journey into the world of dataflow programming.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;!-- End Article --&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7160136240184027073-5069374318627293165?l=dotnetframeworkplanet.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/5069374318627293165/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7160136240184027073&amp;postID=5069374318627293165' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/5069374318627293165'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/5069374318627293165'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/2010/09/dataflow-programming-in-f-and-c.html' title='Dataflow programming in F# and C#'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7160136240184027073.post-4156873393401317831</id><published>2009-11-05T03:38:00.000-08:00</published><updated>2009-11-05T03:44:16.189-08:00</updated><title type='text'>The best .NET debugger in the world</title><content type='html'>Recently I've been playing with pretty complex .NET components written by Vitaliy Liptchinsky and at some point came to the conclusion:&lt;br /&gt;&lt;br /&gt;The best .NET debugger in the world is... IronPython console.&lt;br /&gt;&lt;br /&gt;Why?&lt;br /&gt;&lt;br /&gt;Have you ever tried to continuously play, set up new relationships, create complex objects within Visual Studio? Isn't it boring and cumbersome after each modification re-compile and re-run VS project?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7160136240184027073-4156873393401317831?l=dotnetframeworkplanet.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/4156873393401317831/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7160136240184027073&amp;postID=4156873393401317831' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/4156873393401317831'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/4156873393401317831'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/2009/11/best-net-debugger-in-world.html' title='The best .NET debugger in the world'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7160136240184027073.post-8121786244275042248</id><published>2009-03-12T03:14:00.000-07:00</published><updated>2009-03-12T03:28:56.075-07:00</updated><title type='text'>WCF and delayed computations (C# yield keyword)</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Never use delayed computations in WCF service contract implementation!&lt;/span&gt; Reason: IErrorHandler component is never invoked in this case. If there is any kind of exception within delayed computation, it would be increasingly hard for you to find out the reason even with WCF tracing.&lt;br /&gt;Let's have a detailed look how WCF works (this is very simplified version):&lt;br /&gt;int --&gt; WCF serializer --&gt; some other staff :) --&gt; try { result = CallYourCustomCode() } catch{ CallToErrorHandler() ... } --&gt; WCF serializer (result) --&gt; out&lt;br /&gt;So, if you have delayed computations created with help of &lt;span style="font-weight: bold;"&gt;yield&lt;/span&gt; C# keyword, CallYourCustomCode returns not the actual result, but kind of reference to your implementation. This reference will be resolved and executed during &lt;span style="font-weight: bold;"&gt;serialization&lt;/span&gt; (!). So, any exception during serialization will close wcf channel, get round of IErrorHandler, and produce sensless exception to the WCF client.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7160136240184027073-8121786244275042248?l=dotnetframeworkplanet.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/8121786244275042248/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7160136240184027073&amp;postID=8121786244275042248' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/8121786244275042248'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/8121786244275042248'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/2009/03/wcf-and-delayed-computations-c-yield.html' title='WCF and delayed computations (C# yield keyword)'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7160136240184027073.post-3051769626588930818</id><published>2009-02-27T05:34:00.000-08:00</published><updated>2009-02-27T05:38:13.477-08:00</updated><title type='text'>F# and Parallel Extensions for .NET</title><content type='html'>Recently I've posted an article about F# workflows based on Coordination and Concurrency Runtime:&lt;br /&gt;http://www.codeproject.com/KB/net-languages/wf_Fsharp_ccr.aspx&lt;br /&gt;&lt;br /&gt;Now I've started thinking that in the same way it would be possible to combine F# with Parallel Extensions for .NET....&lt;br /&gt;Really explosive mixture!!!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7160136240184027073-3051769626588930818?l=dotnetframeworkplanet.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/3051769626588930818/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7160136240184027073&amp;postID=3051769626588930818' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/3051769626588930818'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/3051769626588930818'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/2009/02/f-and-parallel-extensions-for-net.html' title='F# and Parallel Extensions for .NET'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7160136240184027073.post-7509857717480180907</id><published>2008-11-26T14:03:00.000-08:00</published><updated>2008-11-26T14:07:13.962-08:00</updated><title type='text'>Transactional repository</title><content type='html'>What transactional repositories do we know at the moment? Here is a list: SQL Server, MSMQ, file system and registry (in Windows Vista/Windows Server 2008). Is it enough? Does it covers all possible needs of enterprises?&lt;br /&gt;At &lt;a href="http://www.codeproject.com/KB/dotnet/Transactional_Repository.aspx"&gt;CodeProject&lt;/a&gt; I've described custom implementation of transactional repository based on Enterprise Library Caching Application block.&lt;br /&gt;Transactional Repository implementation described in article above provides basic principles required for implementation of any custom transactional repository that can easily participate in ambient and explicit transactions in .NET.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7160136240184027073-7509857717480180907?l=dotnetframeworkplanet.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/7509857717480180907/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7160136240184027073&amp;postID=7509857717480180907' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/7509857717480180907'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/7509857717480180907'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/2008/11/transactional-repository.html' title='Transactional repository'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7160136240184027073.post-6052316274553120265</id><published>2008-11-14T04:11:00.000-08:00</published><updated>2008-11-19T06:31:57.597-08:00</updated><title type='text'>volatile field and memory barrier: look inside</title><content type='html'>I've seen a lot of discussions in the web regarding volatile field. I've performed my own small investigation regarding this subject and here is some thoughts on this:&lt;br /&gt;&lt;br /&gt;The two main purposes of c# &lt;span style="font-weight: bold;"&gt;volatile&lt;/span&gt; fields are the following ones:&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;1. Introduce memory barriers for all acce&lt;/span&gt;&lt;span style="font-weight: bold;"&gt;ss operations to this fields. &lt;/span&gt;In order to improve performance CPUs store frequently accessible objects in CPU cache. In case of multi-threaded applications this can cause problems. For instance, imagine situation, when one thread is constantly reading some &lt;span style="font-style: italic;"&gt;boolean &lt;/span&gt;value (&lt;span style="font-style: italic;"&gt;read &lt;/span&gt;thread) and another one is responsible for updating this field (&lt;span style="font-style: italic;"&gt;w&lt;/span&gt;&lt;span style="font-style: italic;"&gt;rite&lt;/span&gt; thread). Now, if OS will decide to run these two threads on different CPUs, it is possible, that &lt;span style="font-style: italic;"&gt;update&lt;/span&gt; thread will change value of the field on CPU1 cache and &lt;span style="font-style: italic;"&gt;read&lt;/span&gt; thread will continue reading this value from CPU2 cache, in other words, it will get the change of thread1 until CPU1 cache is invalidated. Situation can be even worth if two threads update this value.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;volatile&lt;/span&gt; field introduces memory barriers, which means, that CPU always will read from and write to virtual memory, but not to CPU cache.&lt;br /&gt;Nowadays such CPU architectures as x86 and x64 have &lt;span style="font-weight: bold;"&gt;CPU cache coherency&lt;/span&gt;, which means that any change in CPU cache of one processor will be propagated to other CPUs' caches. And, in it's turn, it means that JIT compiler for x86 and x64 platforms makes no difference between &lt;span style="font-weight: bold;"&gt;volatile &lt;/span&gt;and &lt;span style="font-weight: bold;"&gt;non-volatile&lt;/span&gt; fields (except stated in item #2). Also, multicore CPUs usually have &lt;span style="font-weight: bold;"&gt;two levels of cache&lt;/span&gt;: first level is shared between CPU cores and second one is not.&lt;br /&gt;But, such CPU architectures as Itanium with weak memory model does not have cache coherency and therefore &lt;span style="font-weight: bold;"&gt;volatile&lt;/span&gt; keyword and memory barriers play significant role while designing multi-threaded application.&lt;br /&gt;Therefore, I'd recommend always to use &lt;span style="font-weight: bold;"&gt;volatile&lt;/span&gt; and momemory barriers even for x86 and x64 CPUs, because otherwise you introduce &lt;span style="font-weight: bold;"&gt;CPU architecture affinitty to your application&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Note: you can also introduce memory barriers by using Thread.VolatileRead/Thread.VolatileWrite (these two method successfully replace volatile keyword), Thread.MemoryBarrier, or even with c# &lt;span style="font-style: italic;"&gt;lock&lt;/span&gt; keyword etc.&lt;br /&gt;&lt;br /&gt;Below are displayed two CPU architectures: Itanium and AMD (Direct connect architecture). As we can see in AMD's Direct Connect architecture all processors are connected with each other, so we have memory coherence. In Itanium architecture CPU are not connected with each other and communicated with RAM through System Bus.&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/_p6Os3AXuYcg/SSQjIt337aI/AAAAAAAAAAk/UUNi9wtfois/s1600-h/AMDCPUArch.bmp"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 284px; height: 320px;" src="http://4.bp.blogspot.com/_p6Os3AXuYcg/SSQjIt337aI/AAAAAAAAAAk/UUNi9wtfois/s320/AMDCPUArch.bmp" alt="" id="BLOGGER_PHOTO_ID_5270376096590196130" border="0" /&gt;&lt;/a&gt;&lt;a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://2.bp.blogspot.com/_p6Os3AXuYcg/SSQjQ0eMhPI/AAAAAAAAAAs/uRfDnlkD7tE/s1600-h/ItaniumCPUArch.bmp"&gt;&lt;img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer; width: 320px; height: 251px;" src="http://2.bp.blogspot.com/_p6Os3AXuYcg/SSQjQ0eMhPI/AAAAAAAAAAs/uRfDnlkD7tE/s320/ItaniumCPUArch.bmp" alt="" id="BLOGGER_PHOTO_ID_5270376235800495346" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;2. &lt;/span&gt;&lt;span style="font-weight: bold;"&gt;Prevents instruction reordering&lt;/span&gt;. For instance, consider we have a loop:&lt;br /&gt;while(true)&lt;br /&gt;{&lt;br /&gt;if(myField)&lt;br /&gt;{&lt;br /&gt;      //do something&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;In case of non-volatile field, during JIT compilation, JIT compiler due to performance considerations can reorder instructions in the fo9llowing manner:&lt;br /&gt;if(myField)&lt;br /&gt;{&lt;br /&gt; while(true)&lt;br /&gt; {&lt;br /&gt;       //do something&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;In case if you plan to change myField from separate thread, this significant difference, isn't it?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Usually it is recommended to use &lt;span style="font-weight: bold;"&gt;lock&lt;/span&gt; statement (Monitor.Enter or Monitor.Exit), but if you change only one field within this block, then volatile field will perform significantly better than Monitor class.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7160136240184027073-6052316274553120265?l=dotnetframeworkplanet.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/6052316274553120265/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7160136240184027073&amp;postID=6052316274553120265' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/6052316274553120265'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/6052316274553120265'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/2008/11/volatile-field-and-memory-barrier-look.html' title='volatile field and memory barrier: look inside'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/_p6Os3AXuYcg/SSQjIt337aI/AAAAAAAAAAk/UUNi9wtfois/s72-c/AMDCPUArch.bmp' height='72' width='72'/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7160136240184027073.post-5644565353894120809</id><published>2008-10-10T01:13:00.000-07:00</published><updated>2008-11-17T07:56:28.370-08:00</updated><title type='text'>Custom ThreadPool implementation</title><content type='html'>.NET Framework BCL contains very nice implementation of thread pool (System.Threading.ThreadPool class).&lt;br /&gt;But this class is not suitable for following scenarios:&lt;br /&gt;1. Long-running operations. Usually for long-running operations it is recommended to use Thread class.&lt;br /&gt;2. ThreadPool is &lt;span style="font-style: italic;"&gt;per process&lt;/span&gt;.&lt;span style="font-style: italic;"&gt; &lt;/span&gt;It means, that situation when there is no available threads in ThreadPool can happen pretty often. What if you have very important  and emergent work items and do not want rely on such a risk? But pretty often, especially when you have application with number of app domains (like IIS or SQL server) you can run out of threads in thread pool...&lt;br /&gt;3. ThreadPool does not support IAsyncResult. BeginInvoke methods of all delegates internally pass control to ThreadPool, but ThreadPool itself does not support IAsyncResult.&lt;br /&gt;&lt;br /&gt;It is just initial version of CustomThreadPool and I plan to extend it in future.&lt;br /&gt;&lt;br /&gt;Generally, there are number of strict recommendations when you should use ThreadPool and  when you should use Thread class directly or MulticastDelegate.BeginInvoke.  Ideally I plan to create ThreadPool that would suit for those scenarios  applicable for Thread class and BeginInvoke.  The main problem of  System.Threading.ThreadPool is that it is &lt;em&gt;per process.&lt;/em&gt; So  if you have set of very important tasks to do and also have set of third-party  assemblies that you host in the application, there is always probability that  your important tasks will be delayed. In case of CustomThreadPool you have  separate threadpool for each application domain.&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Ok, I know, there is number of maximum threads allowed per process and if there is too much threads, thread switch context can be awfull...&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The sources of ThreadPool implementation can be found on &lt;a href="http://www.codeproject.com/KB/dotnet/custom_thread_pool.aspx"&gt;CodeProject&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;workerthread&gt;&lt;workerthread&gt;&lt;tresult&gt;&lt;tresult&gt;&lt;tresult&gt;&lt;workerthread&gt;&lt;workerthread&gt;The code above is intended to &lt;span style="font-weight: bold;"&gt;demonstrate &lt;/span&gt;an idea how it could be implemented. I haven't tested it carefully, but it seems to be working ;)&lt;/workerthread&gt;&lt;/workerthread&gt;&lt;/tresult&gt;&lt;/tresult&gt;&lt;/tresult&gt;&lt;/workerthread&gt;&lt;/workerthread&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7160136240184027073-5644565353894120809?l=dotnetframeworkplanet.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/5644565353894120809/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7160136240184027073&amp;postID=5644565353894120809' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/5644565353894120809'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/5644565353894120809'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/2008/10/custom-threadpool-implementation.html' title='Custom ThreadPool implementation'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7160136240184027073.post-3459117498201983226</id><published>2008-10-08T12:25:00.000-07:00</published><updated>2008-11-14T13:19:51.920-08:00</updated><title type='text'>ESB: WCF implementation</title><content type='html'>So, at first, in order to implement enterprise service using WCF, we need the idea of generic service, which can handle any incoming messages.&lt;br /&gt;&lt;a href="http://weblogs.asp.net/pglavich/archive/2008/01/19/wcf-handling-generic-messages.aspx"&gt;This &lt;/a&gt;blog describes how to handle generic messages in WCF service.&lt;br /&gt;This means, that we can serialize any custom serializable object and send it as message to WCF service and service will accept it and will try to process.&lt;br /&gt;&lt;br /&gt;But whats next? The question is, how would WCF service know how to process this incoming message.... It can be, for instance business logic related entity or notification about problems in remote component...&lt;br /&gt;Yes, and according to title our WCF service needs to be generic. The idea below describes quite simple WCF service that can handle various incoming messages without any recompilation.&lt;br /&gt;The flow is the following:&lt;br /&gt;1. Retrieve contents of &lt;span style="font-style: italic;"&gt;body&lt;/span&gt; element&lt;br /&gt;2. Based on some criteria (it can be name of the root node plus namespace name), choose XSLT transformation that is appropriate for given message. XSLT transformation file can be stored as local file or in database.&lt;br /&gt;3. Using XSLT transformation transform incoming XML to XAML (or even to c# code!).&lt;br /&gt;4. Compile resulting XAML.&lt;br /&gt;5. Execute compiled code. The resulting code can do anything: from executing database queries to sending e-mail to administrator&lt;br /&gt;&lt;br /&gt;Drawbacks of this approach:&lt;br /&gt;1. You are not able to debug the resulting code&lt;br /&gt;2. It requires a nice tool that would produce XSLT transformation based on given message content and handler (c# code).&lt;br /&gt;&lt;br /&gt;Benefits:&lt;br /&gt;1. As new messages are introduced in enterprise, you do not have to modify/redeploy the service. All you need, is to provide WCF service with additional &lt;span style="font-style: italic;"&gt;scripts&lt;/span&gt;.&lt;br /&gt;2. It is very flexible, because you are not dependent on any message types, so you theoretically can receive and process anything.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;P.S.&lt;br /&gt;&lt;br /&gt;Yes, I know, instead of XSLT transformation we can deploy assemblies-handlers for each event and dynamically load these assemblies.&lt;br /&gt;&lt;br /&gt;Probably, this approach will never get a chance to be implemented... I just wanted to share interesting idea...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7160136240184027073-3459117498201983226?l=dotnetframeworkplanet.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/3459117498201983226/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7160136240184027073&amp;postID=3459117498201983226' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/3459117498201983226'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/3459117498201983226'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/2008/10/most-generic-wcf-reporting-service-in.html' title='ESB: WCF implementation'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7160136240184027073.post-8378996103222362314</id><published>2008-06-12T07:39:00.001-07:00</published><updated>2008-06-12T07:39:56.755-07:00</updated><title type='text'>Database connection: static or non-static?</title><content type='html'>I believe this is quite common question of all developers, that use databases in applications. I’ve seen a lot mistakes regarding this choice.&lt;br /&gt;Is it better to create one static connection and use it thru all the code, or during each database call create new connection object?&lt;br /&gt;&lt;br /&gt;The answers are:&lt;br /&gt;In case of DB server (stand-alone database like SQL Server Express/Standard/Enterprise or Oracle) it is always better to create and dispose new connection objects, because almost all db drivers (like ADO.NET, ODBC, Oracle) have such feature as connection pooling and you won’t experience benefits of keeping one connection alive. Static connection can even decrease performance, because in multithreading application single connection object cannot be used simultaneously. Also static connections decrease scalability of applications. Usually connection pooling performs better than your custom code that tries to re-use created connections. There is always exceptions from this situation: if you are going to execute number of SQL statements sequentially, it would be mistake to create new connection for each new statement!&lt;br /&gt;&lt;br /&gt;In case of embedded databases (like SQL Server CE) it is better to use static connection, because such kind of databases does not have connection pooling and connection re-creation usually costs a lot.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7160136240184027073-8378996103222362314?l=dotnetframeworkplanet.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/8378996103222362314/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7160136240184027073&amp;postID=8378996103222362314' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/8378996103222362314'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/8378996103222362314'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/2008/06/database-connection-static-or-non.html' title='Database connection: static or non-static?'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7160136240184027073.post-6293300684436219812</id><published>2008-01-02T09:50:00.000-08:00</published><updated>2008-01-02T09:51:43.431-08:00</updated><title type='text'>Quite interesting ASP.NET utilization</title><content type='html'>Hi,I just want to present you quite interesting approach of ASP.NET utilization.&lt;br /&gt;&lt;br /&gt;This approach shows how to render html files using ASP.NET pages and server controls. For instance, we have some amount of read-only data which we want to present to the user with high level of user interactivity and ability to print. Let's consider html documents and embedded browser as ActiveX control. Here we already have pretty good printing capability and also we can provide users with rich interactivity using JavaScript. If we could generate html using compiled .aspx pages it would be the best, because we can edit and create web forms in Visual Studio (and also we can use all ASP.NET powerful controls like DataGrid) and then all we have to do is to produce html using generated ASP.NET page handlers.&lt;br /&gt;&lt;br /&gt;Flow is as follows:&lt;br /&gt;1. Retrieve data from database&lt;br /&gt;2. Bind ASP.NET page to retrieved data&lt;br /&gt;3. Render ASP.NET page to output html stream in order to retrieve necessary document&lt;br /&gt;&lt;br /&gt;The principle of my solution is as follows: create pseudo-HTTPContext (Request, Response, Response stream) and pseudo-Browser -&gt; omit all unnecessary HTTP Modules and directly assign this context to page handler -&gt; launch lifecycle of the page -&gt; retrieve rendered html.I've already created proof-of-concept (this proof-of-concept uses simple controls like button with JavaScript as well as complex controls like DataGrid). Everything works great.&lt;br /&gt;&lt;br /&gt;I've looked through web and haven't found anything similar.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7160136240184027073-6293300684436219812?l=dotnetframeworkplanet.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/6293300684436219812/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7160136240184027073&amp;postID=6293300684436219812' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/6293300684436219812'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/6293300684436219812'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/2008/01/quite-interesting-aspnet-utilization.html' title='Quite interesting ASP.NET utilization'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7160136240184027073.post-1667086226610052689</id><published>2007-10-05T01:49:00.000-07:00</published><updated>2007-10-05T01:51:08.429-07:00</updated><title type='text'>typeof(void)???</title><content type='html'>What returns typeof(void)? void is special keyword in C#. void is related to System.Void type in .NET BCL. System.Void is simple structure with no arguments and default constructor.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7160136240184027073-1667086226610052689?l=dotnetframeworkplanet.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/1667086226610052689/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7160136240184027073&amp;postID=1667086226610052689' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/1667086226610052689'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/1667086226610052689'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/2007/10/typeofvoid.html' title='typeof(void)???'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7160136240184027073.post-8830517216233032385</id><published>2007-09-25T05:09:00.000-07:00</published><updated>2008-05-28T04:26:06.229-07:00</updated><title type='text'>Be careful with reflection and delegates</title><content type='html'>&lt;p&gt;For instance, you have pluggable application which runs set of assemblies (plugins), each in separate "sandbox" domain. Next code block included within "malicious" plugin causes application to crash:&lt;/p&gt;&lt;p&gt;&lt;br /&gt;class Program&lt;br /&gt;{&lt;br /&gt;static void Main(string[] args)&lt;br /&gt;{&lt;br /&gt;AppDomain newDomain = AppDomain.CreateDomain("NewDomain");&lt;br /&gt;ObjectHandle oh = newDomain.CreateInstance(Assembly.GetExecutingAssembly().FullName, "CLRCrashApp.SomeClass");&lt;br /&gt;((SomeClass)oh.Unwrap()).DoSomething();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class SomeClass : MarshalByRefObject&lt;br /&gt;{&lt;br /&gt;private delegate void DoSomethingDelegate();&lt;br /&gt;public SomeClass() : base()&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;public void DoSomething()&lt;br /&gt;{&lt;br /&gt;Console.WriteLine("AppDomain is: {0}", AppDomain.CurrentDomain.FriendlyName);&lt;br /&gt;try&lt;br /&gt;{&lt;br /&gt;Activator.CreateInstance(typeof(DoSomethingDelegate), new object[] { null, null });&lt;br /&gt;}&lt;br /&gt;catch (Exception ex)&lt;br /&gt;{&lt;br /&gt;Console.WriteLine(ex.ToString());&lt;br /&gt;if (ex.InnerException != null)&lt;br /&gt;{&lt;br /&gt;Console.WriteLine(ex.InnerException.ToString());&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;finally&lt;br /&gt;{&lt;br /&gt;Console.WriteLine("Finally is executing...");&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;&lt;p&gt;So, be careful when playing with reflection and delegates. Small mistake can cause crash of your entire application. Event separate AppDomain won't help you handle this.&lt;/p&gt;&lt;p&gt;In order to prevent such "malicious" plug-ins it is necessary to use CLR hosting API and host CLR by your own.&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7160136240184027073-8830517216233032385?l=dotnetframeworkplanet.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/8830517216233032385/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7160136240184027073&amp;postID=8830517216233032385' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/8830517216233032385'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/8830517216233032385'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/2007/09/malicious-attack-on-clr.html' title='Be careful with reflection and delegates'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7160136240184027073.post-766112858753858472</id><published>2007-09-21T07:35:00.000-07:00</published><updated>2008-11-28T02:11:06.386-08:00</updated><title type='text'>How to precompile method from MSIL to native code without invoking it</title><content type='html'>In order to precompile method without invoking it you should use System.Runtime.CompilerServices.RuntimeHelpers.PrepareMethod method.&lt;br /&gt;&lt;br /&gt;Possible situation when this method is extremely necessary: You need to precompile huge assembly in separate thread during application delays and you can't use ngen.exe.&lt;br /&gt;&lt;br /&gt;User always complain about lazy load of all screens in .NET applications. This lazy load is caused by JIT compilation...&lt;br /&gt;For instance, you have fat (rich) client application with log-in screen. During log-in process you can pre-compile heavy assemblies in separate thread.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7160136240184027073-766112858753858472?l=dotnetframeworkplanet.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dotnetframeworkplanet.blogspot.com/feeds/766112858753858472/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7160136240184027073&amp;postID=766112858753858472' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/766112858753858472'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7160136240184027073/posts/default/766112858753858472'/><link rel='alternate' type='text/html' href='http://dotnetframeworkplanet.blogspot.com/2007/09/how-to-precompile-method-from-msil-to.html' title='How to precompile method from MSIL to native code without invoking it'/><author><name>Vitaliy Liptchinsky, MCPD|MCSD</name><uri>http://www.blogger.com/profile/02114106238745413799</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>
