<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ravi Singh</title>
	<atom:link href="http://ravidotnet.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://ravidotnet.wordpress.com</link>
	<description>professional website and socket server developer.</description>
	<lastBuildDate>Tue, 10 Mar 2009 11:16:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='ravidotnet.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Ravi Singh</title>
		<link>http://ravidotnet.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://ravidotnet.wordpress.com/osd.xml" title="Ravi Singh" />
	<atom:link rel='hub' href='http://ravidotnet.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Socket Performance Enhancements in Version 3.5</title>
		<link>http://ravidotnet.wordpress.com/2009/03/10/socket-performance-enhancements-in-version-35/</link>
		<comments>http://ravidotnet.wordpress.com/2009/03/10/socket-performance-enhancements-in-version-35/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 09:42:40 +0000</pubDate>
		<dc:creator>mcamail2002</dc:creator>
				<category><![CDATA[Network Programming]]></category>

		<guid isPermaLink="false">http://ravidotnet.wordpress.com/?p=37</guid>
		<description><![CDATA[The SocketAsyncEventArgs class is part of a set of enhancements to the System.Net.Sockets&#8230;: .Socket class that provides an alternative asynchronous pattern that can be used by specialized high-performance socket applications. This class was specifically designed for network server applications that require high performance. An application can use the enhanced asynchronous pattern exclusively or only in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=37&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0 0 6.8pt;"><span style="font-size:10pt;color:black;font-family:&quot;">The <strong>SocketAsyncEventArgs</strong> class is part of a set of enhancements to the </span><a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx"><span style="font-size:10pt;color:#0033cc;font-family:&quot;text-decoration:none;">System.Net.Sockets&#8230;: .Socket</span></a><span style="font-size:10pt;color:black;font-family:&quot;"> class that provides an alternative asynchronous pattern that can be used by specialized high-performance socket applications. This class was specifically designed for network server applications that require high performance. An application can use the enhanced asynchronous pattern exclusively or only in targeted hot areas (for example, when receiving large amounts of data).</span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0 0 6.8pt;"><span style="font-size:10pt;color:black;font-family:&quot;">The main feature of these enhancements is the avoidance of the repeated allocation and synchronization of objects during high-volume asynchronous socket I/O. The Begin/End design pattern currently implemented by the </span><a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx"><span style="font-size:10pt;color:#0033cc;font-family:&quot;text-decoration:none;">System.Net.Sockets&#8230;: .Socket</span></a><span style="font-size:10pt;color:black;font-family:&quot;"> class requires a </span><a href="http://msdn.microsoft.com/en-us/library/system.iasyncresult.aspx"><span style="font-size:10pt;color:#0033cc;font-family:&quot;text-decoration:none;">System&#8230;: .IAsyncResult</span></a><span style="font-size:10pt;color:black;font-family:&quot;"> object be allocated for each asynchronous socket operation. </span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0 0 6.8pt;"><span style="font-size:10pt;color:black;font-family:&quot;">In the new </span><a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx"><span style="font-size:10pt;color:#0033cc;font-family:&quot;text-decoration:none;">System.Net.Sockets&#8230;: .Socket</span></a><span style="font-size:10pt;color:black;font-family:&quot;"> class enhancements, asynchronous socket operations are described by reusable <strong>SocketAsyncEventArgs</strong> objects allocated and maintained by the application. High-performance socket applications know best the amount of overlapped socket operations that must be sustained. The application can create as many of the <strong>SocketAsyncEventArgs</strong> objects that it needs. For example, if a server application needs to have 15 socket accept operations outstanding at all times to support incoming client connection rates, it can allocate 15 reusable <strong>SocketAsyncEventArgs</strong> objects for that purpose. </span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0 0 6.8pt;"><span style="font-size:10pt;color:black;font-family:&quot;">The pattern for performing an asynchronous socket operation with this class consists of the following steps: </span></p>
<ol style="margin-top:0;" type="1">
<li class="MsoNormal"><span style="font-size:10pt;font-family:&quot;">Allocate a new <strong>SocketAsyncEventArgs</strong> context object, or get a free one from an application pool. </span></li>
<li class="MsoNormal"><span style="font-size:10pt;font-family:&quot;">Set properties on the context object to the operation about to be performed (the completion callback method, the data buffer, the offset into the buffer, and the maximum amount of data to transfer, for example). </span></li>
<li class="MsoNormal"><span style="font-size:10pt;font-family:&quot;">Call the appropriate socket method (xxxAsync) to initiate the asynchronous operation. </span></li>
<li class="MsoNormal"><span style="font-size:10pt;font-family:&quot;">If the asynchronous socket method (xxxAsync) returns true, in the callback, query the context properties for completion status. </span></li>
<li class="MsoNormal"><span style="font-size:10pt;font-family:&quot;">If the asynchronous socket method (xxxAsync) returns false, the operation completed synchronously. The context properties may be queried for the operation result. </span></li>
<li class="MsoNormal"><span style="font-size:10pt;font-family:&quot;">Reuse the context for another operation, put it back in the pool, or discard it. </span></li>
</ol>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0 0 6.8pt;"><span style="font-size:10pt;color:black;font-family:&quot;">The lifetime of the new asynchronous socket operation context object is determined by references by the application code and asynchronous I/O references. It is not necessary for the application to retain a reference to an asynchronous socket operation context object after it is submitted as a parameter to one of the asynchronous socket operation methods. It will remain referenced until the completion callback returns. However it is advantageous for the application to retain the reference to the context so that it can be reused for a future asynchronous socket operation. </span></p>
<p> </p>
<div></div>
<p><span style="font-size:10pt;color:black;font-family:&quot;"></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:8pt;color:black;font-family:&quot;"> </span><strong><span style="font-size:12pt;color:black;font-family:&quot;">Examples</span></strong><strong><span style="font-size:8pt;color:black;font-family:&quot;"> </span></strong></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0 0 6.8pt;"><span style="font-size:10pt;color:black;font-family:&quot;">The following code example implements the connection logic for the socket server that uses the <strong>SocketAsyncEventArgs</strong> class. After accepting a connection, all data read from the client is sent back to the client. The read and echo back to the client pattern is continued until the client disconnects. The BufferManager class that is used by this example is displayed in the code example for the </span><a href="http://msdn.microsoft.com/en-us/library/bb517542.aspx"><span style="font-size:10pt;color:black;font-family:&quot;text-decoration:none;">SetBuffer (array&lt;Byte&gt; [] () [], Int32, Int32)</span></a><span style="font-size:10pt;color:black;font-family:&quot;"> method. The SocketAsyncEventArgsPool class that is used in this example is displayed in the code example for the </span><a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.socketasynceventargs.socketasynceventargs.aspx"><span style="font-size:10pt;color:black;font-family:&quot;text-decoration:none;">SocketAsyncEventArgs</span></a><span style="font-size:10pt;color:black;font-family:&quot;"> constructor. </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:8pt;color:black;font-family:&quot;">C#</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><a title="Copy Code" href="CopyCode('ctl00_rs1_mainContentContainer_ctl46CSharp');"><span style="font-size:8pt;color:#0033cc;font-family:&quot;text-decoration:none;">Copy Code</span></a></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:green;font-family:&quot;">// Implements the connection logic for the socket server.<span>  </span></span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:green;font-family:&quot;">// after accepting a connection, all data read from the client </span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:green;font-family:&quot;">// is sent back to the client. The read and echo back to the client pattern </span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:green;font-family:&quot;">// is continued until the client disconnects.</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:blue;font-family:&quot;">class</span><span style="font-size:10pt;color:black;font-family:&quot;"> Server</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;">{</span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">private</span><span style="font-size:10pt;color:black;font-family:&quot;"> </span><span style="font-size:10pt;color:blue;font-family:&quot;">int</span><span style="font-size:10pt;color:black;font-family:&quot;"> m_numConnections;<span>   </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// the maximum number of connections the sample is designed to handle simultaneously</span><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">private</span><span style="font-size:10pt;color:black;font-family:&quot;"> </span><span style="font-size:10pt;color:blue;font-family:&quot;">int</span><span style="font-size:10pt;color:black;font-family:&quot;"> m_receiveBufferSize</span><span style="font-size:10pt;color:green;font-family:&quot;">; // buffer size to use for each socket I/O operation </span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>BufferManager m_bufferManager</span><span style="font-size:10pt;color:green;font-family:&quot;">; <span> </span><span> </span>// represents a large reusable set of buffers for all socket operations</span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">const</span><span style="font-size:10pt;color:black;font-family:&quot;"> </span><span style="font-size:10pt;color:blue;font-family:&quot;">int</span><span style="font-size:10pt;color:black;font-family:&quot;"> opsToPreAlloc = 2;<span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// read, write (don&#8217;t alloc buffer space for accepts)</span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>Socket listenSocket;<span>            </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// the socket used to listen for incoming connection requests</span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// pool of reusable SocketAsyncEventArgs objects for write, read and accept socket operations</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>SocketAsyncEventArgsPool m_readWritePool;</span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">int</span><span style="font-size:10pt;color:black;font-family:&quot;"> m_totalBytesRead;<span>           </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// counter of the total # bytes received by the server</span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">int</span><span style="font-size:10pt;color:black;font-family:&quot;"> m_numConnectedSockets;<span>      </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// the total number of clients connected to the server </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>Semaphore m_maxNumberAcceptedClients;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// Create an uninitialized server instance.<span>  </span></span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// To start the server listening for connection requests</span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// call the Init method followed by Start method </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// &lt;param name=&#8221;numConnections&#8221;&gt;the maximum number of connections the sample is designed to handle simultaneously&lt;/param&gt;</span></p>
<p class="MsoNormal" style="vertical-align:top;text-align:justify;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// &lt;param name=&#8221;receiveBufferSize&#8221;&gt;buffer size to use for each socket I/O operation&lt;/param&gt;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">public</span><span style="font-size:10pt;color:black;font-family:&quot;"> Server(</span><span style="font-size:10pt;color:blue;font-family:&quot;">int</span><span style="font-size:10pt;color:black;font-family:&quot;"> numConnections, </span><span style="font-size:10pt;color:blue;font-family:&quot;">int</span><span style="font-size:10pt;color:black;font-family:&quot;"> receiveBufferSize)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>m_totalBytesRead = 0;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>m_numConnectedSockets = 0;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>m_numConnections = numConnections;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>m_receiveBufferSize = receiveBufferSize;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// allocate buffers such that the maximum number of sockets can have one outstanding read and </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">//write posted to the socket simultaneously<span>  </span></span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>m_bufferManager = </span><span style="font-size:10pt;color:blue;font-family:&quot;">new</span><span style="font-size:10pt;color:black;font-family:&quot;"> BufferManager (receiveBufferSize * numConnections * opsToPreAlloc,</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>receiveBufferSize);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>m_readWritePool = </span><span style="font-size:10pt;color:blue;font-family:&quot;">new</span><span style="font-size:10pt;color:black;font-family:&quot;"> SocketAsyncEventArgsPool (numConnections);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>m_maxNumberAcceptedClients = </span><span style="font-size:10pt;color:blue;font-family:&quot;">new</span><span style="font-size:10pt;color:black;font-family:&quot;"> Semaphore (numConnections, numConnections); </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// Initializes the server by preallocating reusable buffers and </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// context objects.<span>  </span>These objects do not need to be preallocated </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// or reused, but it is done this way to illustrate how the API can </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// easily be used to create reusable objects to increase server performance.</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">//</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">public</span><span style="font-size:10pt;color:black;font-family:&quot;"> </span><span style="font-size:10pt;color:blue;font-family:&quot;">void</span><span style="font-size:10pt;color:black;font-family:&quot;"> Init()</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// Allocates one large byte buffer which all I/O operations use a piece of.<span>  </span>This gaurds </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// against memory fragmentation</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>m_bufferManager.InitBuffer();</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// preallocate pool of SocketAsyncEventArgs objects</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>  </span><span>      </span>SocketAsyncEventArgs readWriteEventArg;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">for</span><span style="font-size:10pt;color:black;font-family:&quot;"> (</span><span style="font-size:10pt;color:blue;font-family:&quot;">int</span><span style="font-size:10pt;color:black;font-family:&quot;"> i = 0; i &lt; m_numConnections; i++)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:green;font-family:&quot;">//Pre-allocate a set of reusable SocketAsyncEventArgs</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>readWriteEventArg = </span><span style="font-size:10pt;color:blue;font-family:&quot;">new</span><span style="font-size:10pt;color:black;font-family:&quot;"> SocketAsyncEventArgs();</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>readWriteEventArg.Completed += </span><span style="font-size:10pt;color:blue;font-family:&quot;">new</span><span style="font-size:10pt;color:black;font-family:&quot;"> EventHandler&lt;SocketAsyncEventArgs&gt;(IO_Completed);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>readWriteEventArg.UserToken = </span><span style="font-size:10pt;color:blue;font-family:&quot;">new</span><span style="font-size:10pt;color:black;font-family:&quot;"> AsyncUserToken();</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// assign a byte buffer from the buffer pool to the SocketAsyncEventArg object</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>   </span><span>         </span>m_bufferManager.SetBuffer(readWriteEventArg);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// add SocketAsyncEventArg to the pool</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>m_readWritePool.Push(readWriteEventArg);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// Starts the server such that it is listening for </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// incoming connection requests.<span>    </span></span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">//</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// &lt;param name=&#8221;localEndPoint&#8221;&gt;The endpoint which the server will listening </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// for connection requests on&lt;/param&gt;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">public</span><span style="font-size:10pt;color:black;font-family:&quot;"> </span><span style="font-size:10pt;color:blue;font-family:&quot;">void</span><span style="font-size:10pt;color:black;font-family:&quot;"> Start(IPEndPoint localEndPoint)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// create the socket which listens for incoming connections</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>listenSocket = </span><span style="font-size:10pt;color:blue;font-family:&quot;">new</span><span style="font-size:10pt;color:black;font-family:&quot;"> Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>listenSocket.Bind(localEndPoint);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// start the server with a listen backlog of 100 connections</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>listenSocket.Listen(100);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// post accepts on the listening socket</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>StartAccept(</span><span style="font-size:10pt;color:blue;font-family:&quot;">null</span><span style="font-size:10pt;color:black;font-family:&quot;">);<span>            </span></span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">//Console.WriteLine(&#8220;{0} connected sockets with one outstanding receive posted to each&#8230;.press any key&#8221;, m_outstandingReadCount);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>Console.WriteLine(</span><span style="font-size:10pt;color:maroon;font-family:&quot;">&#8220;Press any key to terminate the server process&#8230;.&#8221;</span><span style="font-size:10pt;color:black;font-family:&quot;">);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>Console.ReadKey();</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// Begins an operation to accept a connection request from the client </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">//</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// &lt;param name=&#8221;acceptEventArg&#8221;&gt;The context object to use when issuing </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// the accept operation on the server&#8217;s listening socket&lt;/param&gt;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">public</span><span style="font-size:10pt;color:black;font-family:&quot;"> </span><span style="font-size:10pt;color:blue;font-family:&quot;">void</span><span style="font-size:10pt;color:black;font-family:&quot;"> StartAccept(SocketAsyncEventArgs acceptEventArg)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">if</span><span style="font-size:10pt;color:black;font-family:&quot;"> (acceptEventArg == </span><span style="font-size:10pt;color:blue;font-family:&quot;">null</span><span style="font-size:10pt;color:black;font-family:&quot;">)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>acceptEventArg = </span><span style="font-size:10pt;color:blue;font-family:&quot;">new</span><span style="font-size:10pt;color:black;font-family:&quot;"> SocketAsyncEventArgs();</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>acceptEventArg.Completed += </span><span style="font-size:10pt;color:blue;font-family:&quot;">new</span><span style="font-size:10pt;color:black;font-family:&quot;"> EventHandler&lt;SocketAsyncEventArgs&gt;(AcceptEventArg_Completed);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">else</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// socket must be cleared since the context object is being reused</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>acceptEventArg.AcceptSocket = </span><span style="font-size:10pt;color:blue;font-family:&quot;">null</span><span style="font-size:10pt;color:black;font-family:&quot;">;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>m_maxNumberAcceptedClients.WaitOne();</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">bool</span><span style="font-size:10pt;color:black;font-family:&quot;"> willRaiseEvent = listenSocket.AcceptAsync(acceptEventArg);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">if</span><span style="font-size:10pt;color:black;font-family:&quot;"> (!willRaiseEvent)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>ProcessAccept(acceptEventArg);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// This method is the callback method associated with Socket.AcceptAsync </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// operations and is invoked when an accept operation is complete</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">//</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">void</span><span style="font-size:10pt;color:black;font-family:&quot;"> AcceptEventArg_Completed(object sender, SocketAsyncEventArgs e)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>  </span><span>      </span>ProcessAccept(e);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">private</span><span style="font-size:10pt;color:black;font-family:&quot;"> </span><span style="font-size:10pt;color:blue;font-family:&quot;">void</span><span style="font-size:10pt;color:black;font-family:&quot;"> ProcessAccept(SocketAsyncEventArgs e)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>Interlocked.Increment(ref m_numConnectedSockets);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>Console.WriteLine(</span><span style="font-size:10pt;color:maroon;font-family:&quot;">&#8220;Client connection accepted. There are {0} clients connected to the server&#8221;</span><span style="font-size:10pt;color:black;font-family:&quot;">,</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>m_numConnectedSockets);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// Get the socket for the accepted client connection and put it into the </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">//ReadEventArg object user token</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>SocketAsyncEventArgs readEventArgs = m_readWritePool.Pop();</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>((AsyncUserToken)readEventArgs.UserToken).Socket = e.AcceptSocket;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// As soon as the client is connected, post a receive to the connection</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">bool</span><span style="font-size:10pt;color:black;font-family:&quot;"> willRaiseEvent = e.AcceptSocket.ReceiveAsync(readEventArgs);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">if</span><span style="font-size:10pt;color:black;font-family:&quot;">(!willRaiseEvent){</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>ProcessReceive(readEventArgs);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// Accept the next connection request</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>StartAccept(e);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// This method is called whenever a receive or send operation is completed on a socket </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">//</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// &lt;param name=&#8221;e&#8221;&gt;SocketAsyncEventArg associated with the completed receive operation&lt;/param&gt;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">void</span><span style="font-size:10pt;color:black;font-family:&quot;"> IO_Completed(object sender, SocketAsyncEventArgs e)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// determine which type of operation just completed and call the associated handler</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">switch</span><span style="font-size:10pt;color:black;font-family:&quot;"> (e.LastOperation)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">case</span><span style="font-size:10pt;color:black;font-family:&quot;"> SocketAsyncOperation.Receive:</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>                </span>ProcessReceive(e);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>                </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">break</span><span style="font-size:10pt;color:black;font-family:&quot;">;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">case</span><span style="font-size:10pt;color:black;font-family:&quot;"> SocketAsyncOperation.Send:</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>                </span>ProcessSend(e);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>                </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">break</span><span style="font-size:10pt;color:black;font-family:&quot;">;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">default</span><span style="font-size:10pt;color:black;font-family:&quot;">:</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>                </span>throw </span><span style="font-size:10pt;color:blue;font-family:&quot;">new</span><span style="font-size:10pt;color:black;font-family:&quot;"> ArgumentException(</span><span style="font-size:10pt;color:maroon;font-family:&quot;">&#8220;The last operation completed on the socket was not a receive or send&#8221;</span><span style="font-size:10pt;color:black;font-family:&quot;">);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>}<span>       </span></span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// This method is invoked when an asynchronous receive operation completes. </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// If the remote host closed the connection, then the socket is closed.<span>  </span></span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// If data was received then the data is echoed back to the client.</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">//</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">private</span><span style="font-size:10pt;color:black;font-family:&quot;"> </span><span style="font-size:10pt;color:blue;font-family:&quot;">void</span><span style="font-size:10pt;color:black;font-family:&quot;"> ProcessReceive(SocketAsyncEventArgs e)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// check if the remote host closed the connection</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>AsyncUserToken token = (AsyncUserToken)e.UserToken;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">if</span><span style="font-size:10pt;color:black;font-family:&quot;"> (e.BytesTransferred &gt; 0 &amp;&amp; e.SocketError == SocketError.Success)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:green;font-family:&quot;">//increment the count of the total bytes receive by the server</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>Interlocked.Add(ref m_totalBytesRead, e.BytesTransferred);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>Console.WriteLine(</span><span style="font-size:10pt;color:maroon;font-family:&quot;">&#8220;The server has read a total of {0} bytes&#8221;</span><span style="font-size:10pt;color:black;font-family:&quot;">, m_totalBytesRead);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:green;font-family:&quot;">//echo the data received back to the client</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>e.SetBuffer(e.Offset, e.BytesTransferred);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">bool</span><span style="font-size:10pt;color:black;font-family:&quot;"> willRaiseEvent = token.Socket.SendAsync(e);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">if</span><span style="font-size:10pt;color:black;font-family:&quot;"> (!willRaiseEvent)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>                </span>ProcessSend(e);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">else</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>CloseClientSocket(e);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// This method is invoked when an asynchronous send operation completes.<span>  </span></span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// The method issues another receive on the socket to read any additional </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// data sent from the client</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">//</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// &lt;param name=&#8221;e&#8221;&gt;&lt;/param&gt;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">private</span><span style="font-size:10pt;color:black;font-family:&quot;"> </span><span style="font-size:10pt;color:blue;font-family:&quot;">void</span><span style="font-size:10pt;color:black;font-family:&quot;"> ProcessSend(SocketAsyncEventArgs e)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">if</span><span style="font-size:10pt;color:black;font-family:&quot;"> (e.SocketError == SocketError.Success)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// done echoing data back to the client</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>AsyncUserToken token = (AsyncUserToken)e.UserToken;</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// read the next block of data send from the client</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">bool</span><span style="font-size:10pt;color:black;font-family:&quot;"> willRaiseEvent = token.Socket.ReceiveAsync(e);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">if</span><span style="font-size:10pt;color:black;font-family:&quot;"> (!willRaiseEvent)</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>                </span>ProcessReceive(e);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">else</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>{</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>            </span>CloseClientSocket(e);</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"><span>    </span>}</span></p>
<p></span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:blue;font-family:&quot;">private</span><span style="font-size:10pt;color:black;font-family:&quot;"> </span><span style="font-size:10pt;color:blue;font-family:&quot;">void</span><span style="font-size:10pt;color:black;font-family:&quot;"> CloseClientSocket(SocketAsyncEventArgs e)<br />
{<br />
<span>        </span>AsyncUserToken token = e.UserToken as AsyncUserToken;<br />
</span><span style="font-size:10pt;color:green;font-family:&quot;"><br />
<span>        </span>// close the socket associated with the client</span><span style="font-size:10pt;color:black;font-family:&quot;"><br />
<span>        </span>try<br />
<span>        </span>{<br />
<span>            </span>token.Socket.Shutdown(SocketShutdown.Send);<br />
<span>        </span>}<br />
<span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// throws if client process has already closed</span><span style="font-size:10pt;color:black;font-family:&quot;"><br />
<span>        </span>catch (Exception) { }<br />
<span>        </span>token.Socket.Close();</p>
<p><span>        </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// Before releasing the Semaphore..</span><span style="font-size:10pt;color:black;font-family:&quot;"><br />
</span><span style="font-size:10pt;color:green;font-family:&quot;"><span>          </span>// Free the SocketAsyncEventArg so they can be reused by another client<br />
</span><span style="font-size:10pt;color:black;font-family:&quot;"><span>          </span>m_readWritePool.Push(e);</p>
<p><span>          </span></span><span style="font-size:10pt;color:green;font-family:&quot;">// decrement the counter keeping track of the total number of clients connected to the server<br />
</span><span style="font-size:10pt;color:black;font-family:&quot;"><span>          </span>Interlocked.Decrement(ref m_numConnectedSockets);</p>
<p></span><span style="font-size:10pt;color:green;font-family:&quot;"><span>        </span>// Finally, release the m_maxNumberAcceptedClients semaphore<br />
</span><span style="font-size:10pt;color:black;font-family:&quot;"><span>        </span>m_maxNumberAcceptedClients.Release();<br />
<span>        </span>Console.WriteLine(&#8220;</span><span style="font-size:10pt;color:#e36c0a;font-family:&quot;">A client has been disconnected from the server. There are {0} clients connected to the server</span><span style="font-size:10pt;color:black;font-family:&quot;">&#8220;, m_numConnectedSockets);<br />
<span>    </span>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:black;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"><span style="font-size:10pt;color:blue;font-family:&quot;">class</span><span style="font-size:10pt;color:black;font-family:&quot;"> AsyncUserToken<br />
{<br />
<span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">private</span><span style="font-size:10pt;color:black;font-family:&quot;"> System.Net.Socket _socket;</p>
<p><span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">public</span><span style="font-size:10pt;color:black;font-family:&quot;"> AsyncUserToken()<br />
<span>    </span>{<br />
<span>    </span>}<br />
</span><span style="font-size:10pt;color:#00b050;font-family:&quot;"><br />
<span>    </span>// public property to get and set the internal private _socket member</span><span style="font-size:10pt;color:black;font-family:&quot;"><br />
<span>    </span></span><span style="font-size:10pt;color:blue;font-family:&quot;">public</span><span style="font-size:10pt;color:black;font-family:&quot;"> System.Net.Socket Socket<br />
<span>    </span>{<br />
<span>       </span>get { return _socket; }<br />
<span>       </span>set { _socket = value; }<br />
<span>    </span>}</p>
<p>}</span></p>
<p class="MsoNormal" style="vertical-align:top;margin:0;"> </p>
<pre><span style="color:black;"><span style="font-size:x-small;font-family:Courier New;"> </span></span>
<span style="color:black;"><span style="font-size:x-small;font-family:Courier New;"> </span></span><span style="font-size:small;font-family:Calibri;"> </span></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ravidotnet.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ravidotnet.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ravidotnet.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ravidotnet.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ravidotnet.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ravidotnet.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ravidotnet.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ravidotnet.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ravidotnet.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ravidotnet.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ravidotnet.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ravidotnet.wordpress.com/37/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ravidotnet.wordpress.com/37/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ravidotnet.wordpress.com/37/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=37&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ravidotnet.wordpress.com/2009/03/10/socket-performance-enhancements-in-version-35/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64d4bdd3e0210031e9fc0ae3c28edfc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcamail2002</media:title>
		</media:content>
	</item>
		<item>
		<title>Changing the password format</title>
		<link>http://ravidotnet.wordpress.com/2008/12/16/changing-the-password-format/</link>
		<comments>http://ravidotnet.wordpress.com/2008/12/16/changing-the-password-format/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 10:23:54 +0000</pubDate>
		<dc:creator>mcamail2002</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://ravidotnet.wordpress.com/?p=34</guid>
		<description><![CDATA[As web sites mature, website administrators sometimes regret their original (sometimes unintended) choice in passwordFormat when using the AspNetSqlMembershipProvider. That is, membership passwords may be clear text when a hashed format is desired or vice versa. Microsoft’s decision to implement hashing in the default AspNetSqlMembershipProvider was wise and conservative but for many web sites with [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=34&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="vertical-align:top;line-height:140%;margin:0;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">As web sites mature, website administrators sometimes regret their original (sometimes unintended) choice in passwordFormat when using the AspNetSqlMembershipProvider. That is, membership passwords may be clear text when a hashed format is desired or vice versa. Microsoft’s decision to implement hashing in the default AspNetSqlMembershipProvider was wise and conservative but for many web sites with minimal security requirements, the password system can become cumbersome.  By directly calling a couple of the AspNet stored procedures, it is possible to change the password format:</span></p>
<p class="MsoNormal" style="vertical-align:top;line-height:140%;margin:0 51.5pt 7.5pt 87.5pt;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">Note: If the passwordFormat is initially “Clear” or “Encrypted”, use the membership.provider.GetPassword method to cache the original password before calling the stored procedures.</span></p>
<p class="MsoNormal" style="vertical-align:top;text-indent:-.25in;line-height:140%;margin:0 0 0 51.5pt;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">1.      Use the stored procedure aspnet Membership GetPasswordWithFormat to retrieve the current passwordSalt.</span></p>
<p class="MsoNormal" style="vertical-align:top;text-indent:-.25in;line-height:140%;margin:0 0 0 51.5pt;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">2.      Use the stored procedure aspnet Membership ResetPassword to set the passwordFormat to its intended (integer) value. The stored procedure requires readily available parameter values including passwordSalt (retrieved earlier), password (empty string) and passwordAnswer (Null).</span></p>
<p class="MsoNormal" style="vertical-align:top;line-height:140%;margin:0;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">At this point, the membership record has been placed into an initialized (unusable) state and the PasswordAnswer has been lost. If the original password was hashed, then it too will be unrecoverable.  The provider methods listed below and described in previous sections allow for resetting the credentials and, as they are used, the password and password answer will be stored in the new password format (clear, encrypted, hashed.)</span></p>
<p class="MsoNormal" style="vertical-align:top;text-indent:-.25in;line-height:140%;margin:0 0 0 51.5pt;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">1.      Call the ResetPassword method to generate and retrieve a new random Password. Remember that the second parameter (answer) is not required if “requiresQuestionAndAnswer” is set to false in web.config.</span></p>
<p class="MsoNormal" style="vertical-align:top;text-indent:-.25in;line-height:140%;margin:0 0 0 51.5pt;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">2.      Call the ChangePassword method, using the now-current password retrieved in the previous step, to set the password to a desired value.  If the original password was saved at the start of the procedure, it may be restored at this point. </span></p>
<p class="MsoNormal" style="vertical-align:top;line-height:140%;margin:0;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">For originally un-hashed passwords, the preceding steps allow for a change of passwordFormat with complete restoration of the original password. </span></p>
<p class="MsoNormal" style="vertical-align:top;line-height:140%;margin:0;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">The Password Answer could have easily been retrieved from the database at the outset if it was stored in clear text. In the case of an encrypted Password Answer, a more complicated approach which involves the provider’s protected DecryptPassword method could have been used to cache the original Password Answer.  If the original Password Answer were available, it could be restored with a call to the ChangePasswordQuestionAndAnswer provider method.</span></p>
<p class="MsoNormal" style="vertical-align:top;line-height:140%;margin:0;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">So, what can be done if the Password and/or Password Answer had to be sacrificed in favor of a new passwordFormat?  One solution might be to reset everyone’s credentials then send them by Email. Another solution might be to place a notice onto the web site that informs users and provides further instructions. Either way, the web site should leverage the self-service membership controls which allow the member to reset his/her own credentials.  The following outlines a series of steps that can be taken:</span></p>
<p class="MsoNormal" style="vertical-align:top;text-indent:-.25in;line-height:140%;margin:0 0 0 51.5pt;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">1.      A new arbitrary password can be assigned using either the ResetPassword or ChangePassword provider method. Similarly, a new arbitrary Password Question and Password Answer can be assigned using the ChangePasswordQuestionAndAnswer provider method.</span></p>
<p class="MsoNormal" style="vertical-align:top;text-indent:-.25in;line-height:140%;margin:0 0 0 51.5pt;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">2.      Since the user will not know his/her new credentials, ensure the Login Control includes the necessary properties (PasswordRecoveryText and PasswordRecoveryURL) to link the user to a page that includes a PasswordRecovery Control.</span></p>
<p class="MsoNormal" style="vertical-align:top;text-indent:-.25in;line-height:140%;margin:0 0 0 51.5pt;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">3.      Recall that the PasswordRecovery Control is driven by the provider settings in web.config. In particular, ensure that “requiresQuestionAndAnswer” is set to false so the PasswordRecovery Control does not prompt the user for a Password Answer. Also, ensure that the SMTP setting is specified in web.config so that the Email will be sent. If the membership record uses a hashed password format then a new (random) password will be sent, otherwise the password you assigned in the previous step will be sent.</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ravidotnet.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ravidotnet.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ravidotnet.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ravidotnet.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ravidotnet.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ravidotnet.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ravidotnet.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ravidotnet.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ravidotnet.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ravidotnet.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ravidotnet.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ravidotnet.wordpress.com/34/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ravidotnet.wordpress.com/34/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ravidotnet.wordpress.com/34/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=34&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ravidotnet.wordpress.com/2008/12/16/changing-the-password-format/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64d4bdd3e0210031e9fc0ae3c28edfc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcamail2002</media:title>
		</media:content>
	</item>
		<item>
		<title>Changing a member’s Password Question and Password Answer</title>
		<link>http://ravidotnet.wordpress.com/2008/12/16/changing-a-member%e2%80%99s-password-question-and-password-answer/</link>
		<comments>http://ravidotnet.wordpress.com/2008/12/16/changing-a-member%e2%80%99s-password-question-and-password-answer/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 10:13:05 +0000</pubDate>
		<dc:creator>mcamail2002</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://ravidotnet.wordpress.com/?p=26</guid>
		<description><![CDATA[In some situations, the Customer Service department may wish to modify a member’s Password Question and Password Answer. This is easily accomplished if passwords are encrypted or maintained in clear text. For hashed passwords, however, a password-reset is also required since the provider method, ChangePasswordQuestionAndAnswer, requires the member’s password which is not retrievable. By setting [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=26&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="vertical-align:top;line-height:140%;margin:0;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">In some situations, the Customer Service department may wish to modify a member’s Password Question and Password Answer. This is easily accomplished if passwords are encrypted or maintained in clear text. For hashed passwords, however, a password-reset is also required since the provider method, ChangePasswordQuestionAndAnswer, requires the member’s password which is not retrievable. By setting “requiresQuestionAndAnswer” to <em>false</em>, “enablePasswordRetrieval” to <em>true</em> and “enablePasswordReset” to <em>true</em> in web.config, the member’s Password Question and Password Answer may be reset:</span></p>
<p class="MsoNormal" style="vertical-align:top;text-indent:-1in;line-height:140%;margin:0 0 0 123.5pt;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN"><strong>Clear text</strong> :  Call the GetPassword method with the username and without the need for a password answer to retrieve the password. Now, armed with the password, call ChangePasswordQuestionAndAnswer to set the Password Question and Password Answer to a desired value.</span></p>
<p class="MsoNormal" style="vertical-align:top;text-indent:-1in;line-height:140%;margin:0 0 0 123.5pt;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN"><strong>Encrypted </strong>   Call the GetPassword method with the username and without the need for a password answer to retrieve the password. Now, armed with the password, call ChangePasswordQuestionAndAnswer to set the Password Question and Password Answer to a desired value.</span></p>
<p><span style="font-size:10pt;font-family:Verdana;" lang="EN">              <strong>Hashed:</strong>    Call the ResetPassword method with the username and without the need for a password answer to reset the password to a new random value. Using the newly generated password, call ChangePasswordQuestionAndAnswer to set the Password Question and Password Answer to a desired value. Optionally call ChangePassword to set the password to a more user-friendly value.</span></p>
<address><span style="font-size:x-small;color:#0000ff;">string</span><span style="font-size:x-small;"> password = </span><span style="font-size:x-small;color:#2b91af;">Membership</span><span style="font-size:x-small;">.Providers[providerName].GetPassword(currentUser,</span><span style="font-size:x-small;color:#0000ff;">string</span><span style="font-size:x-small;">.Empty);<span style="font-size:x-small;"> </span></span></address>
<address><span style="font-size:x-small;color:#0000ff;">if</span><span style="font-size:x-small;"> (!</span><span style="font-size:x-small;color:#0000ff;">string</span><span style="font-size:x-small;">.IsNullOrEmpty(password))</p>
<address>{</address>
<address><span style="font-size:x-small;color:#0000ff;">if</span><span style="font-size:x-small;"> (</span><span style="font-size:x-small;color:#2b91af;">Membership</span><span style="font-size:x-small;">.Provider.ChangePasswordQuestionAndAnswer(currentUser, password, txtSecurityQuestion.Text.Trim(), txtSecurityAnswer.Text.Trim()))<span style="font-size:x-small;"><br />
FailureText.Text = </span></span><span style="font-size:x-small;color:#a31515;">&#8220;Password Question and Answer changed.&#8221;</span><span style="font-size:x-small;">;<span style="font-size:x-small;"><br />
</span></span><span style="font-size:x-small;color:#0000ff;">else</span></address>
<address></address>
<address><span style="font-size:x-small;"><span style="font-size:x-small;">FailureText.Text = <span style="font-size:x-small;color:#a31515;">&#8220;Change failed. Please re-enter your values and try again.&#8221;</span><span style="font-size:x-small;">;</span></span></span></address>
<address><span style="font-size:x-small;"><span style="font-size:x-small;"><span style="font-size:x-small;">}</span> </span></span></address>
<address></address>
<p> </p>
<p></span></address>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ravidotnet.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ravidotnet.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ravidotnet.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ravidotnet.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ravidotnet.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ravidotnet.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ravidotnet.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ravidotnet.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ravidotnet.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ravidotnet.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ravidotnet.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ravidotnet.wordpress.com/26/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ravidotnet.wordpress.com/26/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ravidotnet.wordpress.com/26/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=26&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ravidotnet.wordpress.com/2008/12/16/changing-a-member%e2%80%99s-password-question-and-password-answer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64d4bdd3e0210031e9fc0ae3c28edfc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcamail2002</media:title>
		</media:content>
	</item>
		<item>
		<title>Retrieving a member’s password</title>
		<link>http://ravidotnet.wordpress.com/2008/12/16/retrieving-a-member%e2%80%99s-password/</link>
		<comments>http://ravidotnet.wordpress.com/2008/12/16/retrieving-a-member%e2%80%99s-password/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 09:51:58 +0000</pubDate>
		<dc:creator>mcamail2002</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://ravidotnet.wordpress.com/?p=13</guid>
		<description><![CDATA[The GetPassword method may be used to retrieve a member’s password and, at first glance, appears to require the password answer. By setting “requiresQuestionAndAnswer” to false in web.config, the GetPassword method can be called with an empty password answer and therefore can be effectively used to administratively retrieve a member’s password. Note that “enablePasswordRetrieval” must [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=13&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="vertical-align:top;line-height:140%;margin:0;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN">The GetPassword method may be used to retrieve a member’s password and, at first glance, appears to require the password answer. By setting “requiresQuestionAndAnswer” to <em>false</em> in web.config, the GetPassword method can be called with an empty password answer and therefore can be effectively used to administratively retrieve a member’s password. Note that “enablePasswordRetrieval” must be set to <em>true</em> in web.config to enable the GetPassword method:</span></p>
<p class="MsoNormal" style="vertical-align:top;line-height:140%;margin:0;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN"> If password is:</span></p>
<p class="MsoNormal" style="vertical-align:top;text-indent:-1in;line-height:140%;margin:0 0 0 123.5pt;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN"><strong>Clear text:</strong>   Simply call the GetPassword method with the username and without the need for a password answer to retrieve the password</span></p>
<p class="MsoNormal" style="vertical-align:top;text-indent:-1in;line-height:140%;margin:0 0 0 123.5pt;"><span style="font-size:10pt;line-height:140%;font-family:Verdana;" lang="EN"><strong>Encrypted:</strong>   Simply call the GetPassword method with the username and without the need for a password answer to retrieve the password</span></p>
<p><span style="font-size:10pt;font-family:Verdana;" lang="EN">             <strong>Hashed:</strong>       Not possible, however the password may be reset as described below.</span></p>
<p><span style="font-size:10pt;font-family:Verdana;" lang="EN"><span style="color:#0000ff;">string</span> password = <span style="color:#33cccc;">Membership</span>.Providers&#8221;MembershipProviderForAdmin&#8221;].GetPassword(currentUser,<span style="color:#0000ff;">string</span>.Empty);</span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ravidotnet.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ravidotnet.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ravidotnet.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ravidotnet.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ravidotnet.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ravidotnet.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ravidotnet.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ravidotnet.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ravidotnet.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ravidotnet.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ravidotnet.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ravidotnet.wordpress.com/13/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ravidotnet.wordpress.com/13/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ravidotnet.wordpress.com/13/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=13&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ravidotnet.wordpress.com/2008/12/16/retrieving-a-member%e2%80%99s-password/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64d4bdd3e0210031e9fc0ae3c28edfc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcamail2002</media:title>
		</media:content>
	</item>
		<item>
		<title>Resetting Password with ASP.NET 2.0 Membership</title>
		<link>http://ravidotnet.wordpress.com/2008/12/16/resetting-password-with-aspnet-20-membership/</link>
		<comments>http://ravidotnet.wordpress.com/2008/12/16/resetting-password-with-aspnet-20-membership/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 09:47:13 +0000</pubDate>
		<dc:creator>mcamail2002</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://ravidotnet.wordpress.com/?p=10</guid>
		<description><![CDATA[If you ever have wanted to be able to programmatically change (reset) a users password while at the same time continuing to be able to use the question and answer feature, this post is for you.  The problem is that if you use code like this:   string username = "username"; string password = "pass@word"; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=10&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you ever have wanted to be able to programmatically change (reset) a users password while at the same time continuing to be able to use the question and answer feature, this post is for you.  The problem is that if you use code like this:</p>
<p> </p>
<pre class="csharpcode"> <span style="color:#0000ff;"><span class="kwrd">string</span> </span>username = <span class="str">"<span style="color:#993300;">username</span>"</span>;
 <span class="kwrd"><span style="color:#0000ff;">string</span></span> password = <span class="str">"<span style="color:#993300;">pass@word</span>"</span>;
 <span style="color:#33cccc;">MembershipUser </span>mUser = Membership.GetUser(username);
 mu.ChangePassword(mUser.ResetPassword(), password);</pre>
<p>You will find that if you have in your web.config <strong>requiresQuestionAnswer=&#8221;true&#8221;,</strong> you will get an error when you try and reset the password.  The elegant solution to this is to create an additional membeship tag in your web.config and reference it when you change passwords.  That is, add another provider like this:</p>
<p><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">&lt;!&#8211; </span></span><span style="font-size:x-small;color:#008000;">To Reset password of the user. </span><span style="font-size:x-small;color:#0000ff;">&#8211;&gt;</span></p>
<address class="csharpcode"><span class="kwrd"><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">&lt; </span></span><span style="font-size:x-small;color:#a31515;">membership</span><span style="font-size:x-small;color:#0000ff;"> </span><span style="font-size:x-small;color:#ff0000;">defaultProvider</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">SqlMembershipProvider</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">&gt;<span style="font-size:x-small;color:#0000ff;"> &lt; </span></span><span style="font-size:x-small;color:#a31515;">providers</span><span style="font-size:x-small;color:#0000ff;">&gt;<span style="font-size:x-small;color:#0000ff;"> &lt; </span></span><span style="font-size:x-small;color:#a31515;">clear</span><span style="font-size:x-small;color:#0000ff;">/&gt;</span> </span><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;"> &lt; </span></span><span style="font-size:x-small;color:#a31515;">add</span><span style="font-size:x-small;color:#0000ff;"> </span><span style="font-size:x-small;color:#ff0000;">name</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">SqlMembershipProviderAdmin</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;"> <span style="font-size:x-small;color:#0000ff;">  </span></span><span style="font-size:x-small;color:#ff0000;">type</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</span><span style="font-size:x-small;">&#8220;</span> <span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">  </span></span><span style="font-size:x-small;color:#ff0000;">applicationName</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">/</span><span style="font-size:x-small;">&#8220;</span> <span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">  </span></span><span style="font-size:x-small;color:#ff0000;">connectionStringName</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">ConnectionString</span><span style="font-size:x-small;">&#8220;</span> <span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">  </span></span><span style="font-size:x-small;color:#ff0000;">enablePasswordRetrieval</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">true</span><span style="font-size:x-small;">&#8220;</span> <span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">  </span></span><span style="font-size:x-small;color:#ff0000;">enablePasswordReset</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">true</span><span style="font-size:x-small;">&#8220;</span> <span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">  </span></span><span style="font-size:x-small;color:#ff0000;">requiresQuestionAndAnswer</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">false</span><span style="font-size:x-small;">&#8220;</span> <span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">  </span></span><span style="font-size:x-small;color:#ff0000;">requiresUniqueEmail</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">true</span><span style="font-size:x-small;">&#8220;</span> <span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">  </span></span><span style="font-size:x-small;color:#ff0000;">passwordFormat</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">Encrypted</span><span style="font-size:x-small;">&#8220;</span> <span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">  </span></span><span style="font-size:x-small;color:#ff0000;">maxInvalidPasswordAttempts</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">5</span><span style="font-size:x-small;">&#8220;</span> <span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">  </span></span><span style="font-size:x-small;color:#ff0000;">minRequiredPasswordLength</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">1</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;"> <span style="font-size:x-small;color:#0000ff;">  </span></span><span style="font-size:x-small;color:#ff0000;">minRequiredNonalphanumericCharacters</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">0</span><span style="font-size:x-small;">&#8220;</span> <span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">  </span></span><span style="font-size:x-small;color:#ff0000;">passwordAttemptWindow</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;</span><span style="font-size:x-small;color:#0000ff;">50</span><span style="font-size:x-small;">&#8220;</span> <span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">  </span></span><span style="font-size:x-small;color:#ff0000;">passwordStrengthRegularExpression</span><span style="font-size:x-small;color:#0000ff;">=</span><span style="font-size:x-small;">&#8220;&#8221;</span><span style="font-size:x-small;color:#0000ff;">/&gt;</span> <span class="kwrd"><span style="font-size:x-small;color:#0000ff;"><span style="font-size:x-small;color:#0000ff;">&lt;/ </span></span><span style="font-size:x-small;color:#a31515;">providers</span><span style="font-size:x-small;color:#0000ff;">&gt;<span style="font-size:x-small;color:#0000ff;"> &lt;/ </span></span><span style="font-size:x-small;color:#a31515;">membership</span><span style="font-size:x-small;color:#0000ff;">&gt;</span> </span> </address>
<p>Then, when you change your password, reference it as follows:</p>
<p><span style="color:#0000ff;"><span class="kwrd">string</span> </span>username = <span class="str">&#8220;<span style="color:#800000;">username</span>&#8220;</span>;<br />
<span class="kwrd"><span style="color:#0000ff;">string</span></span> password = <span class="str">&#8220;<span style="color:#800000;">pass@word</span>&#8220;</span>;<br />
<span style="color:#33cccc;">MembershipUser </span>mUser = <span style="color:#33cccc;">Membership</span>.Providers[<span class="str">"<span style="color:#800000;">SqlMembershipProviderAdmin</span>"</span>].GetUser(username);<br />
mu.ChangePassword(mUser.ResetPassword(), password);</p>
<p> Hope this helps.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ravidotnet.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ravidotnet.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ravidotnet.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ravidotnet.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ravidotnet.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ravidotnet.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ravidotnet.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ravidotnet.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ravidotnet.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ravidotnet.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ravidotnet.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ravidotnet.wordpress.com/10/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ravidotnet.wordpress.com/10/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ravidotnet.wordpress.com/10/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=10&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ravidotnet.wordpress.com/2008/12/16/resetting-password-with-aspnet-20-membership/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64d4bdd3e0210031e9fc0ae3c28edfc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcamail2002</media:title>
		</media:content>
	</item>
		<item>
		<title>Always show GridView header and footer if data soure is empty</title>
		<link>http://ravidotnet.wordpress.com/2008/12/16/always-show-gridview-header-and-footer-if-data-soure-is-empty/</link>
		<comments>http://ravidotnet.wordpress.com/2008/12/16/always-show-gridview-header-and-footer-if-data-soure-is-empty/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 09:30:00 +0000</pubDate>
		<dc:creator>mcamail2002</dc:creator>
				<category><![CDATA[ADO.NET]]></category>

		<guid isPermaLink="false">http://ravidotnet.wordpress.com/?p=5</guid>
		<description><![CDATA[&#60;EmptyDataTemplate&#62; &#60;asp:Table ID="tblEmptyHeadCount" runat="server" Width="100%"&#62; &#60;asp:TableRow ID="TableRow17" runat="server" HorizontalAlign="right" Width="100%"&#62; &#60;asp:TableCell ID="TableCell42" runat="server" Width="15%"&#62;&#60;/asp:TableCell&#62; &#60;asp:TableCell ID="TableCell43" runat="server" Width="40%" HorizontalAlign="center"&#62; &#60;asp:Label ID="lbl1" runat="server" Text="Position"&#62;&#60;/asp:Label&#62; &#60;/asp:TableCell&#62; &#60;asp:TableCell ID="TableCell48" runat="server" Width="15%" HorizontalAlign="center"&#62; &#60;asp:Label ID="Label6" runat="server" Text="FTE Salary"&#62;&#60;/asp:Label&#62; &#60;/asp:TableCell&#62; &#60;asp:TableCell ID="TableCell49" runat="server" Width="10%" HorizontalAlign="center"&#62; &#60;asp:Label ID="Label7" runat="server" Text="HC"&#62;&#60;/asp:Label&#62; &#60;/asp:TableCell&#62; &#60;asp:TableCell ID="TableCell50" runat="server" Width="20%" HorizontalAlign="center"&#62; &#60;/asp:TableCell&#62; &#60;/asp:TableRow&#62; &#60;/asp:Table&#62;&#60;/EmptyDataTemplate&#62;  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=5&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<pre></pre>
<pre class="ProjectHeader NoMargin"><span>&lt;EmptyDataTemplate&gt;
&lt;asp:Table ID="tblEmptyHeadCount" runat="server" Width="100%"&gt;
&lt;asp:TableRow ID="TableRow17" runat="server" HorizontalAlign="right" Width="100%"&gt;
&lt;asp:TableCell ID="TableCell42" runat="server" Width="15%"&gt;&lt;/asp:TableCell&gt;
&lt;asp:TableCell ID="TableCell43" runat="server" Width="40%" HorizontalAlign="center"&gt;
&lt;asp:Label ID="lbl1" runat="server" Text="Position"&gt;&lt;/asp:Label&gt;
&lt;/asp:TableCell&gt;
&lt;asp:TableCell ID="TableCell48" runat="server" Width="15%" HorizontalAlign="center"&gt;
&lt;asp:Label ID="Label6" runat="server" Text="FTE Salary"&gt;&lt;/asp:Label&gt;
&lt;/asp:TableCell&gt;
&lt;asp:TableCell ID="TableCell49" runat="server" Width="10%" HorizontalAlign="center"&gt;
&lt;asp:Label ID="Label7" runat="server" Text="HC"&gt;&lt;/asp:Label&gt;
&lt;/asp:TableCell&gt;
&lt;asp:TableCell ID="TableCell50" runat="server" Width="20%" HorizontalAlign="center"&gt;
&lt;/asp:TableCell&gt;
&lt;/asp:TableRow&gt;
&lt;/asp:Table&gt;&lt;/EmptyDataTemplate&gt;<a id="ctl00_ctl00_WideContent_ProjectTitleControl1_ProjectTitleLink" class="NoUnderline" href="http://code.msdn.microsoft.com/AlwaysShowHeaderFoot"></a></span></pre>
<p><span></p>
<pre class="ProjectHeader NoMargin"> </pre>
<p></span></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ravidotnet.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ravidotnet.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ravidotnet.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ravidotnet.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ravidotnet.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ravidotnet.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ravidotnet.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ravidotnet.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ravidotnet.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ravidotnet.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ravidotnet.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ravidotnet.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ravidotnet.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ravidotnet.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=5&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ravidotnet.wordpress.com/2008/12/16/always-show-gridview-header-and-footer-if-data-soure-is-empty/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64d4bdd3e0210031e9fc0ae3c28edfc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcamail2002</media:title>
		</media:content>
	</item>
		<item>
		<title>Portable Data Aceess Layer</title>
		<link>http://ravidotnet.wordpress.com/2008/05/12/portable-data-aceess-layer/</link>
		<comments>http://ravidotnet.wordpress.com/2008/05/12/portable-data-aceess-layer/#comments</comments>
		<pubDate>Mon, 12 May 2008 06:27:29 +0000</pubDate>
		<dc:creator>mcamail2002</dc:creator>
				<category><![CDATA[ADO.NET]]></category>

		<guid isPermaLink="false">http://ravidotnet.wordpress.com/?p=3</guid>
		<description><![CDATA[In most applications database access is restricted to a single database type. if you need to connect to multiple types of databases? Do you write a data access class for each database? When faced with this dilemma working on a project I decided to use a single class and create the data access objects through [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=3&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><span style="font-size:85%;color:#000000;font-family:arial;">In most applications database access is restricted to a single database type. if you need to connect to multiple types of databases? Do you write a data access class for each database? When faced with this dilemma working on a project I decided to use a single class and create the data access objects through reflection.</span><br />
############################################<br />
<span style="color:#000000;"><span style="color:#000000;">DataProviderFactory</span>.cs Class</span><br />
############################################<br />
using System.Data;<br />
using System.Collections;<br />
using Microsoft.VisualBasic;<br />
using System.Diagnostics;<br />
using System;</p>
<p>namespace PortableDAL<br />
{<br />
internal class DataProviderFactory<br />
{<br />
public static IDbConnection CreateConnection(Type providerType)<br />
{<br />
return ((IDbConnection) Activator.CreateInstance(providerType));<br />
} //CreateConnection<br />
public static IDbConnection CreateConnection(Type providerType, string connectionString)<br />
{<br />
object[] args = new object[] { connectionString };<br />
return ((IDbConnection) Activator.CreateInstance(providerType, args));<br />
} //CreateConnection<br />
public static IDbCommand CreateCommand(Type providerType)<br />
{<br />
return ((IDbCommand) Activator.CreateInstance(providerType));<br />
} //CreateCommand<br />
public static IDbCommand CreateCommand(Type providerType, string cmdText)<br />
{<br />
object[] args = new object[] { cmdText };<br />
return ((IDbCommand) Activator.CreateInstance(providerType, args));<br />
} //CreateCommand<br />
public static IDbCommand CreateCommand(Type providerType, string cmdText, IDbConnection connection)<br />
{<br />
object[] args = new object[] { cmdText, connection };<br />
return ((IDbCommand) Activator.CreateInstance(providerType, args));<br />
} //CreateCommand<br />
public static IDbCommand CreateCommand(Type providerType, string cmdText, IDbConnection connection, IDbTransaction transaction)<br />
{<br />
object[] args = new object[] { cmdText, connection, transaction };<br />
return ((IDbCommand) Activator.CreateInstance(providerType, args));<br />
} //CreateCommand<br />
public static IDbDataAdapter CreateDataAdapter(Type providerType)<br />
{<br />
return ((IDbDataAdapter) Activator.CreateInstance(providerType));<br />
} //CreateDataAdapter<br />
public static IDbDataAdapter CreateDataAdapter(Type providerType, IDbCommand selectCommand)<br />
{<br />
object[] args = new object[] { selectCommand };<br />
return ((IDbDataAdapter) Activator.CreateInstance(providerType, args));<br />
} //CreateDataAdapter<br />
public static IDbDataAdapter CreateDataAdapter(Type providerType, string selectCommandText, IDbConnection selectConnection)<br />
{<br />
object[] args = new object[] { selectCommandText, selectConnection };<br />
return ((IDbDataAdapter) Activator.CreateInstance(providerType, args));<br />
} //CreateDataAdapter<br />
public static IDbDataAdapter CreateDataAdapter(Type providerType, string selectCommandText, string selectConnectionString)<br />
{<br />
object[] args = new object[] { selectCommandText, selectConnectionString };<br />
return ((IDbDataAdapter) Activator.CreateInstance(providerType, args));<br />
} //CreateDataAdapter<br />
public static IDbDataParameter CreateDataParameter(Type providerType)<br />
{<br />
return ((IDbDataParameter) Activator.CreateInstance(providerType));<br />
} //CreateDataParameter<br />
public static IDbDataParameter CreateDataParameter(Type providerType, string parameterName, object value)<br />
{<br />
object[] args = new object[] { parameterName, value };<br />
return ((IDbDataParameter) Activator.CreateInstance(providerType, args));<br />
} //CreateDataParameter<br />
public static IDbDataParameter CreateDataParameter(Type providerType, string parameterName, object value, DbType dataType)<br />
{<br />
IDbDataParameter param = CreateDataParameter(providerType);<br />
if (!(param == null))<br />
{<br />
param.ParameterName = parameterName;<br />
param.DbType = dataType;<br />
param.Value = value;<br />
}<br />
return param;<br />
} //CreateDataParameter<br />
public static IDbDataParameter CreateDataParameter(Type providerType, string parameterName, DbType dataType, int size)<br />
{<br />
IDbDataParameter param = CreateDataParameter(providerType);<br />
if (!(param == null))<br />
{<br />
param.ParameterName = parameterName;<br />
param.DbType = dataType;<br />
param.Size = size;<br />
}<br />
return param;<br />
} //CreateDataParameter<br />
public static IDbDataParameter CreateDataParameter(Type providerType, string parameterName, DbType dataType, int size, string sourceColumn)<br />
{<br />
IDbDataParameter param = CreateDataParameter(providerType);<br />
if (!(param == null))<br />
{<br />
param.ParameterName = parameterName;<br />
param.DbType = dataType;<br />
param.Size = size;<br />
param.SourceColumn = sourceColumn;<br />
}<br />
return param;<br />
} //CreateDataParameter<br />
public static IDbDataParameter CreateDataParameter(Type providerType, string parameterName, DbType dataType, int size, ParameterDirection direction)<br />
{<br />
IDbDataParameter param = CreateDataParameter(providerType);<br />
if (!(param == null))<br />
{<br />
param.ParameterName = parameterName;<br />
param.DbType = dataType;<br />
param.Size = size;<br />
param.Direction = direction;<br />
}<br />
return param;<br />
} //CreateDataParameter<br />
public static IDbDataParameter CreateDataParameter(Type providerType, string parameterName, DbType dataType, object value, int size, ParameterDirection direction)<br />
{<br />
IDbDataParameter param = CreateDataParameter(providerType);<br />
if (!(param == null))<br />
{<br />
param.ParameterName = parameterName;<br />
param.DbType = dataType;<br />
param.Size = size;<br />
param.Direction = direction;<br />
param.Value = value;<br />
}<br />
return param;<br />
} //CreateDataParameter<br />
}<br />
}<br />
#########################################<br />
DataAccessor.cs Class<br />
#########################################</p>
<p>using System.Data;<br />
using System.Configuration;<br />
using System.Collections;<br />
using Microsoft.VisualBasic;<br />
using System.Diagnostics;<br />
using System;<br />
using System.Data.SqlClient;<br />
using System.Data.OleDb;<br />
using System.Data.Odbc;<br />
using System.Data.OracleClient;<br />
using MySql.Data.MySqlClient;<br />
namespace PortableDAL<br />
{<br />
public enum ProviderType<br />
{<br />
Odbc = 0,<br />
OleDb,<br />
Oracle,<br />
SqlClient,<br />
MySqlClient<br />
}<br />
///&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
/// Project : DataAccess<br />
/// Class : DataAccessor<br />
///<br />
///&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
///<br />
/// Helper for data access<br />
/// Wraps the database access calls<br />
///<br />
/// this class requires a reference to System.Data.OracleClient.dll<br />
/// this class requires a reference to MySql.Data.dll<br />
/// This class is dependent on the DataProviderFactory<br />
///<br />
///&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
public class DataAccessor : IDisposable<br />
{<br />
#region &#8220;Private Fields&#8221;<br />
private const string PARAMS_FAILED = &#8220;Could not attach parameters.&#8221;;<br />
private const int COMMAND_TIMEOUT = 120;<br />
private static Type[] m_connectionTypes = new Type[] { typeof(OdbcConnection), typeof(OleDbConnection), typeof(OracleConnection), typeof(SqlConnection),typeof(MySqlConnection) };<br />
private static Type[] m_dataAdapterTypes = new Type[] { typeof(OdbcDataAdapter), typeof(OleDbDataAdapter), typeof(OracleDataAdapter), typeof(SqlDataAdapter),typeof(MySqlDataAdapter) };<br />
private static Type[] m_parameterTypes = new Type[] { typeof(OdbcParameter), typeof(OleDbParameter), typeof(OracleParameter), typeof(SqlParameter),typeof(MySqlParameter) };<br />
private static Type[] m_commandTypes = new Type[] { typeof(OdbcCommand), typeof(OleDbCommand), typeof(OracleCommand), typeof(SqlCommand),typeof(MySqlCommand) };<br />
private static Type[] m_commandBuilderTypes = new Type[] { typeof(OdbcCommandBuilder), typeof(OleDbCommandBuilder), typeof(OracleCommandBuilder), typeof(SqlCommandBuilder),typeof(MySqlCommandBuilder) };<br />
private IDbConnection m_connection; // connection used by the class<br />
private IDbTransaction m_transaction; // transaction used by the class<br />
private string m_connectionString; // connection string used for the connection<br />
private ProviderType m_provider; // provider type to use for the data objects<br />
private int m_commandTimeout = 120; // Timeout for the command execution<br />
#endregion<br />
#region &#8220;Constructors&#8221;<br />
public DataAccessor(ProviderType provider)<br />
{<br />
m_provider = provider;<br />
} //New<br />
public DataAccessor(string connectionString, ProviderType provider)<br />
{<br />
m_provider = provider;<br />
m_connectionString = connectionString;<br />
} //New<br />
public DataAccessor(ProviderType provider, string key)<br />
{<br />
//get the connection string from the configuration file<br />
System.Configuration.AppSettingsReader appRead = new System.Configuration.AppSettingsReader();<br />
m_connectionString = (string) appRead.GetValue(key, typeof(System.String));<br />
m_provider = provider;<br />
} //New<br />
#endregion<br />
#region &#8220;Properties&#8221;<br />
public ProviderType Provider<br />
{<br />
get<br />
{<br />
return m_provider;<br />
}<br />
set<br />
{<br />
m_provider = value;<br />
}<br />
} //Provider<br />
public string ConnectionString<br />
{<br />
get<br />
{<br />
return m_connectionString;<br />
}<br />
set<br />
{<br />
m_connectionString = value;<br />
}<br />
} //ConnectionString<br />
public IDbConnection Connection<br />
{<br />
get<br />
{<br />
// if the connection does not exist, create and open it<br />
if (m_connection == null)<br />
{<br />
m_connection = DataProviderFactory.CreateConnection(m_connectionTypes[ (int) m_provider], m_connectionString);<br />
m_connection.Open();<br />
}<br />
else<br />
{<br />
if (m_connection.State != ConnectionState.Open)<br />
{<br />
m_connection.Open();<br />
}<br />
}<br />
return m_connection;<br />
}<br />
set<br />
{<br />
m_connection = value;<br />
}<br />
} //Connection<br />
public IDbTransaction Transaction<br />
{<br />
get<br />
{<br />
return m_transaction;<br />
}<br />
set<br />
{<br />
m_transaction = value;<br />
}<br />
} //Transaction<br />
public int CommandTimeout<br />
{<br />
get<br />
{<br />
return m_commandTimeout;<br />
}<br />
set<br />
{<br />
m_commandTimeout = value;<br />
}<br />
} // CommandTimeout<br />
#endregion<br />
#region &#8220;Public Methods&#8221;<br />
public void Dispose()<br />
{<br />
// dispose of the transaction if it exists<br />
if (m_transaction != null)<br />
{<br />
m_transaction.Dispose();<br />
}<br />
// dispose of the connection<br />
if (m_connection != null)<br />
{<br />
if (m_connection.State == ConnectionState.Open)<br />
{<br />
m_connection.Close();<br />
}<br />
m_connection.Dispose();<br />
}<br />
} //Dispose<br />
public IDbTransaction BeginTransaction()<br />
{<br />
if (m_transaction == null)<br />
{<br />
m_transaction = Connection.BeginTransaction();<br />
}<br />
return m_transaction;<br />
} //BeginTransaction<br />
///<br />
/// Execute a IDbCommand (that returns no resultset and takes commandText &amp; commandType parameters) against the database specified in<br />
/// the connection string<br />
///<br />
///<br />
/// e.g.:<br />
/// int result = ExecuteNonQuery(&#8220;GetOrders&#8221;,CommandType.StoredProcedure, 24, 36);<br />
///<br />
///<br />
The stored procedure name or T-SQL command<br />
///<br />
The CommandType (stored procedure, text, etc.)<br />
///<br />
Array of objects holding the values to be assigned<br />
/// An int representing the number of rows affected by the command<br />
public int ExecuteNonQuery(string query, CommandType queryType, IDbDataParameter[] param)<br />
{<br />
IDbCommand cmd = CreateCommand(query, queryType);<br />
AppendParameters(param, cmd);<br />
cmd.Transaction = m_transaction;<br />
int retVal = cmd.ExecuteNonQuery();<br />
if (m_transaction == null)<br />
{<br />
m_connection.Close();<br />
}<br />
cmd.Dispose();<br />
return retVal;<br />
} //ExecuteNonQuery<br />
///<br />
/// Execute a IDbCommand (that returns no resultset and takes commandText &amp; commandType parameters) against the database specified in<br />
/// the connection string<br />
///<br />
///<br />
/// e.g.:<br />
/// DataSet ds = ExecuteDataset(&#8220;GetOrders&#8221;,CommandType.StoredProcedure,24,36 );<br />
///<br />
///<br />
The CommandType (stored procedure, text, etc.)<br />
///<br />
The stored procedure name or T-SQL command<br />
///<br />
Array of objects holding the values to be assigned<br />
///<br />
A dataset containing the resultset generated by the command<br />
/// A dataset containing the resultset generated by the command<br />
public int ExecuteDataset ( string query, CommandType queryType, IDbDataParameter[] param, DataSet ds )<br />
{<br />
IDbCommand cmd = CreateCommand(query, queryType);<br />
AppendParameters(param, cmd);<br />
IDbDataAdapter da = DataProviderFactory.CreateDataAdapter(m_dataAdapterTypes[(int)m_provider]);<br />
da.SelectCommand = cmd;<br />
cmd.Transaction = m_transaction;<br />
int retVal = da.Fill(ds);<br />
if (m_transaction == null)<br />
{<br />
m_connection.Close();<br />
}<br />
cmd.Dispose();<br />
return retVal;<br />
} //ExecuteQuery<br />
///<br />
/// Execute a IDbCommand (that returns no resultset and takes commandText &amp; commandType parameters) against the database specified in<br />
/// the connection string<br />
///<br />
///<br />
/// e.g.:<br />
/// DataSet ds = ExecuteDataset(&#8220;GetOrders&#8221;,CommandType.StoredProcedure,24,36 );<br />
///<br />
///<br />
The CommandType (stored procedure, text, etc.)<br />
///<br />
The stored procedure name or T-SQL command<br />
///<br />
Array of objects holding the values to be assigned<br />
/// A dataset containing the resultset generated by the command<br />
public DataSet ExecuteDataset(string query, CommandType queryType, IDbDataParameter[] param)<br />
{<br />
IDbCommand cmd = CreateCommand(query, queryType);<br />
DataSet ds = new DataSet();<br />
AppendParameters(param, cmd);<br />
IDbDataAdapter da = DataProviderFactory.CreateDataAdapter(m_dataAdapterTypes[ (int) m_provider]);<br />
da.SelectCommand = cmd;<br />
cmd.Transaction = m_transaction;<br />
da.Fill(ds);<br />
cmd.Parameters.Clear();<br />
if (m_transaction == null)<br />
{<br />
m_connection.Close();<br />
}<br />
cmd.Dispose();<br />
return ds;<br />
} //ExecuteDataset<br />
///<br />
/// Execute a IDbCommand (that returns no resultset and takes commandText &amp; commandType parameters) against the database specified in<br />
/// the connection string<br />
///<br />
///<br />
/// e.g.:<br />
/// int orderCount = (int)ExecuteScalar(&#8220;GetOrderCount&#8221;,CommandType.StoredProcedure,24 );<br />
///<br />
///<br />
The CommandType (stored procedure, text, etc.)<br />
///<br />
The stored procedure name or T-SQL command<br />
///<br />
Array of objects holding the values to be assigned<br />
/// An object containing the value in the 1&#215;1 resultset generated by the command<br />
public object ExecuteScalar(string query, CommandType queryType, IDbDataParameter[] param)<br />
{<br />
IDbCommand cmd = CreateCommand(query, queryType);<br />
AppendParameters(param, cmd);<br />
cmd.Transaction = m_transaction;<br />
object retVal = cmd.ExecuteScalar();<br />
if (m_transaction == null)<br />
{<br />
m_connection.Close();<br />
}<br />
cmd.Dispose();<br />
return retVal;<br />
} //ExecuteScalar<br />
///<br />
/// Execute a IDbCommand (that returns no resultset and takes commandText &amp; commandType parameters) against the database specified in<br />
/// the connection string<br />
///<br />
///<br />
/// e.g.:<br />
/// SqlDataReader dr = ExecuteReader( &#8220;GetOrders&#8221;,CommandType.StoredProcedure,24,34);<br />
///<br />
///<br />
The CommandType (stored procedure, text, etc.)<br />
///<br />
The stored procedure name or T-SQL command<br />
///<br />
Array of objects holding the values to be assigned<br />
/// A IDataReader containing the resultset generated by the command<br />
public IDataReader ExecuteReader ( string query, CommandType queryType, IDbDataParameter[] param )<br />
{<br />
IDbCommand cmd = CreateCommand(query, queryType);<br />
AppendParameters(param, cmd);<br />
IDataReader dr;<br />
dr = cmd.ExecuteReader();<br />
cmd.Dispose();<br />
return dr;<br />
} //GetReader<br />
public IDbDataParameter[] GetParameters(string query, CommandType queryType)<br />
{<br />
IDbCommand cmd = CreateCommand(query, queryType);<br />
m_commandBuilderTypes[ (int) m_provider].GetMethod(&#8220;DeriveParameters&#8221;).Invoke(null, new object[] {cmd});<br />
IDbDataParameter[] parameters = new IDbDataParameter[cmd.Parameters.Count-1 + 1];<br />
cmd.Parameters.CopyTo(parameters, 0);<br />
return CloneParameters(parameters);<br />
}<br />
#endregion<br />
#region &#8220;Private Methods&#8221;<br />
///<br />
/// Deep copy of cached IDbDataParameter array<br />
///<br />
///</p>
<p>///<br />
private IDbDataParameter[] CloneParameters(IDbDataParameter[] originalParameters)<br />
{<br />
int i;<br />
int j = originalParameters.Length &#8211; 1;<br />
IDbDataParameter[] clonedParameters = new IDbDataParameter[j + 1];<br />
for (i = 0; i &lt;= j; i++)<br />
{<br />
clonedParameters[i] = (IDbDataParameter) (((ICloneable) originalParameters[i]).Clone());<br />
}<br />
return clonedParameters;<br />
} // CloneParameters<br />
///<br />
/// This method is used to attach array of IDbDataParameter to a IDbCommand.<br />
///<br />
///<br />
An array of IDbDataParameter to be added to command<br />
///<br />
The command to which the parameters will be added<br />
private void AppendParameters(IDbDataParameter[] @params, IDbCommand cmd)<br />
{<br />
// add the parameters to the command<br />
try<br />
{<br />
if (!(@params == null))<br />
{<br />
foreach (IDbDataParameter parameter in @params)<br />
{<br />
cmd.Parameters.Add(parameter);<br />
}<br />
}<br />
}<br />
catch (Exception e)<br />
{<br />
throw (new Exception(PARAMS_FAILED, e));<br />
}<br />
} //AppendParameters<br />
///<br />
/// This method opens (if necessary) and assigns a command type and command text<br />
/// to the provided command<br />
///<br />
///<br />
The IDbCommand to be prepared<br />
///<br />
The CommandType (stored procedure, text, etc.)<br />
///<br />
The stored procedure name or T-SQL command<br />
private IDbCommand CreateCommand(string query, CommandType queryType)<br />
{<br />
// Associate the connection with the command<br />
IDbCommand cmd = Connection.CreateCommand();<br />
// Set the command text (stored procedure name or SQL statement)<br />
cmd.CommandText = query;<br />
// Set the command type<br />
cmd.CommandType = queryType;<br />
// Set the command timeout<br />
cmd.CommandTimeout = m_commandTimeout;<br />
return cmd;<br />
} //CreateCommand<br />
#endregion<br />
} //DataAccessor<br />
}<br />
###############################################<br />
Form1.cs<br />
###############################################<br />
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
namespace PortableDataAccessLayer<br />
{<br />
public partial class Form1 : Form<br />
{<br />
public Form1 ()<br />
{<br />
InitializeComponent();<br />
M_Test();<br />
}<br />
public void M_Test ()<br />
{</p>
<p>// Using Portable Data Access Layer<br />
// To get connection string form configuration file<br />
string m_connectionString = ConfigurationManager.AppSettings.Get(&#8220;ConnectionString&#8221;);<br />
// Passing provider type and configuration key for connection string in configuration file<br />
DataAccessor helper = new DataAccessor(ProviderType.MySqlClient, &#8220;ConnectionString&#8221;);<br />
#region Execute Scalar<br />
// How to get scalar value<br />
IDbDataParameter[] @params = new IDbDataParameter[1];<br />
@params = helper.GetParameters(&#8220;p_GetGlobalBalance&#8221;, CommandType.StoredProcedure);<br />
@params[0].Value = 2;<br />
object dBal = helper.ExecuteScalar(&#8220;p_GetGlobalBalance&#8221;, CommandType.StoredProcedure, @params);<br />
#endregion<br />
#region DataSet<br />
// How to get DataSet<br />
//IDbDataParameter[] @params = new IDbDataParameter[1];<br />
//@params = helper.GetParameters(&#8220;p_ItemUpdates&#8221;, CommandType.StoredProcedure);<br />
//@params[0].Value = objRequest.TerminalID;<br />
//DataSet ds = helper.ExecuteDataset(&#8220;p_ItemUpdates&#8221;, CommandType.StoredProcedure, @params);<br />
#endregion<br />
#region Data Reader<br />
// How to get Data Reader<br />
//IDbDataParameter[] @params = new IDbDataParameter[1];<br />
//@params = helper.GetParameters(&#8220;p_ItemUpdates&#8221;, CommandType.StoredProcedure);<br />
//@params[0].Value = objRequest.TerminalID;<br />
//DbDataReader dr = (DbDataReader)helper.GetReader(&#8220;p_ItemUpdates&#8221;, CommandType.StoredProcedure, @params);<br />
//while(dr.Read())<br />
//{<br />
// string aa = dr.GetValue(3).ToString();<br />
//}<br />
#endregion<br />
helper.Dispose();<br />
}<br />
}<br />
}</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ravidotnet.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ravidotnet.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ravidotnet.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ravidotnet.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ravidotnet.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ravidotnet.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ravidotnet.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ravidotnet.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ravidotnet.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ravidotnet.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ravidotnet.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ravidotnet.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ravidotnet.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ravidotnet.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ravidotnet.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ravidotnet.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ravidotnet.wordpress.com&amp;blog=3675569&amp;post=3&amp;subd=ravidotnet&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ravidotnet.wordpress.com/2008/05/12/portable-data-aceess-layer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/64d4bdd3e0210031e9fc0ae3c28edfc9?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">mcamail2002</media:title>
		</media:content>
	</item>
	</channel>
</rss>
