<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Living .NET...  : .NET</title><link>http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx</link><description>Tags: .NET</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>A simple MSMQ Listener helper class</title><link>http://msmvps.com/blogs/manoj/archive/2005/10/16/70979.aspx</link><pubDate>Mon, 17 Oct 2005 01:00:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:70979</guid><dc:creator>Manoj G</dc:creator><slash:comments>10</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=70979</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2005/10/16/70979.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;MSMQ simply provides wonderful capabilities when it comes to building asynchronous messaging based applications. You typically run into situations where two applications exchange messages through a queue &amp;#8211; one application sends messages to a known MSMQ destination and the other application &amp;#8220;listens&amp;#8221; to this queue and picks messages up as and when they arrive. The application which listens usually is a windows service which either has a dedicated thread making a blocking call on the Receive method or uses a timer of sorts to periodically check the queue for new messages. Either ways, to maximize throughput, you need to write the queue-listener code in such a way that you can have multiple threads simultaneously processing different messages that arrive at the queue. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;MSMQ triggers provide one such solution where we can associate incoming messages to a COM component, based on certain rules, which can be application specific. MSMQ triggers are pretty powerful and also easy to administer. But there is one constraint, which has to do with the fact that only COM components are supported in this scheme, or in other words, message processors should be COM components. In a way, it isn&amp;#8217;t that bad a choice after all. You still can create a CCW for a managed class and register the trigger. But, somehow, I didn&amp;#8217;t like doing this.&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;To circumvent this problem, I decided to write a C# helper class, which doesn&amp;#8217;t really do anything much, but helps realize a simple multithreaded job (message processing) dispatcher. The helper itself doesn&amp;#8217;t do any message processing as such; whenever a message arrives at a queue, it just fires an event (on a new thread pool thread) with the message contents being in the event arguments.&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;delegate&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;MessageReceivedEventHandler(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;object&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;sender,&amp;nbsp;MessageEventArgs&amp;nbsp;args);&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;class&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;MSMQListener&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;bool&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;_listen;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Type[]&amp;nbsp;_types;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;MessageQueue&amp;nbsp;_queue;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;event&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;MessageReceivedEventHandler&amp;nbsp;MessageReceived;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Type[]&amp;nbsp;FormatterTypes&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;get&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;return&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;_types;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;set&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&amp;nbsp;_types&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;value&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;MSMQListener(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;string&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;queuePath)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_queue&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;MessageQueue(queuePath);&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Start()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_listen&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;true&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;if&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;(_types&amp;nbsp;!=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;null&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;&amp;amp;&amp;amp;&amp;nbsp;_types.Length&amp;nbsp;&amp;gt;&amp;nbsp;0)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;Using&amp;nbsp;only&amp;nbsp;the&amp;nbsp;XmlMessageFormatter.&amp;nbsp;You&amp;nbsp;can&amp;nbsp;use&amp;nbsp;other&amp;nbsp;formatters&amp;nbsp;as&amp;nbsp;well&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;_queue.Formatter&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;XmlMessageFormatter(_types);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_queue.PeekCompleted&amp;nbsp;+=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;PeekCompletedEventHandler(OnPeekCompleted);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_queue.ReceiveCompleted&amp;nbsp;+=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;ReceiveCompletedEventHandler(OnReceiveCompleted);&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StartListening();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Stop()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_listen&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;false&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_queue.PeekCompleted&amp;nbsp;-=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;PeekCompletedEventHandler(OnPeekCompleted);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_queue.ReceiveCompleted&amp;nbsp;-=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;ReceiveCompletedEventHandler(OnReceiveCompleted);&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;StartListening()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;if&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;(!_listen)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;return&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;The&amp;nbsp;MSMQ&amp;nbsp;class&amp;nbsp;does&amp;nbsp;not&amp;nbsp;have&amp;nbsp;a&amp;nbsp;BeginRecieve&amp;nbsp;method&amp;nbsp;that&amp;nbsp;can&amp;nbsp;take&amp;nbsp;in&amp;nbsp;a&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;MSMQ&amp;nbsp;transaction&amp;nbsp;object.&amp;nbsp;This&amp;nbsp;is&amp;nbsp;a&amp;nbsp;workaround&amp;nbsp;-&amp;nbsp;we&amp;nbsp;do&amp;nbsp;a&amp;nbsp;BeginPeek&amp;nbsp;and&amp;nbsp;then&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;recieve&amp;nbsp;the&amp;nbsp;message&amp;nbsp;synchronously&amp;nbsp;in&amp;nbsp;a&amp;nbsp;transaction.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;Check&amp;nbsp;documentation&amp;nbsp;for&amp;nbsp;more&amp;nbsp;details&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;if&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;(_queue.Transactional)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_queue.BeginPeek();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;else&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_queue.BeginReceive();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;OnPeekCompleted(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;object&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;sender,&amp;nbsp;PeekCompletedEventArgs&amp;nbsp;e)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_queue.EndPeek(e.AsyncResult);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MessageQueueTransaction&amp;nbsp;trans&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;MessageQueueTransaction();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Message&amp;nbsp;msg&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;null&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;try&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;trans.Begin();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;msg&amp;nbsp;=&amp;nbsp;_queue.Receive(trans);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;trans.Commit();&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StartListening();&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FireRecieveEvent(msg.Body);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;catch&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;trans.Abort();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;FireRecieveEvent(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;object&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;body)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;if&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;(MessageReceived&amp;nbsp;!=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;null&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MessageReceived(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;this&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;MessageEventArgs(body));&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;OnReceiveCompleted(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;object&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;sender,&amp;nbsp;ReceiveCompletedEventArgs&amp;nbsp;e)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Message&amp;nbsp;msg&amp;nbsp;=&amp;nbsp;_queue.EndReceive(e.AsyncResult);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StartListening();&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FireRecieveEvent(msg.Body);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;class&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;MessageEventArgs&amp;nbsp;:&amp;nbsp;EventArgs&lt;BR&gt;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;object&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;_messageBody;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;object&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;MessageBody&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;get&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;return&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;_messageBody;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;MessageEventArgs(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;object&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;body)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_messageBody&amp;nbsp;=&amp;nbsp;body;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;As you can see, the helper class is indeed simple. One thing to notice though is that, I switch to &lt;STRONG&gt;BeginPeek&lt;/STRONG&gt; if the queue is transactional. The reason behind this is there is no overload of the &lt;STRONG&gt;BeginRecieve&lt;/STRONG&gt; method that takes a &lt;STRONG&gt;MessageQueueTransaction&lt;/STRONG&gt; object and the MSDN documentation also explains the same. The workaround is to use BeginPeek and then do call &lt;STRONG&gt;Receive&lt;/STRONG&gt; (synchronous/blocking operation) in the EndPeek method. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;At this point of time, this class does not provide much control to the consumer. For instance, it does not allow you to set the maximum number of concurrent threads that can be processing messages - This class queues as many jobs as that supported by the .NET Thread pool. Also, I haven&amp;#8217;t tested this code in all scenarios &amp;#8211; I have not seen if this works well in the context of a distributed (DTC based) transaction. I shall update this version every now and then to make it more powerful and to work seamlessly in all scenarios.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;BR&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=70979" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Patterns in .NET : Citing the Chain of Responsibility pattern</title><link>http://msmvps.com/blogs/manoj/archive/2005/09/29/68256.aspx</link><pubDate>Fri, 30 Sep 2005 03:24:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:68256</guid><dc:creator>Manoj G</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=68256</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2005/09/29/68256.aspx#comments</comments><description>&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;A href="http://www.dofactory.com/Patterns/PatternChain.aspx"&gt;&lt;FONT face=Verdana color=#0000ff&gt;&lt;STRONG&gt;Chain of Responsibility&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana&gt;&lt;FONT color=#000080&gt; basically involves &amp;#8220;chaining&amp;#8221; a set of objects between a sender and receiver, where each object in the chain does some processing on the message that is sent between the sender and the receiver. &lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&lt;FONT face=Verdana color=#000080&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT face=Verdana&gt;&lt;FONT color=#000080&gt;In the .NET Framework, one area where this pattern manifests is the Remoting infrastructure. In Remoting, a remote method call actually translates to a message exchange between the client and the server. When the message passes between client and the server object, it goes through a series of message sinks, which are objects which implement the &lt;STRONG&gt;IMessageSink&lt;/STRONG&gt; interface. Here&amp;#8217;s how the interface looks like (attributes have been elided for clarity):&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;interface&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;IMessageSink&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IMessageCtrl&amp;nbsp;AsyncProcessMessage(IMessage&amp;nbsp;msg,&amp;nbsp;IMessageSink&amp;nbsp;replySink);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IMessage&amp;nbsp;SyncProcessMessage(IMessage&amp;nbsp;msg);&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IMessageSink&amp;nbsp;NextSink&amp;nbsp;{&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;get&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;;&amp;nbsp;}&lt;BR&gt;}&lt;BR&gt;&lt;/DIV&gt;&lt;/SPAN&gt;&lt;/o:p&gt;&lt;/SPAN&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;The important thing to notice here is that each message sink has a reference to the next sink in the chain. Each sink forwards the message to the next sink in the chain and the last sink to handle the message would be the &lt;STRONG&gt;StackBuilderSink&lt;/STRONG&gt;. The client and the server are actually unaware of the intermediate processing; the remote call appears no different from a local (intra-appdomain) call. This&amp;nbsp;scheme forms the basis of the Aspect framework built around the &lt;STRONG&gt;ContextBoundObject&lt;/STRONG&gt;. In fact, I suppose, all aspect frameworks would eventually use a realization of the Chain of Responsibility pattern.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT face=Verdana color=#000080 size=1&gt;&lt;EM&gt;PS: It has been quite a while since I posted on patterns topic and I apologize for that. &lt;/EM&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=68256" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Manual ADO.NET Transactions and TLS</title><link>http://msmvps.com/blogs/manoj/archive/2005/09/18/66838.aspx</link><pubDate>Mon, 19 Sep 2005 00:24:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:66838</guid><dc:creator>Manoj G</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=66838</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2005/09/18/66838.aspx#comments</comments><description>&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT face=Verdana&gt;&lt;FONT color=#000080&gt;I have come across several business and data access layer components which have method overloads taking in SqlTransaction (or IDbTransaction) references. The need here is to facilitate ADO.NET manual transactions, where database operations scattered across several components would need to be enlisted under the same transaction. Let&amp;#8217;s assume for now that we do not want to avail the automatic transaction services of Enterprise Services or the DTC; performance being one reason, the unavoidable need to derive from System.EnterpriseServices.ServicedComponent being another. The obvious pain in the manual transaction scheme in our case is the leakage of ADO.NET objects (SqlTransaction) into the Business layer, which ideally, shouldn&amp;#8217;t be dealing with ADO.NET specific objects at all.&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face=Verdana color=#000080&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT face=Verdana&gt;&lt;FONT color=#000080&gt;So, what could be the solution to this problem? I thought &lt;STRONG&gt;TLS (Thread Local Storage)&lt;/STRONG&gt; could be put to good avail in this case. My idea involves the following steps:&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-spacerun: yes"&gt;1)&amp;nbsp;Create a helper class (call it TransactionUtils) with BeginTransaction, Commit and RollBack methods which do the work of opening and closing connections and storing transaction (SqlTransaction, for example) objects in the current thread&amp;#8217;s named data slot. This helper would be used by the business component which forms the root of the transaction.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-spacerun: yes"&gt;2)&amp;nbsp;Change the data access helper (or the DAAB) to check the current thread&amp;#8217;s slot for a transaction object and use it if it exists (append the current transaction to to the command object).&amp;nbsp; You could consider creating overloads which are &amp;#8216;TLS aware&amp;#8217;.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;This is what TransactionHelper would look like:&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;FONT face=Arial size=2&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;FONT face=Arial&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;
&lt;P style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #808080; FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;class&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;TransactionHelper&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;To&amp;nbsp;ensure&amp;nbsp;TransactionHelper&amp;nbsp;is&amp;nbsp;not&amp;nbsp;instantiated.&amp;nbsp;All&amp;nbsp;methods&amp;nbsp;are&amp;nbsp;static&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;TransactionHelper()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;Name&amp;nbsp;of&amp;nbsp;the&amp;nbsp;TLS&amp;nbsp;data&amp;nbsp;slot.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;const&amp;nbsp;string&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;TLS_SLOT_NAME&amp;nbsp;=&amp;nbsp;"IDbTransactionDataSlot";&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #808080; FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;Returns&amp;nbsp;a&amp;nbsp;boolean&amp;nbsp;value&amp;nbsp;which&amp;nbsp;indicates&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // whether&amp;nbsp;current&amp;nbsp;thread&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;is&amp;nbsp;in&amp;nbsp;a&amp;nbsp;transactional&amp;nbsp;context&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;static&amp;nbsp;bool&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;IsInTransaction()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;return&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;(GetTransactionFromContext()&amp;nbsp;!=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;null&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #808080; FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;Begins&amp;nbsp;an&amp;nbsp;ADO.NET&amp;nbsp;transaction&amp;nbsp;with&amp;nbsp;the&amp;nbsp;specified&amp;nbsp;isolation&amp;nbsp;level.&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #808080; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;Creates&amp;nbsp;a&amp;nbsp;TLS&amp;nbsp;data&amp;nbsp;slot&amp;nbsp;where&amp;nbsp;the&amp;nbsp;transaction&amp;nbsp;object&amp;nbsp;would&amp;nbsp;be&amp;nbsp;stored&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;static&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;BeginTransaction(IsolationLevel&amp;nbsp;isolationLevel,&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;string&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;connectionString)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//To&amp;nbsp;clear&amp;nbsp;off&amp;nbsp;the&amp;nbsp;transaction&amp;nbsp;slot,&amp;nbsp;if&amp;nbsp;in&amp;nbsp;use&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;FreeResources(GetTransactionFromContext());&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IDbConnection&amp;nbsp;conn&amp;nbsp;=&amp;nbsp;GetConnection(connectionString);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IDbTransaction&amp;nbsp;trans&amp;nbsp;=&amp;nbsp;conn.BeginTransaction(isolationLevel);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LocalDataStoreSlot&amp;nbsp;slot&amp;nbsp;=&amp;nbsp;Thread.AllocateNamedDataSlot(TLS_SLOT_NAME);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Thread.SetData(slot,&amp;nbsp;trans);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #808080; FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;Begins&amp;nbsp;an&amp;nbsp;ADO.NET&amp;nbsp;transaction.&amp;nbsp;Creates&amp;nbsp;a&amp;nbsp;TLS&amp;nbsp;data&amp;nbsp;slot&amp;nbsp;where&amp;nbsp;the&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #808080; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;transaction&amp;nbsp;object&amp;nbsp;would&amp;nbsp;be&amp;nbsp;stored&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #808080; FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;static&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;BeginTransaction(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;string&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;connectionString)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//To&amp;nbsp;clear&amp;nbsp;off&amp;nbsp;the&amp;nbsp;transaction&amp;nbsp;slot,&amp;nbsp;if&amp;nbsp;in&amp;nbsp;use&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;FreeResources(GetTransactionFromContext());&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IDbConnection&amp;nbsp;conn&amp;nbsp;=&amp;nbsp;GetConnection(connectionString);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IDbTransaction&amp;nbsp;trans&amp;nbsp;=&amp;nbsp;conn.BeginTransaction();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LocalDataStoreSlot&amp;nbsp;slot&amp;nbsp;=&amp;nbsp;Thread.AllocateNamedDataSlot(TLS_SLOT_NAME);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Thread.SetData(slot,&amp;nbsp;trans);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #808080; FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;Commits&amp;nbsp;the&amp;nbsp;current&amp;nbsp;transaction&amp;nbsp;and&amp;nbsp;frees&amp;nbsp;the&amp;nbsp;TLS&amp;nbsp;data&amp;nbsp;slot&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #808080; FONT-FAMILY: Courier New"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;static&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Commit()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IDbTransaction&amp;nbsp;trans&amp;nbsp;=&amp;nbsp;GetTransactionFromContext();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IDbConnection&amp;nbsp;conn&amp;nbsp;=&amp;nbsp;trans.Connection;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;trans.Commit();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;conn.Dispose();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FreeResources(trans);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #808080; FONT-FAMILY: Courier New"&gt;///&amp;nbsp;&lt;SUMMARY&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;///&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;Rolls&amp;nbsp;back&amp;nbsp;the&amp;nbsp;current&amp;nbsp;transaction&amp;nbsp;and&amp;nbsp;frees&amp;nbsp;the&amp;nbsp;TLS&amp;nbsp;data&amp;nbsp;slot&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #808080; FONT-FAMILY: Courier New"&gt;///&amp;nbsp;&lt;/SUMMARY&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;static&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Rollback()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IDbTransaction&amp;nbsp;trans&amp;nbsp;=&amp;nbsp;GetTransactionFromContext();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IDbConnection&amp;nbsp;conn&amp;nbsp;=&amp;nbsp;trans.Connection;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;trans.Rollback();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;conn.Dispose();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;FreeResources(trans);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;Gets&amp;nbsp;an&amp;nbsp;IDbTransaction&amp;nbsp;object&amp;nbsp;from&amp;nbsp;the&amp;nbsp;TLS&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;internal&amp;nbsp;static&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;IDbTransaction&amp;nbsp;GetTransactionFromContext()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;LocalDataStoreSlot&amp;nbsp;slot&amp;nbsp;=&amp;nbsp;Thread.GetNamedDataSlot(TLS_SLOT_NAME);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;return&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Thread.GetData(slot)&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;as&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;IDbTransaction;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;Disposes&amp;nbsp;the&amp;nbsp;transaction&amp;nbsp;and&amp;nbsp;clears&amp;nbsp;the&amp;nbsp;TLS&amp;nbsp;slot&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;static&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;FreeResources(IDbTransaction&amp;nbsp;trans)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;if&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;(trans&amp;nbsp;!=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;null&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;trans.Dispose();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Thread.FreeNamedDataSlot(TLS_SLOT_NAME);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;Gets&amp;nbsp;a&amp;nbsp;connection&amp;nbsp;object&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;static&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;IDbConnection&amp;nbsp;GetConnection(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;string&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;connectionString)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;IDbConnection&amp;nbsp;conn&amp;nbsp;=&amp;nbsp;SqlHelper.GetConnection(connectionString);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;conn.Open();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;return&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;conn;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;So, the snippet of a business component which uses the TransactionHelper would look something like:&lt;/FONT&gt;&lt;/P&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;class&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;MyBizComp&lt;BR&gt;{&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;DoSomeOperation()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;try&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;TransactionHelper.BeginTransaction(connectionString);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;Call&amp;nbsp;other&amp;nbsp;biz&amp;nbsp;components,&amp;nbsp;they&amp;nbsp;would&amp;nbsp;get&amp;nbsp;enlisted&amp;nbsp;within&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//&amp;nbsp;the&amp;nbsp;same&amp;nbsp;transaction&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;TransactionHelper.Commit();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;catch&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;TransactionHelper.Rollback();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;finally&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;Cleanup&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;BR&gt;}&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;So, we have a solution here, which is not that elegant, but atleast, sheilds the business layer from the nuances of ADO.NET. Note that the assumption here is that all the other business components eventually call those methods in the data access layer which are context (TLS) aware.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Two important things to note regarding this approach:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;1) The connection is kept open (behind the scenes) during the period between the BeginTran and Commit, and therefore during the execution of all the business components that are enlisted in a single transaction. So, it is imperative to keep transaction bracket as small as possible to improve concurrency. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;2) This methodology does not work in a multithreaded scenario, where different components in the call chain happen to execute on different threads. Obviously, because&amp;nbsp;TLS is thread specific.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT face=Verdana color=#000080&gt;&lt;A href="http://www.idesign.net/idesign/uploads/Transactions%20-%20WinForms%20TransContext.zip"&gt;&lt;STRONG&gt;&lt;FONT color=#0000ff&gt;Another similar implementation&lt;/FONT&gt;&lt;/STRONG&gt; &lt;/A&gt;you may want to look into is the transaction helper in Juval Lowy&amp;#8217;s site, IDesign.Net, where you have a helper class which does something similar, but wraps Enterprise Services.&amp;nbsp;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;
&lt;P&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT face=Verdana&gt;&lt;FONT color=#000080&gt;Come .NET 2.0, &lt;STRONG&gt;System.Transactions &lt;/STRONG&gt;provides an elegant way to &amp;#8220;scope&amp;#8221; a transaction in the business layer. Not so surprisingly, the current transaction (Ambient transaction) is persisted in the TLS. &lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT face=Verdana&gt;&lt;FONT color=#000080&gt;System.Transactions is one of the coolest new things in .NET 2.0 and will be the subject of another post altogether. &lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/P&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=66838" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Tip: Embedding Resources in Assemblies</title><link>http://msmvps.com/blogs/manoj/archive/2005/05/18/47380.aspx</link><pubDate>Wed, 18 May 2005 15:01:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:47380</guid><dc:creator>Manoj G</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=47380</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2005/05/18/47380.aspx#comments</comments><description>&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;[Problem]: Many a times, you end up using large string constants (usually placed in text files) in your application code. Typical examples are XSLT templates, SQL scripts and so on. It doesn&amp;#8217;t really look nice to have a string constant defined in a class which have a hundred lines of text to serve this purpose. You wouldn&amp;#8217;t want to store them separately in external text files either, as you wouldn&amp;#8217;t want users to see/edit the contents. &lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face=Verdana color=#000080&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;[A Solution]: One way to solve this issue is to embed the text file contents in the assembly as a resource and then extract them at runtime. &lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;To embed strings in the assembly, you can add the text file to the project, and in the properties of the file select &amp;#8220;Embedded Resource&amp;#8221; as the build action. Then, to extract the contents of the file from the assembly, you can call the &lt;STRONG&gt;GetManifestResourceStream&lt;/STRONG&gt; method on the Assembly instance, and read the stream into a string variable conveniently using StreamReader. You are just done!&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;FONT face=Verdana color=#000080&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P class=MsoNormal style="MARGIN: 0in 0in 0pt"&gt;&lt;SPAN style="FONT-SIZE: 10pt; FONT-FAMILY: Arial"&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;Of course, this method is justified if the strings you embed are quite large (say, at least a 1000 characters or so). Else, be happy to use class constants.&lt;o:p&gt;&lt;/o:p&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=47380" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Patterns in .NET: Citing the Strategy pattern</title><link>http://msmvps.com/blogs/manoj/archive/2005/04/04/40837.aspx</link><pubDate>Mon, 04 Apr 2005 20:17:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:40837</guid><dc:creator>Manoj G</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=40837</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2005/04/04/40837.aspx#comments</comments><description>&lt;FONT size=2&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;&lt;A href="http://www.dofactory.com/Patterns/PatternStrategy.aspx"&gt;&lt;STRONG&gt;&lt;FONT color=#0000ff&gt;Strategy&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/A&gt; &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;is a quite frequently used pattern. Strategy pattern involves "d&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;FONT face=Verdana color=#000080&gt;efining a family of algorithms, encapsulating each one, and making them interchangeable. Strategy lets the algorithm vary independently from clients that use it." &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Essentially strategy entails creating an abstract class or an interface abstracting the family of algorithms to be used. I must say that there are umpteen applications of this pattern within the BCL. Let's examine one of them here:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;&lt;STRONG&gt;IComparer&lt;/STRONG&gt; is an interface defined in the &lt;STRONG&gt;System.Collections&lt;/STRONG&gt; namespace and defines a method that can compare two objects. It's primary purpose is to provide a way to customize the sort order of a collection, which is required in the &lt;STRONG&gt;Array.Sort&lt;/STRONG&gt; and &lt;STRONG&gt;Array.BinarySearch&lt;/STRONG&gt; method implementations. So mapping this to the Strategy (refer to the link above for the class diagram of the pattern):&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Icomparer Interface : Strategy, Array class : Context&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;On similar lines, another citation would be &lt;STRONG&gt;IListSource / IList&lt;/STRONG&gt;, which is used in data binding in Webforms/Windows forms. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;I will leave it as an exercise to the adept .NET Fx developer to just add plenty more to this list!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=40837" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Patterns in .NET: Citing the Command Pattern</title><link>http://msmvps.com/blogs/manoj/archive/2005/04/01/40491.aspx</link><pubDate>Sat, 02 Apr 2005 04:10:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:40491</guid><dc:creator>Manoj G</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=40491</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2005/04/01/40491.aspx#comments</comments><description>&lt;FONT color=#800000&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Of late, I am getting more and more interested in Design Patterns. As Allan Shalloway, in his most excellent book, &lt;STRONG&gt;&lt;A href="http://www.amazon.com/exec/obidos/ASIN/0321247140/qid=1112379177/sr=2-1/ref=pd_bbs_b_2_1/103-7480467-8719051"&gt;&lt;FONT color=#0000ff&gt;Design Patterns Explained&lt;/FONT&gt;&lt;/A&gt;&lt;/STRONG&gt; explains, design patterns are a great way to understand Object Oriented Programming better. Though design patterns are not restricted to the seminal work from the GOF, they do provide a solid basis on how patterns, in general are conceived. I understand that there are umpteen books and resources on patterns, especially those in C# and VB.NET. What I am trying to do here try to cite certain patterns that exist with the BCL, albeit not clearly visible, and also reflect certain problem-solution pairs that we are all way too familiar with, but don&amp;#8217;t classify them as patterns. Let me start with an interesting problem we face quite often:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;You need to execute a method asynchronously. Now, the &lt;STRONG&gt;Thread &lt;/STRONG&gt;and the &lt;STRONG&gt;ThreadPool &lt;/STRONG&gt;class allow you to do that, but there is a catch. They accept only specific types of delegates - &lt;STRONG&gt;ThreadStart &lt;/STRONG&gt;and &lt;STRONG&gt;WaitCallback&lt;/STRONG&gt; respectively. Therefore, you cannot asynchronously call methods with arbitrary signatures directly. So, what's the solution? The answer is &lt;A href="http://www.dofactory.com/Patterns/PatternCommand.aspx"&gt;&lt;STRONG&gt;&lt;FONT color=#0000ff&gt;Command&lt;/FONT&gt;&lt;/STRONG&gt; &lt;/A&gt;Pattern. Essentially, in this pattern,&amp;nbsp;you encapsulate the command/operation to be executed as an object, with the method parameters represented by the state of command object. So, to apply the command pattern to our problem, we would need to create a command class that can execute any method, irrespective of the signature.&amp;nbsp;The best way to handle this is by using &lt;STRONG&gt;Delegate.DynamicInvoke&lt;/STRONG&gt; method. Therefore, the state of our Command object would contain a delegate instance and an object array, which is passed to the DynamicInvoke method and also, optionally, a result object. Here's how:&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;class&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Command&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;readonly&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Delegate&amp;nbsp;_target;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;readonly&amp;nbsp;object&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;[]&amp;nbsp;_args;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;object&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;_result;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Command(Delegate&amp;nbsp;d,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;object&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;[]&amp;nbsp;args)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_target&amp;nbsp;=&amp;nbsp;d;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_args&amp;nbsp;=&amp;nbsp;args;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;InvokeCommand()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;_result&amp;nbsp;=&amp;nbsp;_target.DynamicInvoke(_args);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;object&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Result&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;get&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;return&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;_result;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;}&lt;BR&gt;&lt;/DIV&gt;&lt;/SPAN&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Now, to execute this command/operation asynchronously, we can use the Thread/ThreadPool class. Given below is a helper that wraps the ThreadPool class. All you need to do is call the static method Invoke passing it an instance the command object. Once again, this command object represents the job to be executed. &lt;FONT face=Verdana color=#000080 size=2&gt;Note that the Command object is passed as a state object (second parameter) to the &lt;STRONG&gt;QueueUserWorkItem&lt;/STRONG&gt; method.&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//Just&amp;nbsp;a&amp;nbsp;wrapper&amp;nbsp;to&amp;nbsp;the&amp;nbsp;thread&amp;nbsp;pool&amp;nbsp;class&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;class&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;ThreadPoolHelper&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;static&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;WaitCallback&amp;nbsp;dynamicInvokeShim&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;WaitCallback(CommandInvoker);&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;static&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;CommandInvoker(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;object&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;o)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Command&amp;nbsp;command&amp;nbsp;=&amp;nbsp;(Command)&amp;nbsp;o;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;command.InvokeCommand();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;static&amp;nbsp;bool&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Invoke(Command&amp;nbsp;command)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;return&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;ThreadPool.QueueUserWorkItem(dynamicInvokeShim,&amp;nbsp;command);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;}&lt;BR&gt;&lt;/DIV&gt;&lt;/SPAN&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Now let's see how the Command is used (the 'Receiver'). Shown below is a totally arbitrary class using the command:&lt;/FONT&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;class&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;MyClass&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;delegate&amp;nbsp;int&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;AddDelegate(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;int&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;a,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;int&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;b);&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;TestCommandPattern()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Delegate&amp;nbsp;dlg&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;AddDelegate(Add);&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//Command&amp;nbsp;to&amp;nbsp;invoke&amp;nbsp;the&amp;nbsp;add&amp;nbsp;operation&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Command&amp;nbsp;command&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Command(dlg,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;object&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;[]&amp;nbsp;{5,&amp;nbsp;5});&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ThreadPoolHelper.Invoke(command);&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//Command&amp;nbsp;to&amp;nbsp;invoke&amp;nbsp;the&amp;nbsp;do&amp;nbsp;nothing&amp;nbsp;operation&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;command&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Command(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;ThreadStart(JustDoIt),&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;null&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ThreadPoolHelper.Invoke(command);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//Sample&amp;nbsp;method&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;int&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Add(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;int&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;a,&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;int&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;b)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;return&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;a&amp;nbsp;+&amp;nbsp;b;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//Sample&amp;nbsp;method&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;private&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;JustDoIt()&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine("Nothing!");&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;}&lt;BR&gt;&lt;/DIV&gt;&lt;/SPAN&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Now, many of you would be ready to pounce on me&amp;nbsp;with the suggestion that &lt;STRONG&gt;BeginInvoke/EndInvoke &lt;/STRONG&gt;combination on the delegate offers a similar solution, perhaps more cleanly &amp;amp;&amp;nbsp;efficiently. Well yes, I do agree. I gave&amp;nbsp;the above&amp;nbsp;example as a good illustration of the&amp;nbsp;command pattern and&amp;nbsp;moreover, this method&amp;nbsp;is still apt if you&amp;nbsp;employ the Thread class (Note the&amp;nbsp;BeginInvoke involves a threadpool thread)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000080&gt;&lt;FONT size=2&gt;&lt;FONT face=Verdana&gt;In my subsuquent posts, I shall try to cite more such application of patterns in .NET. If there are better examples, or if my citations are flawed in any way, please feel free to drop in your comments&lt;/FONT&gt;. &lt;FONT face=Verdana color=#000080&gt;Thanks!&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#000080 size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;/FONT&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=40491" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Stream Quips &amp; Traps</title><link>http://msmvps.com/blogs/manoj/archive/2005/03/22/39243.aspx</link><pubDate>Tue, 22 Mar 2005 19:19:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:39243</guid><dc:creator>Manoj G</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=39243</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2005/03/22/39243.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;I was working with &lt;STRONG&gt;SoapExtension&lt;/STRONG&gt;s and my project involved a lot of reading and writing to streams (Yes, in the &lt;STRONG&gt;ProcessMessage&lt;/STRONG&gt; method of the SoapExtension). I came across &lt;/FONT&gt;&lt;A href="http://www.yoda.arachsys.com/csharp/readbinary.html"&gt;&lt;STRONG&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;this good article&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/A&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;&amp;nbsp;by &lt;STRONG&gt;&lt;A href="http://www.yoda.arachsys.com/"&gt;&lt;FONT color=#0000ff&gt;Jon Skeet&lt;/FONT&gt;&lt;/A&gt;&lt;/STRONG&gt; which explains the reading of binary data from the stream. This article basically explains the best ways of using the Read method of the stream and also mentions about the potential errors that might creep up when &lt;STRONG&gt;Stream.Read&lt;/STRONG&gt; is used incorrectly. A related task is to copy the contents of one stream to another. Here's one of the ways of doing this:&lt;/FONT&gt;&lt;FONT face=Verdana color=#000080&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;static void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;CopyStream(Stream&amp;nbsp;inStream,&amp;nbsp;Stream&amp;nbsp;outStream)&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;byte&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;[]&amp;nbsp;buf&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;byte&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;[1024];&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;int&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;size&amp;nbsp;=&amp;nbsp;1024;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;int&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;count&amp;nbsp;=&amp;nbsp;0;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;count&amp;nbsp;=&amp;nbsp;inStream.Read(buf,0,size);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;while&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;(count&amp;nbsp;&amp;gt;&amp;nbsp;0)&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;outStream.Write(buf,0,count);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;count&amp;nbsp;=&amp;nbsp;inStream.Read(buf,0,size);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;outStream.Flush();&lt;BR&gt;&amp;nbsp;}&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;A more simple looking helper would involve the &lt;STRONG&gt;StreamReader&lt;/STRONG&gt; and the &lt;STRONG&gt;StreamWriter &lt;/STRONG&gt;as shown here&lt;STRONG&gt;:&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro" align=left&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;static void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;CopyStream(Stream&amp;nbsp;from,&amp;nbsp;Stream&amp;nbsp;to)&amp;nbsp;&lt;BR&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;TextReader&amp;nbsp;reader&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;StreamReader(from);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;TextWriter&amp;nbsp;writer&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;StreamWriter(to);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.WriteLine(reader.ReadToEnd());&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;writer.Flush();&lt;BR&gt;}&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;&lt;BR&gt;&lt;/P&gt;&lt;/SPAN&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Of course, if you 'reflect' the implementation behind these, you would find them to be very similar to the first CopyStream method. What is evident from this example is that it is definitely easier to use StreamReader and StreamWriter over using Stream directly. Also, you can work with strings directly as against using a byte array.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;I shall conclude this post by mentioning a few traps that one can usually fall into while using the Stream class. By definition, a stream is plainly, a sequence of bytes. The stream is read from start to finish in a serial fashion&amp;nbsp;(unless you do a seek operation)&amp;nbsp;and the &lt;STRONG&gt;Position &lt;/STRONG&gt;property provides the position where the current byte has to be read. Therefore, any operation involving the reading of the entire contents of the stream (like StreamReader.&lt;STRONG&gt;ReadToEnd&lt;/STRONG&gt;, for instance) would shift the position past the last byte read. So, reading the stream without resetting the position to zero causes erroneous behavior (I tried to load an XmlDocument using the Load method and I constantly kept getting "Root Element missing error"!)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Another aspect to keep in mind is that most streams (&lt;STRONG&gt;MemoryStream &lt;/STRONG&gt;is an exception) are buffered. Writing data to the stream does not mean that the data is transferred to the underlying media or device. We would need to call the Flush method to ensure the same. Again StreamWriter provides an &lt;STRONG&gt;AutoFlush&lt;/STRONG&gt; property which can be used when buffering semantics are not really required.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080&gt;Happy 'Stream'ing!&lt;/FONT&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=39243" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Contexts &amp; Interception revisited</title><link>http://msmvps.com/blogs/manoj/archive/2005/02/18/36315.aspx</link><pubDate>Fri, 18 Feb 2005 21:23:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:36315</guid><dc:creator>Manoj G</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=36315</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2005/02/18/36315.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;I have always been awed by the concept of &lt;STRONG&gt;Interception&lt;/STRONG&gt; and &lt;STRONG&gt;Aspect Oriented Programming &lt;/STRONG&gt;(AOP), especially after having seen and used COM+ Services. In very, very simple terms, AOP is about taking concerns or common aspects in the code (like Concurrency, Instrumentation, Security, etc), factoring them out, and weave them into the application in ways which do not create undue coupling. Interception is just a way of realizing AOP.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;The very fact that .NET provides an extensible framework for implementing your own interceptors is an exciting prospect. I would have loved to explain the whole framework myself, which mind you, is undocumented. But then, I came across some great resources, which&amp;nbsp;I'll just go ahead and mention instead:&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://objectsharp.com/Blogs/bruce/articles/198.aspx"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;&lt;STRONG&gt;Putting Attributes to Use&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;&lt;STRONG&gt; &lt;/STRONG&gt;&lt;/FONT&gt;
&lt;LI&gt;&lt;A href="http://msdn.microsoft.com/msdnmag/issues/02/03/AOP/default.aspx"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;&lt;STRONG&gt;Aspect-Oriented Programming Enables Better Code Encapsulation and Reuse &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/A&gt;
&lt;LI&gt;&lt;A href="http://msdn.microsoft.com/msdnmag/issues/03/03/ContextsinNET/default.aspx"&gt;&lt;FONT face=Verdana&gt;&lt;FONT color=#0000ff size=2&gt;&lt;STRONG&gt;Decouple Components by Injecting Custom Services into Your Object's Interception Chain &amp;#8211; Juval Lowy &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Also, make sure you give these books a good reading:&lt;/FONT&gt;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;&lt;A href="http://www.amazon.com/exec/obidos/tg/detail/-/0201734117/qid=1108722079/sr=5-1/ref=cm_lm_asin/103-9601051-2971835?v=glance"&gt;&lt;FONT color=#0000ff&gt;Essential .NET&lt;/FONT&gt; &lt;/A&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face=Verdana size=2&gt;&amp;#8211;&lt;/FONT&gt;&lt;FONT face=Verdana size=2&gt; Volume 1, Don Box (Chapter 7)&lt;/FONT&gt;&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;&lt;A href="http://www.amazon.com/exec/obidos/tg/detail/-/1590590252/qid=1108722079/sr=5-1/ref=cm_lm_asin/103-9601051-2971835?v=glance"&gt;&lt;FONT color=#0000ff&gt;Advanced .NET Remoting&lt;/FONT&gt; &lt;/A&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;FONT face=Verdana&gt;&lt;FONT size=2&gt;&amp;#8211; Ingo Rammer (Chapters 7, 11)&lt;/LI&gt;&lt;/OL&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Dabbling with the framework that allows you to pre and post process method calls can lead to interesting thought processes and implementations. Quite a while back, I found this great article - &lt;/FONT&gt;&lt;A href="http://www.codeproject.com/dotnet/declarativetransactions.asp"&gt;&lt;U&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;&lt;STRONG&gt;Declarative Transactions using ADO.NET and without Enterprise Services&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/U&gt;&lt;/A&gt;&lt;FONT face=Verdana color=#000080 size=2&gt; which showed how declarative (automatic) transactions could be envisaged around ADO.NET transactions. But the problem with the code sample was that it actually did not enlist SQL commands under a single transaction. What was really required was a context-sensitive &lt;STRONG&gt;SqlHelper&lt;/STRONG&gt; class. This is were I thought I could get my hands dirty with &lt;STRONG&gt;ContextBoundObject&lt;/STRONG&gt; and the like. I took the basic idea from the article forward and wrote a simpler version of the context attribute, sink and property classes. What I do here is store a &lt;STRONG&gt;SQLTransaction &lt;/STRONG&gt;object as a context property and have the sink do the opening/closing of the connection, calls BeginTransaction &amp;amp; Commit/Rollback. Of course, the implementation does not bear the complete semantics of COM+ DTC based transactions, and hence can be viewed from the academic perspective.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;I must say that code is shabby, does not have inline documentation and may require some sprucing up. Moreover, I have only made the &lt;STRONG&gt;ExecuteNonQuery&lt;/STRONG&gt; method context sensitive (it is called &lt;EM&gt;ContextExecuteNonQuery&lt;/EM&gt;). The remaining methods would simply need to follow suite. The entire code can be downloaded &lt;A href="http://www.msmvpsphotos.com/manojg/source/interception.zip"&gt;&lt;STRONG&gt;&lt;FONT color=#0000ff&gt;here&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/A&gt;. I apologize for not giving a comprehensive explanation, but I shall do it later sometime.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;It is really interesting to know where &amp;amp; how aspects can be applied in real-world problems. One place that immediately occurs to me, is Logging and Instrumentation. Juval Lowy, author of the most excellent book - &lt;STRONG&gt;&lt;FONT color=#0000ff&gt;&lt;A href="http://www.amazon.com/exec/obidos/ASIN/0596003471/qid=1108722079/sr=2-1/ref=pd_bbs_b_2_1/103-9601051-2971835"&gt;Programming .NET Components&lt;/A&gt;&lt;/FONT&gt; &lt;/STRONG&gt;presents an excellent sample which can be download &lt;A href="http://examples.oreilly.com/pnetcomp/PNC_Sources_for_.NET_1.0.zip"&gt;&lt;STRONG&gt;&lt;FONT color=#0000ff&gt;here&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/A&gt;. For other areas where interception can be of value, it is worth-while to look at COM+ services itself. Automatic Transactions, Security (Authentication and Authorization), Synchronization to mention a few.&lt;/FONT&gt;&lt;B&gt;&lt;/P&gt;&lt;/B&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=36315" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Singleton applications</title><link>http://msmvps.com/blogs/manoj/archive/2005/01/20/33332.aspx</link><pubDate>Fri, 21 Jan 2005 02:26:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:33332</guid><dc:creator>Manoj G</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=33332</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2005/01/20/33332.aspx#comments</comments><description>&lt;FONT color=#800000&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;I think it is a fairly common requirement to develop applications such that only a single instance runs at a time. This is where Mutexes, an inter-process thread synchronization primitive comes into picture. This is one good example where no other WaitHandle, but Mutexes only can be employed. For the implementation details, you can refer to Jon Skeet's most excellent &lt;/FONT&gt;&lt;A href="http://www.yoda.arachsys.com/csharp/faq/#one.application.instance"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;&lt;STRONG&gt;C# FAQ&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;/FONT&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana size=2&gt;. &lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;I believe another way is to check if &lt;STRONG&gt;Process.GetProcessByName&lt;/STRONG&gt;(&amp;#8220;&amp;lt;&amp;lt;App Name&amp;gt;&amp;gt;&amp;#8220;) returns a non-empty array. This approach may work fine, but I don't seem to like this personally.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT face=Verdana color=#000080&gt;Come Whidbey, you need not bother about using any of the above methodologies, as this is as easy as setting a project property!&lt;/FONT&gt;&lt;FONT color=#800000&gt; &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;/FONT&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=33332" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Path and Environment: Truly useful classes</title><link>http://msmvps.com/blogs/manoj/archive/2004/12/16/25862.aspx</link><pubDate>Thu, 16 Dec 2004 20:19:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:25862</guid><dc:creator>Manoj G</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=25862</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2004/12/16/25862.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;I always remember doing painful string manipulations on a file path string, either to extract the drive, the file name or the extension bits from the string in VB6. Things are definitely better in .NET. &lt;STRONG&gt;The System.IO.Path &lt;/STRONG&gt;class basically abstracts the way a path string is structured and provides a set of simple helper methods which also absolves a developer from knowing things like the directory separator character, Volume separator character and so on. &lt;/FONT&gt;&lt;A href="http://dotnetjunkies.com/WebLog/saravana/"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;&lt;STRONG&gt;Saravana&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana size=2&gt;,&amp;nbsp;&lt;/FONT&gt;&lt;FONT face=Verdana size=2&gt;a good friend of mine, explains more on this in his &lt;/FONT&gt;&lt;/FONT&gt;&lt;A href="http://www.extremeexperts.com/Net/CodeSnippets/Pathclass.aspx"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;&lt;STRONG&gt;article&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Another really useful class is &lt;STRONG&gt;System.Environment,&lt;/STRONG&gt; whichs comes packed with plentiful handy functions&amp;nbsp;to get environment information like environment variables, system directory, etc etc.&amp;nbsp;Check &lt;/FONT&gt;&lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemenvironmentclasstopic.asp"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;&lt;STRONG&gt;this&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;FONT size=2&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana&gt;&lt;FONT color=#0000ff&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/FONT&gt;MSDN link&amp;nbsp;&lt;/FONT&gt;&lt;FONT face=Verdana&gt;for a complete listing.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Really useful, I must say.&lt;/FONT&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=25862" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Xpath Expressions and XPathNavigator to the rescue</title><link>http://msmvps.com/blogs/manoj/archive/2004/12/10/Xpath-Expressions-and-XPathNavigator-to-the-rescue.aspx</link><pubDate>Fri, 10 Dec 2004 07:03:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:23240</guid><dc:creator>Manoj G</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=23240</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2004/12/10/Xpath-Expressions-and-XPathNavigator-to-the-rescue.aspx#comments</comments><description>&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;I would like to admit that I have, many a times, taken XML handling for granted and have come with ghastly pieces of code like the one shown below, where all I need is to find out the number of nodes in an Xml document. What I am doing here, is loading the Xml data it to a DOM, creating a node list and then accessing its Count property - all in all, an extremely expensive and needless process.&lt;/font&gt;&lt;/p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;&lt;div style="background-color:gainsboro;border:black 1px solid;padding:5px;"&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;booksDoc.LoadXml(booksXml);&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;int&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;numBooks&amp;nbsp;=&amp;nbsp;booksDoc.SelectNodes(&amp;quot;bookstore/book&amp;quot;).Count;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;for&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;(&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;int&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;i&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;i&amp;nbsp;&amp;lt;&amp;nbsp;numBooks;&amp;nbsp;i++)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#008000;font-family:Courier New;"&gt;//&amp;nbsp;Some&amp;nbsp;code&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/font&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;A much better and more efficient way to solve this problem is to use &lt;strong&gt;XPath Expressions &lt;/strong&gt;and the &lt;strong&gt;XPathNavigator &lt;/strong&gt;class. The XPathNavigator provides a method called &lt;strong&gt;Evaluate &lt;/strong&gt;which can evaluate Xpath expressions to return boolean, string, int values accordingly. As an alternative to the above code, we can just use the &lt;strong&gt;count &lt;/strong&gt;Xpath function to return the number of nodes in the node-set.&lt;/font&gt;&lt;/p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;&lt;div style="background-color:gainsboro;border:black 1px solid;padding:5px;"&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;XPathDocument&amp;nbsp;xpathDoc&amp;nbsp;=&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;new&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;XPathDocument(reader);&lt;br /&gt;XPathNavigator&amp;nbsp;navigator&amp;nbsp;=&amp;nbsp;xpathDoc.CreateNavigator();&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;int&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;numBooks&amp;nbsp;=&amp;nbsp;Convert.ToInt32(navigator.Evaluate(&amp;quot;count(bookstore/book)&amp;quot;));&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;for&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;(&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;int&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;i&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;i&amp;nbsp;&amp;lt;&amp;nbsp;numBooks;&amp;nbsp;i++)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#008000;font-family:Courier New;"&gt;//Some&amp;nbsp;code&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/font&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;The reason why the latter approach is efficient is that XPathDocument creates a read-only in-memory tree that is optimized for Xpath and Xslt. On the other hand, XmlNode is a modifiable DOM representation, which could make it inefficient for Xpath-like scenarios.&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;An example from &lt;strong&gt;Aaron Skonnard&amp;#39;s &lt;/strong&gt;&lt;strong&gt;&lt;font color="#0000ff"&gt;&lt;a href="http://msdn.microsoft.com/msdnmag/issues/03/02/XMLFiles/"&gt;&lt;font color="#0000ff"&gt;Xml Files column&lt;/font&gt;&lt;/a&gt; &lt;/font&gt;&lt;/strong&gt;illustrates this better. What the code sample is trying to do is find the sum price of invoice line items from the given Xml document.&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;The XmlDocument approach:&lt;/font&gt;&lt;/p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;&lt;div style="background-color:gainsboro;border:black 1px solid;padding:5px;"&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;const&amp;nbsp;string&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;EXPRESSION&amp;nbsp;=&amp;nbsp;&amp;quot;/Invoices/Invoice/LineItems/LineItem/Price/text()&amp;quot;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;static&amp;nbsp;void&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;UseDOM()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;XmlDocument&amp;nbsp;doc&amp;nbsp;=&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;new&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;XmlDocument();&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;doc.Load(&amp;quot;invoices.xml&amp;quot;);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;XmlNodeList&amp;nbsp;selection&amp;nbsp;=&amp;nbsp;doc.SelectNodes(EXPRESSION);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;double&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;total&amp;nbsp;=&amp;nbsp;0;&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;foreach&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;(XmlNode&amp;nbsp;n&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;in&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;selection)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;total&amp;nbsp;+=&amp;nbsp;XmlConvert.ToDouble(n.InnerText);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(&amp;quot;sum:&amp;nbsp;{0}&amp;quot;,&amp;nbsp;total);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/font&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;The better approach. Note the usage of the &lt;strong&gt;sum &lt;/strong&gt;Xpath expression.&lt;/font&gt;&lt;/p&gt;&lt;div style="background-color:gainsboro;border:black 1px solid;padding:5px;"&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;const&amp;nbsp;string&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;SUMEXPRESSION&amp;nbsp;=&amp;nbsp;&amp;quot;sum(/Invoices/Invoice/LineItems/LineItem/Price/text())&amp;quot;;&lt;br /&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;static&amp;nbsp;void&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;UseNavigatorEvaluate()&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;XPathDocument&amp;nbsp;doc&amp;nbsp;=&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;new&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;XPathDocument(&amp;quot;invoices.xml&amp;quot;);&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;XPathNavigator&amp;nbsp;nav&amp;nbsp;=&amp;nbsp;doc.CreateNavigator();&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(&amp;quot;sum:&amp;nbsp;{0}&amp;quot;,&amp;nbsp;nav.Evaluate(SUMBLOCKED EXPRESSION;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=23240" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Throw Trap</title><link>http://msmvps.com/blogs/manoj/archive/2004/10/24/16550.aspx</link><pubDate>Sun, 24 Oct 2004 12:55:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:16550</guid><dc:creator>Manoj G</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=16550</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2004/10/24/16550.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Often, in exception handling, we would need to re-throw an exception caught in the catch block, after doing some operation (logging the exception, for instance). But, we&amp;nbsp; generally make the mistake of providing the exception argument in the throw expression. The problem is that, in this case, we might lose the original call stack. Here's a small snippet to give you the idea:&lt;/FONT&gt;&lt;/P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;static&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;exA()&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;try&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;exB();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;catch&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;(Exception&amp;nbsp;ex)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//Watch&amp;nbsp;out!&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//throw&amp;nbsp;ex;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;//Better&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;throw&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&amp;nbsp;&lt;BR&gt;}&lt;BR&gt;&amp;nbsp;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;static&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;exB()&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;throw&amp;nbsp;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Exception("New&amp;nbsp;Exception");&amp;nbsp;&lt;BR&gt;}&lt;BR&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;static&amp;nbsp;void&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Main(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;string&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;[]&amp;nbsp;args)&amp;nbsp;&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;try&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;exA();&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;catch&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;(Exception&amp;nbsp;e)&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(e.StackTrace);&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;}&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;If we were to do a throw ex in the above snippet, then we would have the following stacktrace.&amp;nbsp; Note that we have lost exB() in the stack trace.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;exA()&lt;BR&gt;Main(String[] args)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;If we were to just re-throw the exception, then we would have got the entire stack trace&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;exB()&lt;BR&gt;exA()&lt;BR&gt;Main(String[] args)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;For more information refer to the C# Language Specification and Lance Hunt's C# Coding Guidelines for .NET.&lt;/FONT&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=16550" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Process.Start() Quips</title><link>http://msmvps.com/blogs/manoj/archive/2004/10/23/16505.aspx</link><pubDate>Sat, 23 Oct 2004 11:15:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:16505</guid><dc:creator>Manoj G</dc:creator><slash:comments>27</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=16505</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2004/10/23/16505.aspx#comments</comments><description>&lt;FONT face=Verdana color=#000080 size=2&gt;
&lt;P&gt;It is more than a common requirement to be able to start another process from executing code. For instance, you might want to execute a batch file, run a command line utility of sorts, and so on. This is facilitated in the BCL through &lt;STRONG&gt;System.Diagnostics.Process&lt;/STRONG&gt; class, more specifically, the &lt;STRONG&gt;Start&lt;/STRONG&gt; method.&lt;/P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;
&lt;P&gt;Process.Start("Notepad.exe");&lt;/P&gt;&lt;/FONT&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;
&lt;P&gt;You can also specify startup information like FileName, Argument, WindowStyle and so on, through the instance of another class called &lt;STRONG&gt;ProcessStartInfo.&lt;/STRONG&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=1&gt;&lt;FONT size=2&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;ProcessStartInfo&amp;nbsp;oStartInfo&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;ProcessStartInfo();&lt;BR&gt;oStartInfo.FileName&amp;nbsp;=&amp;nbsp;"Notepad.exe";&lt;BR&gt;oStartInfo.Arguments&amp;nbsp;=&amp;nbsp;"MyWork.txt";&lt;BR&gt;Process.Start(oStartInfo);&lt;BR&gt;&lt;/DIV&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=1&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Simple. But there is a catch here. The instance of ProcessInfo bears a property called &lt;STRONG&gt;UseShellExecute&lt;/STRONG&gt;. A value of true for this property would mean that the windows shell is used to start a process, and this is the default value. Therefore, you wouldn’t be able to extract the output or provide an alternative input for the process to be executed. This feature would be required, for instance, if you want to capture the output of a SQL script execution through osql.exe or as another example, the output of a command-line setup. So, to make this work, we need to set UseShellExecute to &lt;STRONG&gt;false &lt;/STRONG&gt;and then, as required, set &lt;STRONG&gt;RedirectStandardInput &lt;/STRONG&gt;(default is keyboard), &lt;STRONG&gt;RedirectStandardOutput &lt;/STRONG&gt;(default is monitor), &lt;STRONG&gt;RedirectStandardError&lt;/STRONG&gt; (default is monitor) properties to true. One thing to keep in mind, in this case&amp;nbsp;is that, you should specifiy the full path of the executable or the path must be listed in the PATH environment variable. The code snippet given below captures the execution of a batch file and writes it to a log file:&lt;/FONT&gt;&lt;/P&gt;&lt;FONT size=2&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Process&amp;nbsp;runCmd&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Process();&lt;BR&gt;runCmd.StartInfo.FileName&amp;nbsp;=&amp;nbsp;"RunBatch.bat";&lt;BR&gt;runCmd.StartInfo.UseShellExecute&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;false&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;;&lt;BR&gt;runCmd.StartInfo.RedirectStandardOutput&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;true&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;;&lt;BR&gt;runCmd.Start();&amp;nbsp;&lt;BR&gt;&lt;BR&gt;FileStream&amp;nbsp;fs&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;FileStream("OutPut.log",FileMode.Append);&lt;BR&gt;StreamWriter&amp;nbsp;sw&amp;nbsp;=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;StreamWriter(fs);&lt;BR&gt;sw.Write(runCmd.StandardOutput.ReadToEnd());&lt;BR&gt;runCmd.WaitForExit();&lt;BR&gt;&lt;BR&gt;sw.Close();&lt;BR&gt;fs.Close();&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=1&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Another simple tip is that, when UseShellExecute is true, you can start any document if its file type is associated with an executable that has the default action as open. For e.g. The following code launches test.txt in default text editor (notepad on my m/c)&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;FONT size=2&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;ProcessStartInfo&amp;nbsp;psi=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;ProcessStartInfo(@"C:\test.txt");&lt;BR&gt;Process.Start(psi);&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;You can say also associate verbs like &lt;STRONG&gt;Print &lt;/STRONG&gt;etc.&lt;/FONT&gt;&lt;/P&gt;&lt;FONT size=2&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;ProcessStartInfo&amp;nbsp;psi=&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;new&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;ProcessStartInfo(@"C:\test.txt");&lt;BR&gt;psi.verb&amp;nbsp;=&amp;nbsp;"Print";&lt;BR&gt;Process.Start(psi);&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=1&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;As another example, the following code launches the URL provided in the default browser.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;Process.Start("http://www.microsoft.com");&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=16505" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Tip: StrongNameIdentityPermission </title><link>http://msmvps.com/blogs/manoj/archive/2004/10/20/16208.aspx</link><pubDate>Wed, 20 Oct 2004 20:04:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:16208</guid><dc:creator>Manoj G</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=16208</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2004/10/20/16208.aspx#comments</comments><description>&lt;P&gt;&lt;FONT color=#000080&gt;&lt;FONT face=Verdana size=2&gt;Many a times in your application, you would want to have a security scheme which would ensure that your code is not referenced and misused by other assemblies. For instance, there might be public types representing custom Identities and Principals which you wouldn&amp;#8217;t want other applications to use, or some sensitive/proprietary logic, you name it. .NET BCL does provide a handy class &lt;/FONT&gt;&lt;FONT face=Verdana size=2&gt;&lt;STRONG&gt;StrongNameIdentityPermission&lt;/STRONG&gt; which gives you a simple scheme for effecting the same.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;You can refer to &lt;STRONG&gt;&lt;A href="http://www.morganskinner.com/Articles/StrongNameIdentityPermission/"&gt;&lt;FONT color=#0000ff&gt;this&lt;/FONT&gt;&lt;/A&gt;&lt;/STRONG&gt; walkthrough for more information on how this permission can be used. &lt;/FONT&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Also, check &lt;STRONG&gt;&lt;A href="http://blogs.msdn.com/tims/archive/2003/12/03/57463.aspx"&gt;&lt;FONT color=#0000ff&gt;this&lt;/FONT&gt;&lt;/A&gt;&lt;/STRONG&gt; blog entry and the related discussion for more insights.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;&lt;/FONT&gt;&amp;nbsp;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=16208" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Indexer Insights</title><link>http://msmvps.com/blogs/manoj/archive/2004/10/15/15891.aspx</link><pubDate>Fri, 15 Oct 2004 11:29:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:15891</guid><dc:creator>Manoj G</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=15891</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2004/10/15/15891.aspx#comments</comments><description>&lt;FONT face=Verdana color=#000080 size=2&gt;
&lt;P&gt;We all know that types, including interfaces can have a default property. Default properties are used as a short-hand notation for accessing items in an array/collection of objects contained within the object. For e.g. &lt;STRONG&gt;Item&lt;/STRONG&gt; is the default property of an ArrayList (for that matter, IList.Item) which gets the object stored in the specified index. For eg :&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;Dim&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;arrItems&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;As&amp;nbsp;New&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;ArrayList&lt;BR&gt;Console.WriteLine(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;CType&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;(arrItems(0),&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;String&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;))&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;'Equivalent&amp;nbsp;to&amp;nbsp;this&lt;BR&gt;'Console.WriteLine(CType(arrItems.Item(0),&amp;nbsp;String))&lt;BR&gt;&lt;/DIV&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;
&lt;P&gt;In C#, default properties are called &lt;STRONG&gt;Indexers&lt;/STRONG&gt;. There is a subtle difference in the way indexers are declared in VB.NET and C#. Here's an example (MSDN):&lt;/P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#008000 size=1&gt;&lt;FONT size=2&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;'&amp;nbsp;Visual&amp;nbsp;Basic&lt;BR&gt;'&amp;nbsp;A&amp;nbsp;class&amp;nbsp;that&amp;nbsp;contains&amp;nbsp;many&amp;nbsp;Widgets&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;Public&amp;nbsp;Class&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Widgets&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;'Default&amp;nbsp;property&amp;nbsp;implementation&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;Default&amp;nbsp;Public&amp;nbsp;Property&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Widget(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;ByVal&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;I&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;As&amp;nbsp;Integer&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;)&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;As&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Widget&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;Get&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;'&amp;nbsp;Implementation&amp;nbsp;code&amp;nbsp;to&amp;nbsp;return&amp;nbsp;a&amp;nbsp;widget&amp;nbsp;goes&amp;nbsp;here.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;End&amp;nbsp;Get&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Set&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;ByVal&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Value&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;As&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Widget)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;'&amp;nbsp;Implementation&amp;nbsp;code&amp;nbsp;to&amp;nbsp;set&amp;nbsp;a&amp;nbsp;widget&amp;nbsp;goes&amp;nbsp;here.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;End&amp;nbsp;Set&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;End&amp;nbsp;Property&lt;BR&gt;End&amp;nbsp;Class&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#0000ff&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT color=#800000&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#008000&gt;&lt;FONT size=2&gt;&lt;/P&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;C#&lt;BR&gt;//&amp;nbsp;Class&amp;nbsp;that&amp;nbsp;contains&amp;nbsp;many&amp;nbsp;Widgets.&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;class&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Widgets&amp;nbsp;&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;Indexer&amp;nbsp;implementation.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;Widget&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;this&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;[&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;int&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;index]&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;get&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;Insert&amp;nbsp;code&amp;nbsp;to&amp;nbsp;return&amp;nbsp;a&amp;nbsp;Widget.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;set&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #008000; FONT-FAMILY: Courier New"&gt;//&amp;nbsp;Insert&amp;nbsp;code&amp;nbsp;to&amp;nbsp;set&amp;nbsp;a&amp;nbsp;Widget.&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;}&lt;BR&gt;&lt;/DIV&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;/FONT&gt;&lt;FONT color=#800000&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;If you notice, there is no provision to specify the name of the Indexer in C#, as present in VB.NET. In C#, the name defaults to "&lt;STRONG&gt;Item&lt;/STRONG&gt;". However, you can specify a name&amp;nbsp;by means of an attribute as shown below:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;[System.Runtime.CompilerServices.CSharp.IndexerName("Widget")]&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;public&amp;nbsp;int&amp;nbsp;this&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;[&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;int&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;index]&amp;nbsp;{&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&lt;/DIV&gt;
&lt;P&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;FONT color=#800000&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Here are some other aspects of an Indexer we need to know:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;1. You can have only one Default property/Indexer per class. The Indexer, however, can be overloaded.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;2. Default properties should accept parameters. We cannot have VB6 like (parameterless) properties. Refer to &lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcn7/html/vaconDefaultProperties.asp"&gt;&lt;FONT color=#0000ff&gt;&lt;STRONG&gt;this link&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/A&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#800000&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;for Property changes in VB.NET as compared to VB.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;/FONT&gt;&lt;FONT color=#000080&gt;&lt;FONT size=2&gt;&lt;FONT face=Verdana&gt;Finally, a tip to end this note on indexers; a frequently asked question in the newsgroups. If we need to find out programmatically, if a class has an indexer, we can query a custom attribute called &lt;STRONG&gt;DefaultMemberAttribute&lt;/STRONG&gt; attribute using Reflection as shown below:&lt;/FONT&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#800000&gt;&lt;/FONT&gt;&lt;FONT face="Courier New" color=#0000ff size=1&gt;&lt;FONT size=2&gt;
&lt;DIV style="BORDER-RIGHT: black 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: black 1px solid; PADDING-LEFT: 5px; PADDING-BOTTOM: 5px; BORDER-LEFT: black 1px solid; PADDING-TOP: 5px; BORDER-BOTTOM: black 1px solid; BACKGROUND-COLOR: gainsboro"&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;static&amp;nbsp;bool&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;CheckIfIndexerPresent(Object&amp;nbsp;obj)&amp;nbsp;&lt;BR&gt;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;object&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;[]&amp;nbsp;oAttributes&amp;nbsp;=&amp;nbsp;obj.GetType().GetCustomAttributes(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;true&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;);&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;for&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;(&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;short&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;j&amp;nbsp;=&amp;nbsp;0;&amp;nbsp;j&amp;nbsp;&amp;lt;&amp;nbsp;oAttributes.Length;&amp;nbsp;j++)&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;if&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;(oAttributes[j]&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;is&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;DefaultMemberAttribute)&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;return&amp;nbsp;true&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&amp;nbsp;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #0000ff; FONT-FAMILY: Courier New"&gt;return&amp;nbsp;false&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; COLOR: #000000; FONT-FAMILY: Courier New"&gt;;&lt;BR&gt;}&lt;BR&gt;&lt;/DIV&gt;&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;&lt;FONT size=2&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=15891" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Tip: Checking whether an Assembly is compiled in Debug or Release Configuration</title><link>http://msmvps.com/blogs/manoj/archive/2004/10/12/Tip_3A00_-Checking-whether-an-Assembly-is-compiled-in-Debug-or-Release-Configuration.aspx</link><pubDate>Tue, 12 Oct 2004 14:09:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:15580</guid><dc:creator>Manoj G</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=15580</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2004/10/12/Tip_3A00_-Checking-whether-an-Assembly-is-compiled-in-Debug-or-Release-Configuration.aspx#comments</comments><description>&lt;font color="#000080" face="Verdana" size="2"&gt;&lt;p&gt;Often, it may be necessary to inspect whether a particular assembly was compiled with a Debug configuration (or Release configuration). For instance, you might want to run a tool of sorts on a build folder to ensure that all assemblies are compiled in the Release configuration. The reason being that - in Debug mode, certain JIT optimizations are turned off (to aid debugging) and you would want them to be turned on for the production environment.&lt;/p&gt;&lt;/font&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;&lt;p&gt;We can inspect this by checking for an attribute &lt;strong&gt;DebuggableAttribute&lt;/strong&gt; (System.Diagnostics namespace) in the assembly metadata. The compiler would automatically qualify the assembly/module with this attribute when compiled in the debug configuration. All we need to do is use Reflection to extract this information out. Given below is a C# code snippet for the same &lt;em&gt;&lt;font face="Courier New"&gt;[Courtesy: Nick Weinholt (MVP), News Groups]&lt;/font&gt;&lt;/em&gt;: &lt;/p&gt;&lt;/font&gt;&lt;font color="#800000" size="2"&gt;&lt;/font&gt;&lt;font color="#0000ff"&gt;&lt;font face="Courier New" size="2"&gt;&lt;div style="background-color:gainsboro;border:black 1px solid;padding:5px;"&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;static&amp;nbsp;bool&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;IsDebugMode(Assembly&amp;nbsp;objAssembly)&amp;nbsp;&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;object&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;[]&amp;nbsp;atts&amp;nbsp;=&amp;nbsp;objAssembly.GetCustomAttributes(&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;typeof&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;(DebuggableAttribute),&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;true&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;if&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;(atts.Length&amp;nbsp;==&amp;nbsp;0)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(&amp;quot;This&amp;nbsp;is&amp;nbsp;a&amp;nbsp;Release&amp;nbsp;Configuration&amp;quot;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;return&amp;nbsp;false&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;DebuggableAttribute&amp;nbsp;da&amp;nbsp;=&amp;nbsp;atts[0]&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;as&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;DebuggableAttribute;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;if&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;(da&amp;nbsp;!=&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;null&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;bool&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;standardDebugBuild&amp;nbsp;=&amp;nbsp;da.IsJITOptimizerDisabled&amp;nbsp;&amp;amp;&amp;amp;&amp;nbsp;da.IsJITTrackingEnabled;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Console.WriteLine(&amp;quot;Standard&amp;nbsp;DebugBuild&amp;nbsp;=&amp;nbsp;{0}&amp;quot;,&amp;nbsp;standardDebugBuild);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;return&amp;nbsp;true&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;return&amp;nbsp;false&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#800000" size="2"&gt;&lt;/font&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=15580" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>String.Format: A Simple Tip</title><link>http://msmvps.com/blogs/manoj/archive/2004/09/29/String.Format_3A00_-A-Simple-Tip.aspx</link><pubDate>Wed, 29 Sep 2004 12:43:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:14551</guid><dc:creator>Manoj G</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=14551</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2004/09/29/String.Format_3A00_-A-Simple-Tip.aspx#comments</comments><description>&lt;p&gt;&lt;font color="#000099" face="verdana,geneva"&gt;More often than not, we end up concatenating strings in our application code, and a bit aggressively. Sometimes, these concatenations can get a ugly, resulting into a mire of single quotes, double quotes and escape sequences. A simple case of not-so lucid concatenation is shown below:&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000099" face="verdana,geneva"&gt;string consoleMessage = &amp;quot;Time taken by operation: &amp;quot; + operation + &amp;quot; under category: &amp;quot; + category + &amp;quot; :&amp;quot; + time.ToString() + &amp;quot; ms&amp;quot;; &lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000099" face="verdana,geneva"&gt;A much better approach would be to use the &lt;strong&gt;string.Format&lt;/strong&gt; method which makes concatenations much more readable and less error prone. The same example can be replaced with:&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000099" face="verdana,geneva"&gt;string consoleMessage = string.Format(&amp;quot;Time taken by operation: {0} under category {1}: {2} ms&amp;quot;, operation, category, time);&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000099" face="verdana,geneva"&gt;Simple. Create placeholders by inserting {n} into the string, where n describes the position of the replacement parameter.The &lt;strong&gt;StringBuilder&lt;/strong&gt; too comes with the &lt;strong&gt;AppendFormat &lt;/strong&gt;method which serves the same purpose. Now, here is a small catch. What happens if the format string itself has &amp;quot;{&amp;quot; or &amp;quot;}&amp;quot;?&lt;br /&gt;&amp;nbsp;&lt;br /&gt;strProblemFormat = &amp;quot;{Now, this is a problem}, {0}&amp;quot;; &lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000099" face="verdana,geneva"&gt;In this case, you would end up getting a FormatException. The solution however, is simple. Escape &amp;quot;{&amp;quot; with &amp;quot;{{&amp;quot; and &amp;quot;}&amp;quot; with &amp;quot;}}&amp;quot; in your format string.&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000099" face="verdana,geneva"&gt;string strProblemFormat = &amp;quot;{{Now, this is a problem}}, {0}&amp;quot;; &lt;br /&gt;string strDisplay = string.Format(strProblemFormat, &amp;quot;Not anymore!&amp;quot;);&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000099" face="verdana,geneva" size="1"&gt;&lt;em&gt;[Updated] I got a lot of flak for chosing a SQL example for string concatenation! As many of the comments rightly indicate, concatenations on SQL statements could potentially invite SQL injection attacks. Parameterized SQL in such cases is a better choice. Thanks for the feedback! &lt;/em&gt;&lt;/font&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=14551" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>Getting the best of the two (otherwise mutually exclusive) worlds together!</title><link>http://msmvps.com/blogs/manoj/archive/2004/08/26/Getting-the-best-of-the-two-_2800_otherwise-mutually-exclusive_2900_-worlds-together_2100_.aspx</link><pubDate>Thu, 26 Aug 2004 09:01:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:12394</guid><dc:creator>Manoj G</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=12394</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2004/08/26/Getting-the-best-of-the-two-_2800_otherwise-mutually-exclusive_2900_-worlds-together_2100_.aspx#comments</comments><description>&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;We generally don&amp;rsquo;t talk about .NET and J2EE together unless, it&amp;#39;s a heated debate as to which is better and why. &lt;/font&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;Recently, I came across two very interesting tools which, sort of glues the otherwise mutually exclusive worlds together. The first of which is &lt;strong&gt;MainSoft Visual MainWin&lt;/strong&gt;,&amp;nbsp;&lt;/font&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;a tool which helps C# and VB.NET developers to develop applications for J2EE platform! Do have a look at the published whitepapers &lt;a href="http://mainsoft.com/products/vmw_j2ee.html"&gt;&lt;strong&gt;&lt;font color="#0000ff"&gt;here&lt;/font&gt;&lt;/strong&gt;&lt;/a&gt;&amp;nbsp;to see how the magic really happens.&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;Just to summarize, MainWin integrates with VS.NET seamlessly and has a compiler which converts MSIL code to Java Bytecode, which eventually executes on a JVM. &lt;/font&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;Two impressive aspects of the tool are :&lt;/font&gt;&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;Accessing JDK classes and EJB components from C# and VB.NET. &lt;/font&gt;&lt;/li&gt;&lt;li&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;Debugging : MainWin allows you to debug C#/VB.NET applications running on a JVM, using the full gamut of debugging features that VS.NET provides. &lt;/font&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;This is really interesting, considering the amount of plumbing that might be required at runtime.&lt;/font&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;Isn&amp;#39;t this testimonial to the fact that VS.NET is a &amp;ldquo;high productivity&amp;rdquo; developer tool? In fact, the first tenet mentioned in&amp;nbsp;the whitepaper of MainWin is &amp;quot;&lt;em&gt;Preserve the Visual Studio Developer Experience&lt;/em&gt;&amp;quot;.&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font size="2"&gt;&lt;font color="#000080" face="Verdana"&gt;The next really interesting tool was &lt;strong&gt;&lt;a href="http://www.ikvm.net/index.html"&gt;&lt;font color="#0000ff"&gt;IKVM.NET&lt;/font&gt;&lt;/a&gt;&lt;/strong&gt;, which basically is a JVM for .NET and Mono runtimes.&amp;nbsp;&lt;/font&gt;&lt;/font&gt;&lt;font face="Verdana"&gt;&lt;font color="#000080" size="2"&gt;So, this is sort of a converse technology to MainWin. Using IKVM.NET, one can run Java applications on the .NET runtime by converting Java Bytecode to CIL. &lt;/font&gt;&lt;font color="#000080" size="2"&gt;IKVM comes with an implementation of GNU Classpath in .NET. So, normal C# apps can leverage the entire Java class library. Also, IKVM.NET comes with a tool which converts .NET assemblies to Jar files which can be consumed by Java applications. &lt;/font&gt;&lt;/font&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;You can also check out the IKVM.NET weblog &lt;strong&gt;&lt;a href="http://weblog.ikvm.net"&gt;&lt;font color="#0000ff"&gt;here&lt;/font&gt;&lt;/a&gt;&lt;/strong&gt;.&lt;/font&gt;&lt;/p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;&lt;p&gt;I have not really worked with IKVM.NET nor MainWin, or for that matter Java/J2EE. So, I am not in a position to really justify the practical value of these tools (I guess, there should be). But I thought it is worth mentioning about these because, if not for anything else, they can really be appreciated&amp;nbsp;from the academic perspective !&lt;/p&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;&lt;/font&gt;&lt;/p&gt;&lt;/font&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;&lt;/font&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=12394" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>CallContext</title><link>http://msmvps.com/blogs/manoj/archive/2004/07/30/CallContext.aspx</link><pubDate>Fri, 30 Jul 2004 13:26:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:10949</guid><dc:creator>Manoj G</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=10949</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2004/07/30/CallContext.aspx#comments</comments><description>&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;Many a times, it would be necessary to share some common information/data between methods that form a call chain. For example, consider a call chain like the one shown below:&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;A(); --&amp;gt; B() --&amp;gt; C() --&amp;gt; D(); &lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;And lets say B,C and D need to access some information created in A. Lets look at some common ways of doing it&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;1. Create placeholder parameters in all methods.&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;This is the least elegant solution in most situations, especially in cases where that data is optional. Creating overloads might clutter up the class definition.&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;2. Using static members/properties&lt;/font&gt;&lt;/p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;&lt;font color="#0000ff" size="1"&gt;&lt;div style="background-color:gainsboro;border:black 1px solid;padding:5px;"&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;class&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;DataStore&lt;br /&gt;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;private&amp;nbsp;static&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;Hashtable&amp;nbsp;_slots&amp;nbsp;=&amp;nbsp;Hashtable.Synchronized(&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;new&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;Hashtable());&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;public&amp;nbsp;static&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;Hashtable&amp;nbsp;Slots&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;get&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;{&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#0000ff;font-family:Courier New;"&gt;return&amp;nbsp;&lt;/span&gt;&lt;span style="font-size:10pt;color:#000000;font-family:Courier New;"&gt;_slots;&amp;nbsp;}&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;}&lt;/span&gt;&lt;/div&gt;&lt;/font&gt;&lt;/font&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;&lt;br /&gt;Static instances live until the AppDomain unloads and can be used to house&amp;nbsp;data, and they would be used through the Singleton pattern. This may be a better solution than creating as many parameters in each method in the call stack.&amp;nbsp;But,&amp;nbsp;you&amp;nbsp;would need to be careful&amp;nbsp;with its design and usage as you may need to synchronize access to a static member in a multi-threaded environment. It would be nice to have a framework expose a common data store which could be accessed across methods. CallContext is just that.&lt;/font&gt; &lt;p&gt;&lt;font color="#000080"&gt;&lt;font face="Verdana" size="2"&gt;The &lt;strong&gt;CallContext&lt;/strong&gt; class&amp;nbsp;&lt;/font&gt;&lt;font face="Verdana" size="2"&gt;provides data slots that are unique to each thread of execution. You can use the &lt;strong&gt;SetData &lt;/strong&gt;and &lt;strong&gt;GetData&lt;/strong&gt; methods to create and retrieve values from named place holders. The usage seems very similar to that of named/unnamed data slots of the &lt;strong&gt;Thread &lt;/strong&gt;class. So, you may ask - When do I use which method? The answer really lies in the namespace CallContext lies in : &lt;strong&gt;System.Runtime.Remoting.Messaging &lt;/strong&gt;and this is where the similarity ends. Using CallContext, data can be passed along with method calls that are remoted (which execute in different a AppDomain). However, to make this work, the classes of those objects have to implement &lt;strong&gt;ILogicalThreadAffinative&lt;/strong&gt; interface. This interface is just a marker and does not contain any methods to be implemented. And of course, the objects should be serializable.&lt;/font&gt;&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;One good use of CallContext could be a case where a caller&amp;#39;s Identity information could be stored in the call context. The remote method call could access this information to implement some sort of a Role-based Security. &lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;You can also have a look at &lt;a href="http://dotnet.org.za/armand/archive/2004/04/07/985.aspx"&gt;&lt;strong&gt;&lt;font color="#0000ff"&gt;this&lt;/font&gt;&lt;/strong&gt; &lt;/a&gt;well-written entry on CallContext.&amp;nbsp;For more information and samples, refer to MSDN.&lt;/font&gt;&lt;/p&gt;&lt;p&gt;&lt;font color="#000080" face="Verdana" size="2"&gt;&lt;/font&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=10949" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item><item><title>XML API - When to use What?</title><link>http://msmvps.com/blogs/manoj/archive/2004/07/01/9331.aspx</link><pubDate>Thu, 01 Jul 2004 17:23:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:9331</guid><dc:creator>Manoj G</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/manoj/rsscomments.aspx?PostID=9331</wfw:commentRss><comments>http://msmvps.com/blogs/manoj/archive/2004/07/01/9331.aspx#comments</comments><description>&lt;P&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;I came across &lt;/FONT&gt;&lt;A href="http://msdn.microsoft.com/msdntv/episode.aspx?xml=episodes/en/20030603XMLDB/manifest.xml"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;&lt;STRONG&gt;this&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/A&gt;&amp;nbsp;&lt;FONT size=2&gt;&lt;FONT face=Verdana&gt;&lt;FONT color=#000080&gt;old, but excellent MSDN TV presentation, by Don Box, on ways of using XML API or passing XML data around within an Appdomain. The presentation starts off by discussing pros and cons of passing XML in the form of &lt;STRONG&gt;XmlNode &lt;/STRONG&gt;and &lt;STRONG&gt;XmlReader &lt;/STRONG&gt;types. In a gist, XmlNode is very easy to use, but taxing on memory as it requires you to create an in-memory representation (DOM) of the Xml data. Also the XmlNode is not read only and you can change the cached Xml structure by calling methods like SetAttribute, AppendChild etc. XmlReader is a better approach in terms of performance, is read only, but is very hard to use.&amp;nbsp;So, the approach that can give the best of the two worlds is &lt;/FONT&gt;&lt;FONT color=#000080&gt;&lt;STRONG&gt;XpathNavigator&lt;/STRONG&gt; present in System.Xpath namespace. The advantage of the XPathNavigator is that it is read only, extensible, and does not impose an representation constraint like DOM. Classes can override &lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT color=#000000&gt;&lt;FONT size=3&gt;&lt;FONT face=Verdana&gt;&lt;FONT color=#000080&gt;&lt;FONT size=2&gt;XPathNavigator class&amp;nbsp;and provide their own representations. Excellent examples&amp;nbsp;are - creating a navigator to traverse a File system directory structure or Registry structure&amp;nbsp;using the XpathNavigator semantics. You can download the implementation &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;A href="http://www.theserverside.net/developmentor/downloads/samples/CustomNavigatorsReaders.zip"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;&lt;STRONG&gt;here&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;.&lt;/FONT&gt;&lt;FONT face="Times New Roman"&gt; &lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face=Verdana color=#000080 size=2&gt;Also, go through &lt;/FONT&gt;&lt;A href="http://support.softartisans.com/kbview.aspx?ID=673"&gt;&lt;FONT face=Verdana color=#0000ff size=2&gt;&lt;STRONG&gt;this&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/A&gt;&amp;nbsp;&lt;FONT face=Verdana color=#000080 size=2&gt;fantastic article by Aaron Skonnard relating to the same discussion.&lt;/FONT&gt;&lt;/P&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=9331" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/manoj/archive/tags/.NET/default.aspx">.NET</category></item></channel></rss>