<?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>Dumping Ground for Random Thoughts &#187; Development</title>
	<atom:link href="http://ke4ktz.org/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://ke4ktz.org</link>
	<description>Random thoughts and information from anywhere they happen to come from.</description>
	<lastBuildDate>Sat, 26 Nov 2011 03:14:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='ke4ktz.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Creating an RSS Reader Using ASP.NET and LINQ &#8211; Part 1</title>
		<link>http://ke4ktz.org/2011/11/creating-an-rss-reader-using-asp-net-and-linq-part-1/</link>
		<comments>http://ke4ktz.org/2011/11/creating-an-rss-reader-using-asp-net-and-linq-part-1/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 23:32:50 +0000</pubDate>
		<dc:creator>Glenn</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://ke4ktz.org/?p=778</guid>
		<description><![CDATA[With every release of the .NET Framework, more power has been placed in the developer&#8217;s hands.  We can now concentrate on the overall process of our applications rather than developing the low-level functionality.  In this post, I&#8217;ll start creating a simple RSS reader using ASP.NET and LINQ. Feed Definitions The first step is to decide [...]]]></description>
			<content:encoded><![CDATA[<p>With every release of the .NET Framework, more power has been placed in the developer&#8217;s hands.  We can now concentrate on the overall process of our applications rather than developing the low-level functionality.  In this post, I&#8217;ll start creating a simple RSS reader using ASP.NET and LINQ.</p>
<h2>Feed Definitions</h2>
<p>The first step is to decide how to define the RSS feed information and how our program will use it.  The basic information we&#8217;re going to need will be:</p>
<ul>
<li>The name of the feed.</li>
<li>The URL of the feed.</li>
</ul>
<p>For ease of maintenance, and to allow updating the list without having to edit our source code, we&#8217;ll store the feed definitions in an external file.  There are many options for this and we&#8217;ll use the easiest option: XML.  Our file will have a basic format, only storing the basic information that we need:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?--&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Feeds<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Feed<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Stuck In Customs<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://www.stuckincustoms.com/feed/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Feed<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Feed<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>2000 Things You Should Know About C#<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://csharp.2000things.com/feed/<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Feed<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Feed<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>There, I Fixed It!<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://feeds.feedburner.com/ThereIFixedIt<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Feed<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Feeds<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div></div>

<p>We&#8217;ll store this information in a basic class:</p>
<div>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> FeedDefinition
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Name <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Url <span style="color: #008000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div></div>

</div>
<h2>Loading the Feed Definitions</h2>
<p>Now that we have our feed definitions stored in an external file and a class defined to store the information, we need to load that data into our application. Before we had the .NET Framework, we would have to read in the file and create our own XML parser in order to load the data. This process has become easier with each version of the .NET Framework.</p>
<p>By utilizing LINQ, the process of obtaining the data from an XML file has become quite easy:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">XDocument feedDefinitions <span style="color: #008000;">=</span> XDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span>Server<span style="color: #008000;">.</span><span style="color: #0000FF;">MapPath</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Feeds.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
var feeds <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">from</span> feed <span style="color: #0600FF; font-weight: bold;">in</span> feedDefinitions<span style="color: #008000;">.</span><span style="color: #0000FF;">Descendants</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Feed&quot;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #0600FF; font-weight: bold;">select</span> <span style="color: #008000;">new</span> FeedDefinition<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
            <span style="color: #008000;">&#123;</span>
                Name <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>feed<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Name&quot;</span><span style="color: #008000;">&#41;</span>,
                Url <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>feed<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Url&quot;</span><span style="color: #008000;">&#41;</span>,
            <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></pre></td></tr></table></div></div>

<p>Using just one line of code (line 1), the entire feed definition file is loaded and parsed for you. The contents are stored in an XDocument object.  Processing this object using LINQ (lines 3-8), we obtain a collection of FeedDefinition objects (IEnumerable&lt;FeedDefinition&gt;) that we will use in our web page.</p>
<h2>Building The Display</h2>
<p>Now that we have our data, we need to be able to display it to the user. We will use a drop-down list control that will allow the user to select a feed for reading. In our ASP.NET content page, we&#8217;ll define the our display as this:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="asp" style="font-family:monospace;"><span style="color: #006600; font-weight: bold;">&lt;</span>div <span style="color: #0000ff; font-weight: bold;">class</span><span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;feedList&quot;</span><span style="color: #006600; font-weight: bold;">&gt;</span>
    Feed<span style="color: #006600; font-weight: bold;">:</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>DropDownList ID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;FeedList&quot;</span>
                      DataValueField<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Url&quot;</span>
                      DataTextField<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Name&quot;</span>
                      runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span>
    <span style="color: #006600; font-weight: bold;">&lt;</span>asp<span style="color: #006600; font-weight: bold;">:</span>Button ID<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;OpenButton&quot;</span>
                Text<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;Open&quot;</span>
                runat<span style="color: #006600; font-weight: bold;">=</span><span style="color: #cc0000;">&quot;server&quot;</span> <span style="color: #006600; font-weight: bold;">/&gt;</span>
<span style="color: #006600; font-weight: bold;">&lt;/</span>div<span style="color: #006600; font-weight: bold;">&gt;</span></pre></td></tr></table></div></div>

<p>The drop-down list box will be bound to our feed data that we read in earlier. However, we have two fields in our data. Using the DropDownList control, we can display the Name property to the user and use the Url property for processing later on.</p>
<h2>Putting It Together</h2>
<p>Now that we have our feed definitions and our display, we just need to put them both together:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">FeedList<span style="color: #008000;">.</span><span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> feeds<span style="color: #008000;">;</span>
FeedList<span style="color: #008000;">.</span><span style="color: #0000FF;">DataBind</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div></div>

<p>This code binds our feed definition collection to the drop-down list control. All we need to do now is put our code into our content page&#8217;s Page_Init method so that it&#8217;ll populate our control the first time our page is loaded.</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">protected</span> <span style="color: #6666cc; font-weight: bold;">void</span> Page_Init<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    XDocument feedDefinitions <span style="color: #008000;">=</span> XDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Load</span><span style="color: #008000;">&#40;</span>Server<span style="color: #008000;">.</span><span style="color: #0000FF;">MapPath</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Feeds.xml&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    var feeds <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">from</span> feed <span style="color: #0600FF; font-weight: bold;">in</span> feedDefinitions<span style="color: #008000;">.</span><span style="color: #0000FF;">Descendants</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Feed&quot;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #0600FF; font-weight: bold;">select</span> <span style="color: #008000;">new</span> FeedDefinition<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
                <span style="color: #008000;">&#123;</span>
                    Name <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>feed<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Name&quot;</span><span style="color: #008000;">&#41;</span>,
                    Url <span style="color: #008000;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#41;</span>feed<span style="color: #008000;">.</span><span style="color: #0000FF;">Element</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Url&quot;</span><span style="color: #008000;">&#41;</span>,
                <span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
    FeedList<span style="color: #008000;">.</span><span style="color: #0000FF;">DataSource</span> <span style="color: #008000;">=</span> feeds<span style="color: #008000;">;</span>
    FeedList<span style="color: #008000;">.</span><span style="color: #0000FF;">DataBind</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div></div>

<p>It&#8217;s important that you load the feed definitions in Page_Init() rather than in Page_Load().  Putting them in Page_Init() will prevent the drop-down list control from getting reinitialized during postback when we implement our Open button in Part 2.</p>
<p>When we load our website, we get our list of feeds neatly displayed in our control:</p>
<div>
<p><a href="http://ke4ktz.org/2011/11/creating-an-rss-reader-using-asp-net-and-linq-part-1/screen-shot-2011-11-09-at-9-54-16-pm/" rel="attachment wp-att-818"><img class="size-medium wp-image-818 alignnone" title="Feed List Display" src="http://ke4ktz.org/wp-content/uploads/2011/11/Screen-Shot-2011-11-09-at-9.54.16-PM-300x159.png" alt="" width="300" height="159" /></a></p>
</div>
<h2>Conclusion</h2>
<p>In this post, we wrote the first half of our simple RSS reader web application. By using the .NET Framework and the LINQ feature, we were able to load a list of RSS feeds into our web page without writing a lot of code.</p>
<p>In part 2, we&#8217;ll show how to get a list of posts from the feed and display them along with other data related to the posts.</p>
<div class="printfriendly align"><a href="http://ke4ktz.org/2011/11/creating-an-rss-reader-using-asp-net-and-linq-part-1/?pfstyle=wp" rel="nofollow" ><img src="//cdn.printfriendly.com/pf-icon-small.gif" alt="Print Friendly"/><span class="printfriendly-text"></span></a></div><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fke4ktz.org%2F2011%2F11%2Fcreating-an-rss-reader-using-asp-net-and-linq-part-1%2F&amp;title=Creating%20an%20RSS%20Reader%20Using%20ASP.NET%20and%20LINQ%20%26%238211%3B%20Part%201" id="wpa2a_2"><img src="http://ke4ktz.org/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://ke4ktz.org/2011/11/creating-an-rss-reader-using-asp-net-and-linq-part-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Why I Hate Computers: Reason 283</title>
		<link>http://ke4ktz.org/2009/08/why-i-hate-computers-reason-283/</link>
		<comments>http://ke4ktz.org/2009/08/why-i-hate-computers-reason-283/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 18:21:22 +0000</pubDate>
		<dc:creator>Glenn</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://ke4ktz.org/?p=409</guid>
		<description><![CDATA[Those that know me hear me say often that &#8220;I hate computers.&#8221; &#160;The first time someone hears me say that, their comment is usually &#8220;But, you make your living with computers!&#8221; &#160;I have to politely tell that it doesn&#8217;t mean I have to like them. I was recently working on an issue discovered by our [...]]]></description>
			<content:encoded><![CDATA[<p>Those that know me hear me say often that &#8220;I hate computers.&#8221; &nbsp;The first time someone hears me say that, their comment is usually &#8220;But, you make your living with computers!&#8221; &nbsp;I have to politely tell that it doesn&#8217;t mean I have to like them.</p>
<p>I was recently working on an issue discovered by our QA tester. &nbsp;We have a feature that allows access to dBase database files. &nbsp;As most programs that provide this access, we utilize the Microsoft Jet Database Engine to perform the heavy lifting. &nbsp;(I know, Jet is on it&#8217;s way out, but for now it still works.)</p>
<p>During my development, I tested this feature many times without any problems. &nbsp;Now, our QA tester opens a problem ticket stating that he cannot see any of the database&#8217;s columns. &nbsp;I attempt to duplicate it but cannot on my development machine, so I turn to one of my test machines.</p>
<p>Using a few different machines, I find that the issue is not the test file or the version of the operating system. &nbsp;What is strange is that I can get it to work on one machine but cannot on another with the same operating system. &nbsp;What&#8217;s worse, is that the error is not throwing an exception so my utility is not displaying anything to the user.</p>
<p>I do manage to finally get the application to throw an exception. &nbsp;What&#8217;s strange is that the exception message is &#8220;Microsoft jet database engine could not find object &#8216;xyz.dbf&#8217;&#8221;. &nbsp;SAY WHAT??? &nbsp;Not possible, the file is RIGHT THERE!!</p>
<p>Next I turn to my favorite tool, the web search. &nbsp;A bit of searching turned up the following interesting text:</p>
<blockquote><p>3011 &#8211; <strong>Microsoft jet database engine could not find object &#8216;xyz.dbf&#8217;&#8230;&nbsp; </strong>Notice the dbf file extension.&nbsp; A dbf file name needs to be in 8.3 DOS name  format.&nbsp;&nbsp; It is quite possible that some machines can open the file with a  longer file name and others it must be in 8.3 DOS name format.</p></blockquote>
<p>Now, isn&#8217;t that interesting? &nbsp;Long file names have been in existence since Windows 95, but yet the jet database engine doesn&#8217;t support them after almost 15 years! &nbsp;</p>
<p>By renaming the file to a DOS 8.3 format name resulted in a successful test on all systems. &nbsp;(Yes, the internal &#8220;short&#8221; name, the one with the &#8216;~&#8217;, also worked.) &nbsp;What is interesting is that one machine worked just fine with the long file name, but the rest didn&#8217;t. &nbsp;If anyone has any input in to why long names work on some systems and not others, I&#8217;d appreciate the input.</p>
<div class="printfriendly align"><a href="http://ke4ktz.org/2009/08/why-i-hate-computers-reason-283/?pfstyle=wp" rel="nofollow" ><img src="//cdn.printfriendly.com/pf-icon-small.gif" alt="Print Friendly"/><span class="printfriendly-text"></span></a></div><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fke4ktz.org%2F2009%2F08%2Fwhy-i-hate-computers-reason-283%2F&amp;title=Why%20I%20Hate%20Computers%3A%20Reason%20283" id="wpa2a_4"><img src="http://ke4ktz.org/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://ke4ktz.org/2009/08/why-i-hate-computers-reason-283/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Implementing Automatic Build Versions using PowerShell and Subversion</title>
		<link>http://ke4ktz.org/2009/03/implementing-automatic-build-versions-using-powershell-and-subversion/</link>
		<comments>http://ke4ktz.org/2009/03/implementing-automatic-build-versions-using-powershell-and-subversion/#comments</comments>
		<pubDate>Wed, 25 Mar 2009 01:17:31 +0000</pubDate>
		<dc:creator>Glenn</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://ke4ktz.org/?p=70</guid>
		<description><![CDATA[Recently I was tasked with implementing an automatic build and versioning system for our .NET development project. As part of that process, I needed to come up with a versioning mechanism to place into the process. Background The system had to be automatic so that any member of the team could implement it as part [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was tasked with implementing an automatic build and versioning system for our .NET development project. As part of that process, I needed to come up with a versioning mechanism to place into the process.</p>
<h1>Background</h1>
<p>The system had to be automatic so that any member of the team could implement it as part of a regular build cycle. Other projects within the company integrate with this project, so distribution builds are a weekly occurrence.</p>
<p>The build environment has been kept simple: Visual Studio 2008 and Subversion.  Integration between the two was accomplished by using the ankhsvn Visual Studio plugin.  To enable Subversion maintenance from outside of Visual Studio, the TortoiseSVN Explorer shell extension is used.</p>
<h1>Build Versions</h1>
<p>Due to the numerous builds that occur, it is necessary to integrate a build number into the application version information.  The AssemblyVersion and AssemblyFileVersion attributes allow for a four level versioning system. Having four levels would allow us to simply add the build number to the end of our existing standard three-level version system (Major.Minor.ServicePack).</p>
<p>The only issue was how to come up with a build number that would be meaningful.  Visual Studio has an option to replace the third and fourth levels with values that are hacks of the date and time of the build. However, this option resulted in the removal of the Service Pack number from our version number, which was not acceptable.</p>
<p>Back in Visual Basic 6, there was a build option that auto-incremented the last value in the version each time the output was built. That feature was not available in the C/C++ versions of Visual Studio and was not brought forward to the .NET versions. Although an auto-incrementing value allows for determining which build is newer, it provides for not much else.</p>
<h1>The Plan</h1>
<p>During my research, I came across a blog post where the author was utilizing the latest subversion revision as the build number. This allowed for a build to be directly connected to point in the subversion repository. However, this author&#8217;s instructions were utilizing MSBuild, which we were not.</p>
<p>The plan was to implement the following versioning scheme: Major.Minor.ServicePack.Build. The major, minor and service pack values would be specified manually and the build value would be generated automatically as part of the build process.</p>
<h1>The Tools</h1>
<p>Coming up with the versioning scheme was only half of the work.  The other half, and arguably the hardest half, was to come up with the actual implementation.  The challenge was to determmine what tools would be needed to accomplish the task.  The tools would have to be lightweight and easy to use.</p>
<p>I decided to utilize Windows PowerShell to accomplish the task.  Not only could it be used for automating the build process, but it could also be used to perform automated tests on our .NET classes.  PowerShell utilizes the .NET framework classes, so any task that we can accomplish in a .NET application could be accomplished in a PowerShell script.</p>
<p>To allow access to the Subversion repository, a command-line SVN client would be needed.  I chose the SlikSVN client available from the Subversion client page.  It comes with a Microsoft MSI installation package and was easily distributed to other team members.</p>
<h1>The Setup</h1>
<p>Before diving into the PowerShell script, some work had to be done to make the job easier and more efficient.  Each project has its own AssemblyInfo.cs file.  Since our procedure would only update the build number, this would mean that each AssemblyInfo.cs file would have to be updated with the major, minor and service pack numbers.  As the number of projects grows, this task becomes more prone to error.</p>
<p>To solve this issue, the AssemblyInfo.cs file would be split into two files.  The first file, AssemblyVersionInfo.cs, would contain the company, product, copyright and version information.  The second, AssemblyInfo.cs, would contain the remaining attributes.  AssemblyVersionInfo.cs would be a single file that would be shared by all projects, whereas AssemblyInfo.cs would be located in each project&#8217;s directory.</p>
<p>By placing all version information into a single file, this reduces the modifications needed by the development team when a version changes.  It also allows the PowerShell script to simply modify a single file rather than having to search through each project directory.</p>
<h1>Getting the Latest Revision Number</h1>
<p>The first step in the process is to obtain the value of the latest Subversion revision.  Alejandro Espinoza wrote a little PowerShell script that accomplished this task.  With a bit of simplification for my use, here&#8217;s the result:</p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> GetLatestRevisionNumber<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #800080;">$revLinePattern</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;(.*?)(Revision: )(\d+\.?\d*)(.*?)&quot;</span>;
&nbsp;
   <span style="color: #800080;">$result</span> <span style="color: pink;">=</span> svn info .
&nbsp;
   <span style="color: #800080;">$revision</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
&nbsp;
   <span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span><span style="color: #008080;">regex</span><span style="color: #000000;">&#93;</span>::Match<span style="color: #000000;">&#40;</span><span style="color: #800080;">$result</span><span style="color: pink;">,</span> <span style="color: #800080;">$revLinePattern</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
      <span style="color: #800080;">$matches</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">regex</span><span style="color: #000000;">&#93;</span>::Split<span style="color: #000000;">&#40;</span><span style="color: #800080;">$result</span><span style="color: pink;">,</span><span style="color: #800080;">$revLinePattern</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #800080;">$revision</span> <span style="color: pink;">=</span> <span style="color: #800080;">$matches</span><span style="color: #000000;">&#91;</span><span style="color: #804000;">3</span><span style="color: #000000;">&#93;</span>;
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #0000FF;">return</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$revision</span>;
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div></div>

<p>This PowerShell function calls &#8216;svn info&#8217; to get the Subversion repository information for the current directory.  Then it locates the line with &#8216;Revision&#8217;, pulls out the value and returns it to the caller.</p>
<h1>Updating AssemblyVersionInfo.cs</h1>
<p>Now that the latest revision value is obtained, we need to take that value and update the AssemblyVersionInfo.cs file.  However, there are two issues that need to be addressed when doing so:</p>
<ol>
<li>If the build number in AssemblyVersionInfo.cs is the same, the file should not be updated.</li>
<li>After the AssemblyVersionInfo.cs is updated, the new copy will be loaded back into the Subversion respository at the next revision level.</li>
</ol>
<p>To address issue #1, the script will need to compare the updated value with the value that exists in the file.  The file will only be updated if the value has changed.</p>
<p>To address issue #2, the revision level obtained above will be incremented before writing it to the file.  Otherwise, the file will be updated each time the script is run.  By incrementing the value, the value written to the file will match the level when the file is copied back into the respository after the update.</p>
<p>The PowerShell script to perform the update is quite simple:</p>
<p><span style="font-size: 26px; font-weight: bold;"> </span></p>

<div class="my_syntax_box"><span class="my_syntax_selecall"><a href="javascript:;" onclick="selectCode(this); return false;">Selec All</a> </span><span class="my_syntax_Bar">Code:</span><div class="my_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="powershell" style="font-family:monospace;"><span style="color: #0000FF;">function</span> UpdateAssemblyVersion<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
   <span style="color: #800080;">$currRevision</span> <span style="color: pink;">=</span> GetLatestRevisionNumber;
   <span style="color: #800080;">$nextRevision</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">int</span><span style="color: #000000;">&#93;</span><span style="color: #800080;">$currRevision</span> <span style="color: pink;">+</span> <span style="color: #804000;">1</span>;
   <span style="color: #800080;">$versionPattern</span> <span style="color: pink;">=</span> <span style="color: #800000;">'(?\d+)\.(?\d+)\.(?\d+)\.\d+'</span>;
   <span style="color: #800080;">$hasChanged</span> <span style="color: pink;">=</span> <span style="color: #804000;">0</span>;
&nbsp;
   <span style="color: #800080;">$assemblyFileData</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-Content</span> Common\AssemblyVersionInfo.cs;
&nbsp;
   <span style="color: #0000FF;">for</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$line</span><span style="color: pink;">=</span><span style="color: #804000;">0</span>; <span style="color: #800080;">$line</span> <span style="color: #FF0000;">-lt</span> <span style="color: #800080;">$assemblyFileData</span>.Length; <span style="color: #800080;">$line</span><span style="color: pink;">++</span><span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #800080;">$newLine</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">regex</span><span style="color: #000000;">&#93;</span>::Replace<span style="color: #000000;">&#40;</span><span style="color: #800080;">$assemblyFileData</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$line</span><span style="color: #000000;">&#93;</span><span style="color: pink;">,</span> <span style="color: #800080;">$versionPattern</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`$</span>{major}.<span style="color: #008080; font-weight: bold;">`$</span>{minor}.<span style="color: #008080; font-weight: bold;">`$</span>{sp}.&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$currRevision</span><span style="color: #000000;">&#41;</span>;
      <span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$newLine</span>.CompareTo<span style="color: #000000;">&#40;</span><span style="color: #800080;">$assemblyFileData</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$line</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span> <span style="color: #FF0000;">-ne</span> <span style="color: #804000;">0</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
         <span style="color: #800080;">$hasChanged</span> <span style="color: pink;">=</span> <span style="color: #804000;">1</span>;
         <span style="color: #800080;">$newLine</span> <span style="color: pink;">=</span> <span style="color: #000000;">&#91;</span><span style="color: #008080;">regex</span><span style="color: #000000;">&#93;</span>::Replace<span style="color: #000000;">&#40;</span><span style="color: #800080;">$assemblyFileData</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$line</span><span style="color: #000000;">&#93;</span><span style="color: pink;">,</span> <span style="color: #800080;">$versionPattern</span><span style="color: pink;">,</span> <span style="color: #800000;">&quot;<span style="color: #008080; font-weight: bold;">`$</span>{major}.<span style="color: #008080; font-weight: bold;">`$</span>{minor}.<span style="color: #008080; font-weight: bold;">`$</span>{sp}.&quot;</span> <span style="color: pink;">+</span> <span style="color: #800080;">$nextRevision</span><span style="color: #000000;">&#41;</span>;
         <span style="color: #800080;">$currRevision</span> <span style="color: pink;">=</span> <span style="color: #800080;">$nextRevision</span>;
         <span style="color: #800080;">$assemblyFileData</span><span style="color: #000000;">&#91;</span><span style="color: #800080;">$line</span><span style="color: #000000;">&#93;</span> <span style="color: pink;">=</span> <span style="color: #800080;">$newLine</span>;
      <span style="color: #000000;">&#125;</span>
   <span style="color: #000000;">&#125;</span>
&nbsp;
   <span style="color: #0000FF;">if</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$hasChanged</span> <span style="color: #FF0000;">-eq</span> <span style="color: #804000;">1</span><span style="color: #000000;">&#41;</span>
   <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-weight: bold;">write-host</span> <span style="color: #800000;">&quot;Updating AssemblyVersionInfo.cs to version &quot;</span> <span style="color: #800080;">$nextRevision</span> <span style="color: #800000;">&quot;.&quot;</span>;
      <span style="color: #008080; font-weight: bold;">Set-Content</span> Common\AssemblyVersionInfo.cs <span style="color: #800080;">$assemblyFileData</span>;
      svn commit Common <span style="color: pink;">-</span>m `<span style="color: #800000;">&quot;Version change for build.<span style="color: #008080; font-weight: bold;">`&quot;</span>
   }
}</span></pre></td></tr></table></div></div>

<h1>Conclusion</h1>
<p>These PowerShell functions are simply two steps in the entire build automation process.  When placed between a Subversion update and a Visual Studio compile, they provide an automated method for integrating meaningful build numbers into your .NET assemblies.</p>
<div class="printfriendly align"><a href="http://ke4ktz.org/2009/03/implementing-automatic-build-versions-using-powershell-and-subversion/?pfstyle=wp" rel="nofollow" ><img src="//cdn.printfriendly.com/pf-icon-small.gif" alt="Print Friendly"/><span class="printfriendly-text"></span></a></div><p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fke4ktz.org%2F2009%2F03%2Fimplementing-automatic-build-versions-using-powershell-and-subversion%2F&amp;title=Implementing%20Automatic%20Build%20Versions%20using%20PowerShell%20and%20Subversion" id="wpa2a_6"><img src="http://ke4ktz.org/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://ke4ktz.org/2009/03/implementing-automatic-build-versions-using-powershell-and-subversion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

