<?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/"
	>

<channel>
	<title>C# City</title>
	<atom:link href="http://www.csharpcity.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.csharpcity.com</link>
	<description>C# Articles, Tutorials, and Reusable Code</description>
	<lastBuildDate>Mon, 22 Apr 2013 16:06:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Deserializing to Dynamic with RestSharp</title>
		<link>http://www.csharpcity.com/2013/deserializing-to-dynamic-with-restsharp/</link>
		<comments>http://www.csharpcity.com/2013/deserializing-to-dynamic-with-restsharp/#comments</comments>
		<pubDate>Mon, 22 Apr 2013 16:06:53 +0000</pubDate>
		<dc:creator>Ashiq Alibhai, PMP</dc:creator>
				<category><![CDATA[Libraries]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Wndows Forms]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[json.net]]></category>
		<category><![CDATA[restsharp]]></category>

		<guid isPermaLink="false">http://www.csharpcity.com/?p=482</guid>
		<description><![CDATA[You can use JSON.NET to convert any RestSharp service response to a list of dynamic instances; you just need a bit of plumbing code to do it. <a href="http://www.csharpcity.com/2013/deserializing-to-dynamic-with-restsharp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>RestSharp is awesome. Go try it. I highly recommend it (it&#8217;s overall less code and simpler than WCF, at any rate).</p>
<p>Services may or may not provide a well-defined list of fields to you, the consumer, which doesn&#8217;t change. There are two design options with trade-offs:</p>
<ol>
<li><strong>Wrapper Classes:</strong> Create wrapper classes that convert expected fields in the JSON to a simple C# object. (Eg. for twitter, create a class with <code>CreatedAt</code> and <code>Text</code> for tweets, which expects the API to send <code>created_at</code> and <code>text</code> back.)</li>
<li><strong>Dynamic Classes:</strong> Use the new DLR, don&#8217;t care about static typing and expecting fields at runtime. Take whatever you get, and expose it.</li>
</ol>
<p>For the second option, you need to sort out how to make RestSharp deserialize to (in the case of Twitter tweets) <code>List&lt;dynamic&gt;</code>.</p>
<p>Assuming you already have this working with a wrapper class, you only need to make a couple of changes. First, add a handler with a deserializer, as per <a href="https://gist.github.com/rdingwall/1884642">this gist</a>:</p>
<p><pre><code>
namespace RestSharp.Deserializers
{
&nbsp;&nbsp;public class DynamicJsonDeserializer : IDeserializer
&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;public string RootElement { get; set; }
&nbsp;&nbsp;&nbsp;&nbsp;public string Namespace { get; set; }
&nbsp;&nbsp;&nbsp;&nbsp;public string DateFormat { get; set; }

&nbsp;&nbsp;&nbsp;&nbsp;public T Deserialize&lt;T&gt;(IRestResponse response)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return JsonConvert.DeserializeObject&lt;dynamic&gt;(response.Content);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}
</code></pre></p>
<p>(You need <a href="http://json.codeplex.com/">JSON.NET</a> for this.) When you build your <code>RestClient</code> instance, add the handler to it:</p>
<p><pre><code>
client.AddHandler(&quot;application/json&quot;, new DynamicJsonDeserializer());
</code></pre></p>
<p>Finally, deserialize it. The tricky part is deserializing. The <code>DynamicJsonDeserializer</code> class actually returns (in this case) a <code>JArray</code> instance. To convert that to <code>List&lt;dynamic&gt;</code>, you need to do this:</p>
<p><pre><code>
var response = client.Execute&lt;JArray&gt;(request).Data.ToObject&lt;List&lt;dynamic&gt;&gt;();
</code></pre></p>
<p>Et voila, you&#8217;re done! You now have a list of <code>dynamic</code> objects, and you can dynamically access their fields!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharpcity.com/2013/deserializing-to-dynamic-with-restsharp/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Writing Automated Performance Tests</title>
		<link>http://www.csharpcity.com/2013/writing-automated-performance-tests/</link>
		<comments>http://www.csharpcity.com/2013/writing-automated-performance-tests/#comments</comments>
		<pubDate>Fri, 01 Feb 2013 15:27:54 +0000</pubDate>
		<dc:creator>Ashiq Alibhai, PMP</dc:creator>
				<category><![CDATA[Core .NET]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Wndows Forms]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[automated testing]]></category>
		<category><![CDATA[memory leaks]]></category>

		<guid isPermaLink="false">http://www.csharpcity.com/?p=477</guid>
		<description><![CDATA[I found a memory leak in my application, and wrote an automated test using GC.GetTotalMemory to tell if objects were leaking or not. They were (20MB of them in 100k instances). <a href="http://www.csharpcity.com/2013/writing-automated-performance-tests/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I recently found out how easy it is to see how much total RAM your .NET app uses: `GC.GetTotalMemory(false)`. This gives you a simple total number of bytes that your app is using.</p>
<p>Throwing this into the trace that outputs the game FPS every second, I noticed a repetitive workflow where my game&#8217;s RAM usage kept climbing, from around 10MB (baseline) to over 30MB!</p>
<p>Having a qualified, quantified problem (potentially unlimited growth of memory usage, i.e. a memory leak) I sat out to answer an important question: <em>could I somehow write an automated test that would check if this workflow leaked memory?</em></p>
<p>The answer was a resounding <em>yes</em>. Using the very powerful Moq framework, I was able to rip out most external dependencies and isolate my workflow to two calls: a constructor and <code>Initialize</code> method on one of my <code>Screen</code> subclasses.</p>
<p>Once I did this, simply creating and disposing 10k objects and checking the memory usage (before and after) indicated whether the usage shot up. A small rise (10k? 100k?) was probably insignificant; in my case, I saw <em>more than a 20MB increase</em> in RAM usage.</p>
<p>Also, one key here is to run <code>GC.GetTotalMemory(true)</code>. This forces the GC to collect anything immediately, which is slower, but necessary. Without it, we can&#8217;t tell if the memory increase is due to disposed objects that <em>the GC didn&#8217;t get around to disposing yet</em>.</p>
<p>The culprit? Appending event handlers in the constructor which <em>should have been</em> removed on subsequent calls to dispose (but weren&#8217;t).</p>
<p>The conclusion? Testing for memory leaks is not hard, and can be very useful under certain circumstances, provided you have enough infrastructure in place to mock out dependencies.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharpcity.com/2013/writing-automated-performance-tests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DriveInfo: Total vs. Available Free Space</title>
		<link>http://www.csharpcity.com/2013/driveinfo-total-vs-available-free-space/</link>
		<comments>http://www.csharpcity.com/2013/driveinfo-total-vs-available-free-space/#comments</comments>
		<pubDate>Wed, 02 Jan 2013 14:27:53 +0000</pubDate>
		<dc:creator>Ashiq Alibhai, PMP</dc:creator>
				<category><![CDATA[Core .NET]]></category>
		<category><![CDATA[Wndows Forms]]></category>
		<category><![CDATA[hardware]]></category>

		<guid isPermaLink="false">http://www.csharpcity.com/?p=473</guid>
		<description><![CDATA[DriveInfo.AvailableFreeSpace gives you free space with quotas applied; on the other hand, TotalFreeSpace gives you the total, physical amount of empty space on the drive. <a href="http://www.csharpcity.com/2013/driveinfo-total-vs-available-free-space/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>If you&#8217;re curious about how query a drive and find out how much free space is left on it, in C#, there&#8217;s a conveniant <a href="http://msdn.microsoft.com/en-us/library/system.io.driveinfo.aspx">DriveInfo class</a> which exposes two properties for your usage: <code>TotalFreeSpace</code> and <code>AvailableFreeSpace</code>.</p>
<p>What&#8217;s the difference?</p>
<p><strong>AvailableFreeSpace</strong> returns the amount of free space available <em>given disk quotas.</em> Yes, it&#8217;s true; apparently, <a href="http://en.wikipedia.org/wiki/Disk_quota#Quotas">every Windows OS since Windows 2000</a> implements and allows you to specify storage quotas. </p>
<p><strong>TotalFreeSpace</strong>, on the other hand, just gives you the total amount of free space on the disk &#8212; just bear in mind that you may not be able to use all of it (depending on your user and permissions/quotas).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharpcity.com/2013/driveinfo-total-vs-available-free-space/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Drawing vs. Updating and IsFixedTimeStamp (XNA)</title>
		<link>http://www.csharpcity.com/2012/drawing-vs-updating-and-isfixedtimestamp-xna/</link>
		<comments>http://www.csharpcity.com/2012/drawing-vs-updating-and-isfixedtimestamp-xna/#comments</comments>
		<pubDate>Fri, 07 Dec 2012 16:39:26 +0000</pubDate>
		<dc:creator>Ashiq Alibhai, PMP</dc:creator>
				<category><![CDATA[Libraries]]></category>
		<category><![CDATA[monogame 3]]></category>
		<category><![CDATA[XNA]]></category>

		<guid isPermaLink="false">http://www.csharpcity.com/?p=469</guid>
		<description><![CDATA[XNA.Game.IsFixedTimeStep allows you to run Draw calls after every update, or to limit them to a certain FPS (and also provide fixed-time updates instead of variable ones). <a href="http://www.csharpcity.com/2012/drawing-vs-updating-and-isfixedtimestamp-xna/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Before we discuss frame rate, we need to understand games from an MVC-like perspective.</p>
<h3>Game State vs. Game Presentation</h3>
<p>You can divide game &#8220;stuff&#8221; into two things: game <em>state</em> (data, classes, etc. of how things look right now &#8212; the current player the level is on, health, position), and your <em>presentation</em> (3D models, lights, textures, sprites, etc.).</p>
<p>Microsoft build XNA with the understanding that these two things are separate, but related. For example, if you have a physics-like simulation, you may want to <em>update the state/simulation several times between calls to <code>Draw</code>.</em> This could be because your users have older hardware, and while they can&#8217;t draw things at 60 (or maybe even 30) FPS, they shouldn&#8217;t have the game run slowly.</p>
<p>To say that another way, the state of the game and the display of the game should be independent. You want the game state to always, always be consistent and to run &#8220;fast,&#8221; even if you can&#8217;t draw that fast; this means your game will still play at the same speed, regardless of CPU/hardware performance &#8212; it&#8217;ll just draw less frames on slower computers than on newer ones.</p>
<h3>Who Cares, Tell Me About IsFixedTimeStep?</h3>
<p>Now that you (hopefully) understand this difference, lets talk about <code>IsFixedTimeStep</code>. <code>IsFixedTimeStep</code> is <code>true</code> by default, which means that XNA calls <code>Draw</code> <strong>no more than once every 1/60th of a second.</strong> (You can change this frequency if you modify the <code>TargetElapsedTime</code> property.)</p>
<p>That&#8217;s what &#8220;fixed time-step&#8221; means &#8212; every call to <code>Draw</code> occurs at the same, fixed, time-step. Because game state is not the same thing as game presentation, <strong>calls to <code>Update</code> still occur as frequently as your computer can handle</strong>. (Regardless, I suggest you <a href="http://gafferongames.com/game-physics/fix-your-timestep/">use a semi-fixed timestamp</a> to avoid troubles like fast-moving objects &#8220;skipping&#8221; through walls.)</p>
<p>This is generally a good thing. It means that your game will run at 60FPS, and not faster. After update calls, XNA will ask, &#8220;did 1/60th of a second pass since the last <code>Draw</code> call yet? No? Okay, I&#8217;m not drawing anything yet.</p>
<p>This also means that you don&#8217;t lose &#8220;resolution&#8221; in your game state by limiting it to 60FPS.</p>
<p>On the other hand, setting this to <code>false</code> causes <strong>XNA to call <code>Draw</code> after every <code>Update</code>.</strong> It doesn&#8217;t hold back. This means you will <em>eat more CPU cycles, but potentially run a faster FPS on better hardware.</em> Again, it won&#8217;t change your game state update frequency, since that runs full-tilt regardless of the framerate.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharpcity.com/2012/drawing-vs-updating-and-isfixedtimestamp-xna/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running Windows Services in Visual Studio</title>
		<link>http://www.csharpcity.com/2012/running-windows-services-in-visual-studio/</link>
		<comments>http://www.csharpcity.com/2012/running-windows-services-in-visual-studio/#comments</comments>
		<pubDate>Wed, 14 Nov 2012 18:12:47 +0000</pubDate>
		<dc:creator>Ashiq Alibhai, PMP</dc:creator>
				<category><![CDATA[Core .NET]]></category>
		<category><![CDATA[Wndows Forms]]></category>
		<category><![CDATA[windows service]]></category>

		<guid isPermaLink="false">http://www.csharpcity.com/?p=464</guid>
		<description><![CDATA[You can run a project that's a Windows service with an infinite loop (yes, really) by invoking the service and waiting. Forever. You can do this conditionally (while not debugging), instead of running ServiceBase.Run. <a href="http://www.csharpcity.com/2012/running-windows-services-in-visual-studio/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Have you ever wanted to run a Windows service, without installing it, through Visual Studio? It&#8217;s actually not that hard to do this.</p>
<p>This is useful in cases where you have a component which is a Windows service, and you need to run integration tests against that service, but don&#8217;t want the hassle of continuously installing and uninstalling the service; you just want to use the latest code version.</p>
<p>The safest way to do this (although I wouldn&#8217;t recommend checking this in to your production code-line) would be to check if the environment is interactive (or if the debugger is running), and simply invoke the service directly, then keep the process suspended.</p>
<p><pre><code>static void main() {
&nbsp;&nbsp;ServiceBase someService = new ServiceBase[] { new MyService() };
&nbsp;&nbsp;if (!Environment.UserInteractive) {
&nbsp;&nbsp;&nbsp;&nbsp;ServiceBase.Run(someService);
&nbsp;&nbsp;} else {
&nbsp;&nbsp;&nbsp;&nbsp;someService[0].OnStart(new string[0]); // Pass no command-line args
&nbsp;&nbsp;&nbsp;&nbsp;while (true) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Thread.Sleep(100); // Suspended animation
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}</code></pre></p>
<p>Yep, that&#8217;s right, <em>I used an infinite loop and didn&#8217;t call <code>OnStop</code>.</em> Like I said, this is a quick and dirty solution to get around this problem.</p>
<p>As an alternative, you can use InstallUtil, PowerShell, or some other methods &#8212; which I&#8217;m not familiar with and can&#8217;t recommend.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharpcity.com/2012/running-windows-services-in-visual-studio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Enumerations as Bit Flags</title>
		<link>http://www.csharpcity.com/2012/using-enumerations-as-bit-flags/</link>
		<comments>http://www.csharpcity.com/2012/using-enumerations-as-bit-flags/#comments</comments>
		<pubDate>Thu, 08 Nov 2012 20:50:38 +0000</pubDate>
		<dc:creator>Ashiq Alibhai, PMP</dc:creator>
				<category><![CDATA[Core .NET]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Wndows Forms]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[bitwise operators]]></category>
		<category><![CDATA[enumerations]]></category>

		<guid isPermaLink="false">http://www.csharpcity.com/?p=455</guid>
		<description><![CDATA[You can use enumerations as bit flags. The FlagsAttribute is optional; more importantly, you need to assign powers of 2^n to the enum's values. Depending on the .NET version, checking can be easy (.HasFlag) or hard (bitwise AND operation) <a href="http://www.csharpcity.com/2012/using-enumerations-as-bit-flags/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>Enumerations are great. Bit flags are also great. How can we combine these two together?</p>
<p>That is, we want to do something like:</p>
<p><pre><code>
enum Fruits { Apple, Pear, Mango, Grapes }
Fruits f = Fruits.Apple | Fruits.Pear;
// pseudocode, bear with me
if (f has Apple) { ... }
if (f has Mango) { ... }
</code></pre></p>
<p>How?</p>
<h2>In Any Version of .NET</h2>
<p>The core step is really to <strong>assign 2^n integer values to each value of the enumeration</strong>, like so:</p>
<p><pre><code>
enum Fruits {
&nbsp;&nbsp;Apple = 1,
&nbsp;&nbsp;Pear = 2,
&nbsp;&nbsp;Mango = 4,
&nbsp;&nbsp;Grapes = 8
}
</code></pre></p>
<p>Note that <strong>adding the Flags attribute is <em>not</em> required.</strong> It&#8217;s optional. It just gives you nice <code>ToString()</code> representations.</p>
<p>All that&#8217;s left is checking the value. </p>
<p>How?</p>
<h2>In .NET 4.0</h2>
<p>.NET 4.0 has this down. It&#8217;s as simple as:</p>
<p><pre><code>
Fruits f = Fruits.Apple | Fruits.Mango;
if (f.HasFlag(Fruit.Apple)) { ... }
</code></pre></p>
<h2>In Pre-4.0</h2>
<p>Unfortunately, it&#8217;s not that easy before .NET 4.0, but it&#8217;s still quite easy. If you understand bit operators, then the same principle applies.</p>
<p>Look at it as binary. If apple = 1 and mango = 4, these in binary are:</p>
<p><pre><code>
Apple: 0x001
Mango: 0x100
</code></pre></p>
<p>Combining them (ANDing them) gives us <code>0x101</code>, or a value of <code>5</code>.</p>
<p>Reversing the value of five, how do we know if the third bit (for mango) is enabled? A simple AND operation:</p>
<p><pre><code>
Source: 0x101
Mango:&nbsp;&nbsp;0x100
=============
Result: 0x100
</code></pre></p>
<p>Since AND requires both bits to be one for the resulting bit to be one, ANDing something to any value gives you that value back if the bit was set.</p>
<p>So we get:</p>
<p><pre><code>
if (f &amp; Fruits.Mango == Fruits.Mango) { ... we have mangos ... }
</code></pre></p>
<p>Simple, right?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharpcity.com/2012/using-enumerations-as-bit-flags/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating an Instance Without Using Constructors</title>
		<link>http://www.csharpcity.com/2012/creating-an-instance-without-using-constructors/</link>
		<comments>http://www.csharpcity.com/2012/creating-an-instance-without-using-constructors/#comments</comments>
		<pubDate>Thu, 01 Nov 2012 16:58:24 +0000</pubDate>
		<dc:creator>Ashiq Alibhai, PMP</dc:creator>
				<category><![CDATA[Core .NET]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Wndows Forms]]></category>
		<category><![CDATA[reflection]]></category>

		<guid isPermaLink="false">http://www.csharpcity.com/?p=448</guid>
		<description><![CDATA[You can create an instance of a class without calling any of its contructors, via a call to System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(A)). Scary, but useful in certain cases (like creating instances of highly-coupled or hard-to-instantiate classes in tests). <a href="http://www.csharpcity.com/2012/creating-an-instance-without-using-constructors/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>I recently worked on a complicated API project which had tight coupling. To create an instance of my class, I had to instantiate two variables of other classes, which were themselves difficult to instantiate (one didn&#8217;t have a public constructor).</p>
<p>As a simplified example, I need to instantiate <code>A</code>, which only has one constructor:</p>
<p><code>public class A(B b, C c);</code></p>
<p>In this case, <code>C</code> did not have public constructors, and <code>B</code> required instances of other classes which were not trivial to instantiate.</p>
<p>Sigh.</p>
<p>Reflection to the rescue! Actually, no. Using the usual <code>someType.GetConstuctor(...).Invoke(null)</code> required a parameterless constructor; ditto for <code>Activator.CreateInstance</code>. While I <em>could</em> have simply added a parameterless constructor, since this project was an API, that would change the API and possibly cause other problems if used outside of testing.</p>
<p>While searching for another solution, I found <a href="http://stackoverflow.com/a/2501740/210780">this pro tip by Mark Gravell</a>: you can actually create an instance <em>without initializing it</em> by calling:</p>
<p><code>A a = (A) System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeof(A));</code></p>
<p>Where <code>A</code> is the class you wish to instantiate. It&#8217;s tricky, and probably not what you ever want to do in production code; in test code, it was exactly the solution I needed. Thanks, Mark!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharpcity.com/2012/creating-an-instance-without-using-constructors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Simple Strategies to Boost SQL Server Performance</title>
		<link>http://www.csharpcity.com/2012/simple-strategies-to-boost-sql-server-performance/</link>
		<comments>http://www.csharpcity.com/2012/simple-strategies-to-boost-sql-server-performance/#comments</comments>
		<pubDate>Mon, 14 May 2012 17:54:02 +0000</pubDate>
		<dc:creator>Ashiq Alibhai, PMP</dc:creator>
				<category><![CDATA[Platform]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[Topic]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.csharpcity.com/?p=443</guid>
		<description><![CDATA[SQL Server can be a beast on your disk. Two easy ways to help raise performance for high-volume databases: Use a drive other than the Windows partition for the SQL Server DB files, and split the MDF and LDF files onto different drives if possible. <a href="http://www.csharpcity.com/2012/simple-strategies-to-boost-sql-server-performance/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>SQL Server is quite the beast. It eats most of your available RAM. And, with a high-volume database, it can also gobble up your IO, leaving your disk with a long queue.</p>
<p>Disk Queue Length, incidentally, is something you can see in Windows 7+, under the Resource Monitor (Start > Task Manager > Performance tab > Resource Monitor > Disk tab)</p>
<p><img src="http://www.csharpcity.com/wp-content/uploads/2012/05/disk-queue-length.png" alt="" title="disk-queue-length" width="800" height="600" class="alignnone size-full wp-image-444" /></p>
<p>Anyway, during large inserts, or imports, or selects, you may see your disk queue length spike up. This means that your computer is requesting data from the disk, but it&#8217;s busy spinning and getting data for different requests; so they start to rack up.</p>
<p>If you see a queue length of, say, 50, that means you have some heavy IO that&#8217;s sucking up all your disk availability and queueing up tons of requests &#8212; like when you&#8217;re restoring an 8GB database into SQL Server (please, use Attach next time.)</p>
<p>Two easy ways to increase performance in this case:</p>
<ol>
<li><strong>Not the Windows Partition:</strong> By default, SQL Server installs to C:\, the same place as Windows. <em>Move the data files off of the Windows drive.</em> You will immediately see an (often drastic) drop in queue length. Why? Because Windows makes a lot of requests, and if they start to queue up, it can make the queue much longer. If you have a second (physical, not logical) hard drive, that&#8217;s an ideal place to locate the files.</li>
<li><strong>Split MDF and LDF Drives:</strong> Ideally, if you have three or more drives, you should put Windows on one, your MDFs on a second, and your LDFs on a third for maximum performance.</li>
</ol>
<p>Again, unless you see the specific problem of disk queue length, you might not need to do all this. Plus, memory is usually a heavier bottleneck for SQL Server, not disk &#8212; so check that first.</p>
<p>How can you set the location of the MDF and LDF files? With new databases, when creating them, SQL Server shows you &#8220;Database Files&#8221; listed under &#8220;General,&#8221; with a &#8220;Path&#8221; property. Just change it to something else.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharpcity.com/2012/simple-strategies-to-boost-sql-server-performance/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Storing a String List in App.Config or Web.Config</title>
		<link>http://www.csharpcity.com/2012/storing-a-string-list-in-app-config-or-web-config/</link>
		<comments>http://www.csharpcity.com/2012/storing-a-string-list-in-app-config-or-web-config/#comments</comments>
		<pubDate>Thu, 10 May 2012 21:38:23 +0000</pubDate>
		<dc:creator>Ashiq Alibhai, PMP</dc:creator>
				<category><![CDATA[Core .NET]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Wndows Forms]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[app.config]]></category>
		<category><![CDATA[web.config]]></category>

		<guid isPermaLink="false">http://www.csharpcity.com/?p=441</guid>
		<description><![CDATA[How can you store a list of strings (or ints, say) into a single appSetting key/value pair? Surprisingly, it's as easy as using a delimited list (like comma-delimited or semicolon-delimited) and splitting it at runtime. <a href="http://www.csharpcity.com/2012/storing-a-string-list-in-app-config-or-web-config/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>App.Config and Web.Config allow you to easily store configuration for use at runtime. The facility they expose is key/value pairs, like so:</p>
<p><pre><code>
&lt;appSettings&gt;
&nbsp;&nbsp;&lt;add key=&quot;drive&quot; value=&quot;C:\&quot; /&gt;
&lt;/appsettings&gt;
</code></pre></p>
<p>At runtime, you can access these values like so:</p>
<p><pre><code>
System.Configuration.ConfigurationManager.AppSettings[&quot;drive&quot;]; // returns &quot;C:\&quot;
</code></pre></p>
<p>This is great for storing a few key/value pairs for different uses. But what if you wanted to store a list of strings? In our example, what if you wanted to store multiple drives? How can you do this?</p>
<p>One solution is to use multiple key/value pairs, like so:</p>
<p><pre><code>
&lt;appSettings&gt;
&nbsp;&nbsp;&lt;add key=&quot;drive1&quot; value=&quot;C:\&quot; /&gt;
&nbsp;&nbsp;&lt;add key=&quot;drive2&quot; value=&quot;D:\&quot; /&gt;
&lt;/appsettings&gt;
</code></pre></p>
<p>Of course, this means you have to individually access the values at runtime; you can&#8217;t just grab them all into a single array or string.</p>
<p>The answer is surprisingly easy: just <strong>store the value as a delimited list and split it at runtime,</strong> like so:</p>
<p><pre><code>
&lt;appSettings&gt;
&nbsp;&nbsp;&lt;add key=&quot;drives&quot; value=&quot;C:, D:&quot; /&gt;
&lt;/appsettings&gt;
</code></pre></p>
<p>And at runtime:</p>
<p><pre><code>
string[] drives = ConfigurationManager.AppSettings[&quot;drives&quot;].Split(&#039;,&#039;).Select(s =&gt; s.Trim()).ToArray();
</code></pre></p>
<p>(The <code>Select</code> and <code>Trim</code> calls allow us to use white-spaces in the config file, and get a trimmed, whitespace-free list of drives.) This code would return, as expected, a string array:</p>
<p><pre><code>
drives[0] = &quot;C:&quot;
drives[1] = &quot;D:&quot;
</code></pre></p>
<p>Easy, right? But not so obvious. In fact, the obvious answer to this is <strong>to use a custom section</strong>. Personally, I feel that this is overkill for  asimple list of strings, ints, or other basic types. (Technically, you can shove any serializable class in here; as long as you have a non-ambiguous delimiter, you&#8217;re good.)</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharpcity.com/2012/storing-a-string-list-in-app-config-or-web-config/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Procedure Expects Parameter Which Was Not Supplied</title>
		<link>http://www.csharpcity.com/2012/procedure-expects-parameter-which-was-not-supplied/</link>
		<comments>http://www.csharpcity.com/2012/procedure-expects-parameter-which-was-not-supplied/#comments</comments>
		<pubDate>Mon, 12 Mar 2012 19:40:42 +0000</pubDate>
		<dc:creator>Ashiq Alibhai, PMP</dc:creator>
				<category><![CDATA[Core .NET]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[Wndows Forms]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[null]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[System.Data]]></category>

		<guid isPermaLink="false">http://www.csharpcity.com/?p=439</guid>
		<description><![CDATA[SqlCommand's Parameters prevent SQL injection attacks. Surprisingly, when passed in a null value, you get the exception: Procedure or function "..." expects parameter '@foo', which was not supplied. <a href="http://www.csharpcity.com/2012/procedure-expects-parameter-which-was-not-supplied/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>SQL parameters are awesome. They automatically protect you from SQL injection attacks. They do have one strange caveat, though: <strong>you cannot pass a null value to a SQL parameter.</strong> Doing so will give you this interesting and unhelpful exception:</p>
<p><code>Procedure or function &quot;...&quot; expects parameter &#039;@foo&#039;, which was not supplied</code></p>
<p>This doesn&#8217;t make sense when you have code like:</p>
<p><pre><code>
string foo = getSomeFoo(); // May return null
SqlCommand cmd = new SqlCommand(&quot;SELECT * FROM someTable WHERE foo = @foo&quot;);
cmd.Parameters.AddWithValue(&quot;@foo&quot;, foo);
cmd.ExecuteNonQuery(); // Throws exception
</code></pre></p>
<p>You can see that <code>@foo</code> is, indeed, supplied. But in the case where the value is null, you will see an exception.</p>
<p>So what&#8217;s the solution? You can make sure that <code>foo</code> has a value, or some non-null value (like empty string or <code>DBNull.Value</code>), or you can conditionally add the parameter when needed.</p>
<p>And calling a stored procedure with a parameter that may be null? That, my friends, is <a href="https://www.google.ca/webhp?ie=UTF-8&#038;ion=1#hl=en&#038;sclient=psy-ab&#038;q=sql+parameter+null&#038;oq=sql+parameter+null&#038;fp=1">a question for Google</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.csharpcity.com/2012/procedure-expects-parameter-which-was-not-supplied/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
