<?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>PeterKellner.net &#187; Visual Studio 2010</title>
	<atom:link href="http://peterkellner.net/category/visual-studio-2010/feed/" rel="self" type="application/rss+xml" />
	<link>http://peterkellner.net</link>
	<description>Microsoft Focused, JavaScript,HTML5 (ExtJS, SenchaTouch &#38; Windows 8 Metro)</description>
	<lastBuildDate>Fri, 11 May 2012 16:43:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Microsoft&#8217;s ASP.NET MVC Lite, Building a TextReformatter With JsonResult</title>
		<link>http://peterkellner.net/2012/04/10/microsofts-asp-net-mvc-lite-building-a-textreformatter-with-jsonresult/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=microsofts-asp-net-mvc-lite-building-a-textreformatter-with-jsonresult</link>
		<comments>http://peterkellner.net/2012/04/10/microsofts-asp-net-mvc-lite-building-a-textreformatter-with-jsonresult/#comments</comments>
		<pubDate>Tue, 10 Apr 2012 19:40:18 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[ASP.NET 3.5]]></category>
		<category><![CDATA[ASP.NET 4.0]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[Visual Studio 2011 Developer Preview]]></category>

		<guid isPermaLink="false">http://peterkellner.net/?p=1931</guid>
		<description><![CDATA[&#160;
Introduction
&#160;
People often ask me if I use MVC or WebForms for my Microsoft ASP.NET projects.&#160; I always tell them MVC, but I don’t use it in the classic way that most others do.&#160; That is to say, I emit no html from my views, and for that matter really don’t even use views (with the [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<h2>Introduction</h2>
<p>&#160;</p>
<p>People often ask me if I use <a href="http://www.asp.net/mvc">MVC</a> or <a href="http://www.asp.net/web-forms">WebForms</a> for my Microsoft <a href="http://www.asp.net/">ASP.NET</a> projects.&#160; I always tell them <a href="http://www.asp.net/mvc">MVC</a>, but I don’t use it in the classic way that most others do.&#160; That is to say, I emit no html from my views, and for that matter really don’t even use views (with the exception of one view which is essentially my entire app.&#160;&#160; People often refer to this as a SPA or Single-Page-Application. What this basically means is that my entire view is built with JavaScript (in my case <a href="'">Sencha’s</a> <a href="http://www.sencha.com/products/extjs/">ExtJS</a>) and the only interaction with the server is to simply bring down <a href="http://json.org/">Json</a> results.</p>
<p>In this post, I’m going to build a simple app following this principle. It’s purpose will be to take a big pile of text, chop it up into words, then output it in some c# friendly manner.&#160; My motivation for doing this is I happen to need that functionality right now and instead of creating a little <a href="http://en.wikipedia.org/wiki/AWK">awk</a> script to do it, I’m going to right a web site!</p>
<p>&#160;</p>
<p>So, Here is what we will build.&#160; A handy formatter! (and you can run it at:&#160;&#160; <a href="http://reformatter.peterkellner.net/">http://reformatter.peterkellner.net/</a> )</p>
<p>&#160;</p>
<p><a href="http://reformatter.peterkellner.net/app.html"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/04/image4.png" width="510" height="438" /></a></p>
<p><a href="http://reformatter.peterkellner.net/app.html"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/04/image5.png" width="511" height="444" /></a></p>
<p>&#160;</p>
<p>&#160;</p>
<h2>Part I, The Visual Studio Project</h2>
<p>&#160;</p>
<p>Let’s first create the Visual Studio project.&#160; My development these days is actually using the latest Windows 8 Consumer build with the beta of Visual Studio 2011 running on it.&#160; The nice thing about this is because Microsoft added “Round Tripping” to VS2011, I can now use VS2011 to create the project and you, my happy readers, can download the project and open it in Visual Studio 2010.&#160; This has been long in coming and Microsoft has finally done it for the newest version of Visual Studio.&#160; Thank you Microsoft!</p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/04/image.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/04/image_thumb.png" width="438" height="271" /></a></p>
<p>&#160;</p>
<p>And with an Internet Application with Razor (though I really don’t need all that extra stuff, it does not hurt)</p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/04/image1.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/04/image_thumb1.png" width="385" height="353" /></a></p>
<p>&#160;</p>
<p>The structure created is as as shown below with a Controller and Views folder and all the proper routing setup.</p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/04/image2.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/04/image_thumb2.png" width="218" height="340" /></a></p>
<p>&#160;</p>
<p>Because I want to make a very simple MVC app that only emits Json, Let’s create our own empty controller called ReformatterController.&#160; We do that by right clicking on the Controller folder and pressing “Add Controller”, then “Add Empty Controller”.</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/04/image3.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/04/image_thumb3.png" width="352" height="256" /></a></p>
<p>&#160;</p>
<p>Now, let’s create a very very simple controller with just one method that returns a JSON Result.&#160; I’m returning the data in a friendly format to&#160; Sencha’s ExtJS (or SenchaTouch for that matter).&#160; <a href="http://www.webappers.com/2011/02/16/how-to-build-single-page-applications-with-jquery/">JQuery</a> would be similar.&#160; Here is our new controller class (I’m sure my little tokenizer, line maker could be improved, but I’m just doing this to show some real work.</p>
<pre class="csharpcode"><span class="kwrd">using</span> System;
<span class="kwrd">using</span> System.Collections.Generic;
<span class="kwrd">using</span> System.Linq;
<span class="kwrd">using</span> System.Text;
<span class="kwrd">using</span> System.Web;
<span class="kwrd">using</span> System.Web.Mvc;

<span class="kwrd">namespace</span> MvcLiteApp.Controllers
{
    <span class="kwrd">public</span> <span class="kwrd">class</span> ReformatterController : Controller
    {

        <span class="kwrd">public</span> JsonResult ConvertTextToFriendlyText(<span class="kwrd">string</span> inputText)
        {
            <span class="kwrd">const</span> <span class="kwrd">int</span> maxCharactersInLine = 30;
            <span class="kwrd">bool</span> success;
            <span class="kwrd">string</span> messageReturn;
            var outputDataList = <span class="kwrd">new</span> List&lt;<span class="kwrd">string</span>&gt;();

            <span class="kwrd">try</span>
            {
                var words = inputText.Split(<span class="str">' '</span>).ToList();
                var newLine = <span class="kwrd">new</span> StringBuilder();
                <span class="kwrd">foreach</span> (var word <span class="kwrd">in</span> words)
                {
                    <span class="kwrd">if</span> (newLine.Length + word.Length &lt; maxCharactersInLine &amp;&amp;
                        word.Length &lt; maxCharactersInLine)
                    {
                        newLine.Append(<span class="str">&quot; &quot;</span>);
                        newLine.Append(word);
                    }
                    <span class="kwrd">else</span>
                    {
                        outputDataList.Add(newLine.ToString().Trim());
                        newLine = <span class="kwrd">new</span> StringBuilder();
                        newLine.Append(word);
                    }
                }
                success = <span class="kwrd">true</span>;
                messageReturn = <span class="str">&quot;&quot;</span>;
            }
            <span class="kwrd">catch</span> (Exception e)
            {
                messageReturn = e.ToString();
                success = <span class="kwrd">false</span>;
            }

            <span class="kwrd">return</span> Json(<span class="kwrd">new</span>
            {
                Success = success,
                Data = outputDataList,
                message = messageReturn
            }, JsonRequestBehavior.AllowGet);
        }

    }
}</pre>
<p>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Now, let’s feed it a URL and see what happens.&#160; Here is a sample URL:</p>
<p><a title="http://localhost:3600/Reformatter/ConvertTextToFriendlyText?inputText=Romeo%20Montague%20and%20Juliet%20Capulet%20meet%20and%20fall%20in%20love%20in%20Shakespeare&#39;s%20lyrical%20tale%20of%20%22star-cross&#39;d%22%20lovers." href="http://localhost:3600/Reformatter/ConvertTextToFriendlyText?inputText=Romeo%20Montague%20and%20Juliet%20Capulet%20meet%20and%20fall%20in%20love%20in%20Shakespeare's%20lyrical%20tale%20of%20%22star-cross'd%22%20lovers.">http://localhost:3600/Reformatter/ConvertTextToFriendlyText?inputText=Romeo%20Montague%20and%20Juliet%20Capulet%20meet%20and%20fall%20in%20love%20in%20Shakespeare&#8217;s%20lyrical%20tale%20of%20%22star-cross&#8217;d%22%20lovers.</a></p>
<p>I actually pasted the sentence in to the the browser’s URL line and it added the nice encoded spaces for me.&#160; The output is as follows:</p>
<p>{&quot;Success&quot;:true,&quot;Data&quot;:[&quot;Romeo Montague and Juliet&quot;,&quot;Capulet meet and fall in love&quot;,&quot;in Shakespeare\u0027s lyrical tale&quot;],&quot;message&quot;:&quot;&quot;}</p>
<p>Notice that it created the nice lines formatted as a JavaScript array!</p>
<p>Next step, let’s make a real JavaScript program out of this.</p>
<p>&#160;</p>
<h2>Part 2, Our SPA (Single Page App) That Does Real Work</h2>
<p>&#160;</p>
<p>I’m only going to explain here the part of the Sencha Program that actually consumes the data but first, let’s add a little “properness” to our controller so it responds only to POST.&#160; That is, we add an attribute to the top of the method and we change the behavior attribute on the bottom to say to not allow get.</p>
<pre class="csharpcode">        [HttpPost]
        <span class="kwrd">public</span> JsonResult ConvertTextToFriendlyText(<span class="kwrd">string</span> inputText)
        {
            .......
            <span class="kwrd">return</span> Json(<span class="kwrd">new</span>
            {
                Success = success,
                Data = outputDataList,
                message = messageReturn
            }, JsonRequestBehavior.DenyGet);
        }</pre>
<p>And, the JavaScript in the SPA is very straight forward and is here:</p>
<pre class="csharpcode"><span class="kwrd">var</span> beforeTextAreaComponent = Ext.getCmp(<span class="str">'BeforeFormatTextAreaId'</span>);
textBefore = beforeTextAreaComponent.getValue();

<span class="rem">// how to not pollute global name space here?</span>
<span class="kwrd">var</span> stringbuildervalue = Ext.getCmp(<span class="str">'stringbuilderid'</span>).getValue();
<span class="kwrd">var</span> javascriptarrayvalue = Ext.getCmp(<span class="str">'javascriptarrayid'</span>).getValue();
<span class="kwrd">var</span> noformattingvalue = Ext.getCmp(<span class="str">'noformattingid'</span>).getValue();
<span class="kwrd">var</span> outputwidthvalue = Ext.getCmp(<span class="str">'outputwidthid'</span>).getValue();

Ext.Ajax.request({
    url: <span class="str">'ReFormatter/ConvertTextToFriendlyText'</span>,
    <span class="kwrd">params</span>: {
        inputText: textBefore,
        stringbuilder: stringbuildervalue,
        javascriptarray: javascriptarrayvalue,
        noformatting: noformattingvalue,
        outputwidth: outputwidthvalue
    },
    success: <span class="kwrd">function</span>(response){
        <span class="kwrd">var</span> localData = Ext.JSON.decode(response.responseText).Data;
        <span class="kwrd">var</span> numberRows = localData.length;
        <span class="kwrd">var</span> newData = <span class="str">''</span>;
        <span class="kwrd">for</span> (<span class="kwrd">var</span> i=0;i&lt;numberRows;i++) {
            newData += localData[i] + <span class="str">'\n'</span>;
        }

        <span class="kwrd">var</span> afterTextAreaComponent = Ext.getCmp(<span class="str">'AfterFormatTextAreaId'</span>);
        afterTextAreaComponent.setValue(newData);

    },
    failure: <span class="kwrd">function</span>(error) {
        alert(<span class="str">'error'</span>);
    }

});</pre>
<p>Now, in reality, I’ve added some useful stuff to the controller (and put the project here for your review, comments and suggestions).</p>
<div id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:279ce901-a751-4b1a-8603-ebda182698cc" class="wlWriterEditableSmartContent" style="float: none; padding-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px">
<p>Visual Studio Project <a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/04/TextReformatter.zip" target="_blank">TextReformatter.zip</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2012/04/10/microsofts-asp-net-mvc-lite-building-a-textreformatter-with-jsonresult/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a Simple Window Service Application in Visual Studio 2010 That Runs, Sleeps and Stops</title>
		<link>http://peterkellner.net/2012/03/26/building-a-simple-window-service-application-in-visual-studio-2010-that-runs-sleeps-and-stops/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-simple-window-service-application-in-visual-studio-2010-that-runs-sleeps-and-stops</link>
		<comments>http://peterkellner.net/2012/03/26/building-a-simple-window-service-application-in-visual-studio-2010-that-runs-sleeps-and-stops/#comments</comments>
		<pubDate>Mon, 26 Mar 2012 18:24:46 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[VS2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2012/03/26/building-a-simple-window-service-application-in-visual-studio-2010-that-runs-sleeps-and-stops/</guid>
		<description><![CDATA[Introduction
The goal is to create a simple Service in Windows 7 (or other similar OS’s) with Visual Studio 2010 that simply starts, sleeps for 15 seconds, then stops.&#160; I realize this is not that useful, but basically, it covers the case of building a service that actually does something and when finished stops.&#160; In my [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>The goal is to create a simple <a href="http://en.wikipedia.org/wiki/Windows_service">Service</a> in <a href="http://windows.microsoft.com/en-us/windows7/products/home">Windows 7</a> (or other similar OS’s) with <a href="http://www.microsoft.com/visualstudio/en-us">Visual Studio 2010</a> that simply starts, sleeps for 15 seconds, then stops.&#160; I realize this is not that useful, but basically, it covers the case of building a service that actually does something and when finished stops.&#160; In my personal case, my thread has a while(true) loop which keeps looking for new work and only completes when certain conditions are met (like a fatal error that is non recoverable).</p>
<p>Microsoft has given us a pretty good set of docs and walk through.&#160; You can find them all here:&#160; <a href="http://msdn.microsoft.com/en-us/library/y817hyb6.aspx">http://msdn.microsoft.com/en-us/library/y817hyb6.aspx</a></p>
<p>&#160;</p>
<h2>The Steps</h2>
<p>Create a new Visual Studio Project of type <a href="http://en.wikipedia.org/wiki/Windows_service">“Windows Service”</a></p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image16.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image_thumb15.png" width="383" height="299" /></a></p>
<p>Once the project is created, make sure you have the design surface up for Service1 [Design] and right click on it and say “Add Installer”.</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image17.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image_thumb16.png" width="458" height="189" /></a></p>
<p>Now, you’ll get to “gear” icons that you can set properties for.&#160; They are “serviceProcessInstaller1” and “serviceInstaller1” as follows:</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image18.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image_thumb17.png" width="410" height="283" /></a></p>
<p>Select “serviceProcessInstaller1” and in it’s properties window change the account to “LocalSystem”.&#160; You should have a screen that looks like the following:</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image19.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image_thumb18.png" width="329" height="227" /></a></p>
<p>Then, on the other gear “serviceInstaller”, change the nanes as follows.&#160; I’m naming my service “MyDoNothingService”.</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image20.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image_thumb19.png" width="415" height="338" /></a></p>
<p>Now, rebuild your project and you’ll get an exe file in your /bin/Debug folder.&#160; Open that folder with the Visual Studio Command Prompt (elevated as admin).&#160; To install the service, enter the command:</p>
<p><strong>InstallUtil WindowsServiceCreateSample.exe</strong></p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image21.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image_thumb20.png" width="603" height="79" /></a></p>
<p>You should get some messages that end with “The Commit phase completed successfully”, then “The Transaction install has completed”.</p>
<p>Now, you should see that you have the service in your services panel (you can get there by going to control panel / Administrative / Services or simple run “services” from your start button.</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image22.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image_thumb21.png" width="390" height="142" /></a></p>
<p>(sorry for the rename here to MyServiceTest, but I got distracted and had to come back and rename the project)</p>
<p>So, now we want to add something to this project.&#160; Let’s just add to the OnStart event of Service1.cs something that starts a thread that sleeps for 15 seconds, then stops the thread.&#160; Very simple code as follows:</p>
<pre class="csharpcode">  <span class="kwrd">protected</span> <span class="kwrd">override</span> <span class="kwrd">void</span> OnStart(<span class="kwrd">string</span>[] args)
        {
            <span class="kwrd">new</span> Thread(()
                      =&gt;
            {
                Thread.Sleep(15000); <span class="rem">// sleep 15 seconds</span>
                <span class="kwrd">this</span>.Stop();
            }).Start();
        }</pre>
<p>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Now, before we can install the service, we first need to uninstall it.&#160; Very easy, just add a /u to the end of your install command.</p>
<p><strong>InstallUtil WindowsServiceCreateSample.exe /u</strong></p>
<p>You should be greeted with “The uninstall has completed.”.</p>
<p>Now, when you try and reinstall, sadly, you might get this error: “<strong>Error 1001. The specified service has been marked for deletion”.&#160; </strong>If you do, thanks to a post by <a href="http://www.laxdal.org/node/30">Mr. Laxdel</a>, all you have to do is exit your services dialog and restart the service.</p>
<p>Assuming you left the default autoLog set to true as follows:</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image23.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image_thumb22.png" width="436" height="272" /></a></p>
<p>In your event viewer, you should see you service starting and stopping as follows:</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image24.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image_thumb23.png" width="593" height="193" /></a></p>
<p>&#160;</p>
<p>&#160;</p>
<h2>Conclusions</h2>
<p>That was pretty straight forward.&#160; Hopefully, you can follow along and generate the same thing yourself.&#160; Here is the source code attached for a visual studio 2010 project if for some reason you have trouble recreating it yourself.&#160; HTH’s!</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:2e502815-6df7-416f-bba2-da664c6304ef" class="wlWriterEditableSmartContent">
<p>Visual Studio 2010 Project <a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/WindowsServiceCreateSample.zip" target="_blank">Project</a></p>
</div>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2012/03/26/building-a-simple-window-service-application-in-visual-studio-2010-that-runs-sleeps-and-stops/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Test First Development Walk Through with Visual Studio 2011 Preview</title>
		<link>http://peterkellner.net/2012/03/18/test-first-development-walk-through-with-visual-studio-2011-preview/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=test-first-development-walk-through-with-visual-studio-2011-preview</link>
		<comments>http://peterkellner.net/2012/03/18/test-first-development-walk-through-with-visual-studio-2011-preview/#comments</comments>
		<pubDate>Sun, 18 Mar 2012 23:14:10 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[Visual Studio 2011 Developer Preview]]></category>
		<category><![CDATA[VSTS]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2012/03/18/test-first-development-walk-through-with-visual-studio-2011-preview/</guid>
		<description><![CDATA[Introduction
Many of us know we should be using test first development, however it is hard to break old habits.&#160; I have to admit, I started to solve this particular problem I’m going to use an an example first without test first, then realized what a pickle I was going to be in proving to myself [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>
<p>Many of us know we should be using test first development, however it is hard to break old habits.&#160; I have to admit, I started to solve this particular problem I’m going to use an an example first without test first, then realized what a pickle I was going to be in proving to myself it worked.&#160; So, I thought, why not blog my experience as I do it.&#160; Using <a href="http://www.microsoft.com/visualstudio/en-us">Visual Studio</a>&#160;<a href="http://msdn.microsoft.com/en-us/library/ms379625(v=vs.80).aspx">unit testing</a> makes this pretty easy.</p>
<p>&#160;</p>
<h2>The Problem</h2>
<p>I’m currently building a multithreaded email processor and part of that process is I have to figure out, for any given use whether they are supposed to have there email server checked.&#160; So, the way I look at it, I need a method that takes in the following parameter.</p>
<ol>
<ol>
<li><strong>Now</strong> – This is actually todays date and time in UTC, but since this method need to be tested, I’m going to pass in now rather just use it directly. </li>
<li><strong>LastActivityOfUserDateTime</strong> – This is the last time we saw any activity for this user. </li>
<li><strong>LastEmailSession</strong> – This is when the email session was last run (server check).</li>
<li><strong>DefaultSecondsForLastActivityTreshold</strong> – This is used to determine weather the user last activity makes that user considered active or inactive.&#160; That is, say this is 60 seconds.&#160; Then, if the user has been active in the last 60 seconds, then this user is considered active. </li>
<li><strong>DefaultSecondsBetweenEmailRetryOutsideThreshold</strong> – If the user is<u> not active</u> (as defined by DefaultSecondsForLastActivityTreshold) then this is the number of seconds we should wait before we should try and contact the users email server again. </li>
<li><strong>DefaultSecondsBetweenEmailRetryInsideThreshold</strong> – If the user is <u>active</u> (as defined by DefaultSecondsForLastActivityTreshold) then this is the number of seconds we should wait before we should try and contact the users email server again. </li>
</ol>
</ol>
<p>Given these 4 parameters, we should write a method that returns a Boolean indicating weather the mail server needs to be rechecked for a given user.</p>
<p>&#160;</p>
<h2>The Method To Test</h2>
<p>So, let’s write a method signature.&#160; We will use this in our real project, then write some tests to see if it works correctly.&#160; We have not written the actual method yet, we are just defining what it does, then first, very important, writing the tests to prove it will work.&#160; This way, as we add all the corner cases, we can keep making sure the original cases we programmed have not broken.</p>
<p>So, here is how I see the class definition:</p>
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">bool</span> IsUserReadyToContactEmailServer(DateTime currentDateTime,
            DateTime lastActivityOfUserDateTime,
            DateTime lastEmailSession,
            <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold,
            <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryOutsideThreshold,
            <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryInsideThreshold)
        {
            <span class="kwrd">bool</span> activeUser = IsUserCurrentlyActive(currentDateTime, lastActivityOfUserDateTime,
                                                      defaultSecondsForLastActivityTreshold);
            <span class="kwrd">int</span> thresholdForRetrySeconds =
                activeUser
                    ? defaultSecondsBetweenEmailRetryInsideThreshold
                    : defaultSecondsBetweenEmailRetryOutsideThreshold;

            <span class="rem">// this has to be a double because could be huge number if last email send very long ago</span>
            <span class="kwrd">double</span> timeSinceLastEmailSessionSeconds =
                currentDateTime.Subtract(lastEmailSession).TotalSeconds;

           <span class="kwrd">return</span> timeSinceLastEmailSessionSeconds &gt; thresholdForRetrySeconds;
        }</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<h2>Creating a Test Project In Visual Studio 2011 Preview</h2>
<p>Now that we have our method we want to test, let’s create a test project.&#160; This is very straight forward.&#160; We first just say “File/New Project” then choose “Unit Test Project”.</p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image11.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image_thumb11.png" width="432" height="255" /></a></p>
<p>&#160;</p>
<p>I’m going to simply create a method called TestMethodIsUserReadyToContactEmailServer().</p>
<pre class="csharpcode"><span class="kwrd">using</span> System;
<span class="kwrd">using</span> Microsoft.VisualStudio.TestTools.UnitTesting;

<span class="kwrd">namespace</span> AEWeb.Tests
{
    [TestClass]
    <span class="kwrd">public</span> <span class="kwrd">class</span> UnitTest1
    {
        [TestMethod]
        <span class="kwrd">public</span> <span class="kwrd">void</span> TestMethodIsUserReadyToContactEmailServer()
        {
        }
    }
}</pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Now, we need to add some guts.&#160; There is an automated way to create the actual test stubs, but at the moment I can’t seem to find it.&#160; In this case, we really just need to test a bunch of corner cases so let’s code the tests up. Below are the tests I’ve come up with including comments that make them self explanatory.</p>
<p>So, as I’m creating my tests, I realize I need to create an extra class to help this one.&#160; That is specifically, I need to add a method that simply determines if the user is currently active.&#160; I’m going to call that IsCurrentlyActive and have it take three paramaters.&#160; currentDateTime, DefaultSecondsForLastActivityThreshold and usersLastActivityDate.&#160; I realized this because solving the full problem of figuring out if the user is ready to sync email is to complex in one step.</p>
<p>If I were developing this without unit tests, I would have figured the same thing out, but then as I built it, I would not be verifying it’s correctness with testing.</p>
<h2>Creating Stub Classes</h2>
<p>So, my new class I want to test will be this:</p>
<pre class="csharpcode"> <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// This really just a supporting method for the above IsUserReadyToContactEmailServer call</span>
        <span class="rem">/// &lt;/summary&gt;</span>
        <span class="rem">/// &lt;param name=&quot;currentDateTime&quot;&gt;Current datetime (for testing can be anything)&lt;/param&gt;</span>
        <span class="rem">/// &lt;param name=&quot;usersLastActivityDate&quot;&gt;time user was last seen &lt;/param&gt;</span>
        <span class="rem">/// &lt;param name=&quot;defaultSecondsForLastActivityTreshold&quot;&gt;our definition of what makes a user currently active&lt;/param&gt;</span>
        <span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span class="kwrd">public</span> <span class="kwrd">bool</span> IsUserCurrentlyActive(DateTime currentDateTime,
            DateTime usersLastActivityDate,
            <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold
            )
        {

            <span class="kwrd">return</span> <span class="kwrd">true</span>;
        }</pre>
<p>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
</p>
<h2>The Unit Tests Themselves</h2>
<p>So, now is time to actually write the tests.&#160; I’m not going to explain here all the mechanics of testing.&#160; I assume you understand the basics are you right calls to methods, then make assertions for correctness.&#160; If the assertion is wrong, then print that message and of course the test fails.</p>
<p>So, Here are my test cases for just the helper method (IsUserCurrentlyActive).</p>
<pre class="csharpcode"><span class="kwrd">using</span> System;
<span class="kwrd">using</span> AELib;
<span class="kwrd">using</span> Microsoft.VisualStudio.TestTools.UnitTesting;

<span class="kwrd">namespace</span> AEWeb.Tests
{
    [TestClass]
    <span class="kwrd">public</span> <span class="kwrd">class</span> UnitTestEmailReadyCheck
    {

        [TestMethod]
        <span class="kwrd">public</span> <span class="kwrd">void</span> UnitTestRecentActivity()
        {
            {
                <span class="rem">// a long time ago</span>
                var currentDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 12, 0, 0); <span class="rem">// assume current time is 3/1/2012 at noon</span>
                var lastActivityOfUserDateTime = <span class="kwrd">new</span> DateTime(2012, 2, 1, 12, 0, 0);
                <span class="rem">// last activity was a month ago, 2/1/2012 at noon</span>
                <span class="rem">// threshhold for calling a user active</span>
                <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold = 60*15; <span class="rem">// let's call this 15 minute threshhold</span>
                <span class="kwrd">bool</span> isActive = <span class="kwrd">new</span> MailServerReadyCheck().
                    IsUserCurrentlyActive(currentDateTime, lastActivityOfUserDateTime,
                                          defaultSecondsForLastActivityTreshold);
                Assert.IsFalse(isActive,
                               <span class="str">&quot;User Should Be Inactive because last activity was 1 month ago and threshhold is 15 minutes&quot;</span>);
            }
        }

        [TestMethod]
        <span class="kwrd">public</span> <span class="kwrd">void</span> UnitTestNoRecentActivity()
        {
            <span class="rem">// a just inside threshold</span>
            var currentDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 12, 0, 0); <span class="rem">// assume current time is 3/1/2012 at noon</span>
            var lastActivityOfUserDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 11, 55, 0);
            <span class="rem">// last activity was 5 minutes before noon on 2/1/2012</span>
            <span class="rem">// threshhold for calling a user active</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold = 60*15; <span class="rem">// let's call this 15 minute threshhold</span>
            <span class="kwrd">bool</span> isActive = <span class="kwrd">new</span> MailServerReadyCheck().
                IsUserCurrentlyActive(currentDateTime, lastActivityOfUserDateTime,
                                      defaultSecondsForLastActivityTreshold);
            Assert.IsTrue(isActive,
                          <span class="str">&quot;User Should Be active because last activity was 5 minutes ago and threshhold is 15 minutes&quot;</span>);
        }

        [TestMethod]
        <span class="kwrd">public</span> <span class="kwrd">void</span> UnitTestCurrentDateBehindActivity()
        {
            <span class="rem">// impossible cause of last activity being after current time. just need to</span>
            <span class="rem">// make sure it returns true and does not crash</span>
            var currentDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 12, 0, 0); <span class="rem">// assume current time is 3/1/2012 at noon</span>
            var lastActivityOfUserDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 18, 0, 0);
            <span class="rem">// last activity was 5 minutes after noon on 2/1/2012</span>
            <span class="rem">// threshhold for calling a user active</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold = 60*15; <span class="rem">// let's call this 15 minute threshhold</span>
            <span class="kwrd">bool</span> isActive = <span class="kwrd">new</span> MailServerReadyCheck().
                IsUserCurrentlyActive(currentDateTime, lastActivityOfUserDateTime,
                                      defaultSecondsForLastActivityTreshold);
            Assert.IsTrue(isActive,
                          <span class="str">&quot;User Should Be active because last activity was 5 minutes after current time&quot;</span>);
        }
    }
}</pre>
<p>&#160;</p>
<p>Then, I write the actual method:</p>
<p>&#160;</p>
<pre class="csharpcode">       <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// This really just a supporting method for the above IsUserReadyToContactEmailServer call</span>
        <span class="rem">/// &lt;/summary&gt;</span>
        <span class="rem">/// &lt;param name=&quot;currentDateTime&quot;&gt;Current datetime (for testing can be anything)&lt;/param&gt;</span>
        <span class="rem">/// &lt;param name=&quot;usersLastActivityDate&quot;&gt;time user was last seen &lt;/param&gt;</span>
        <span class="rem">/// &lt;param name=&quot;defaultSecondsForLastActivityTreshold&quot;&gt;our definition of what makes a user currently active&lt;/param&gt;</span>
        <span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span class="kwrd">public</span> <span class="kwrd">bool</span> IsUserCurrentlyActive(DateTime currentDateTime,
            DateTime usersLastActivityDate,
            <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold
            )
        {
            <span class="kwrd">int</span> secondsSinceLastActivity = Convert.ToInt32(currentDateTime.Subtract(usersLastActivityDate).TotalSeconds);
            <span class="kwrd">bool</span> activeStatus = defaultSecondsForLastActivityTreshold &gt; secondsSinceLastActivity;
            <span class="kwrd">return</span> activeStatus;
        }</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>And finally, run the test (skipping a little debugging to make them all work)</p>
<p>Presto!</p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image12.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image_thumb12.png" width="338" height="263" /></a></p>
<p>&#160;</p>
<p>For completeness, I’m pasting the full unit test for the other main class I’m interested in testing below (as well as the class itself), however I won’t go into all the details.&#160; Everything is basically the same, just more of it</p>
<p>&#160;</p>
<h2>Conclusions and Observations</h2>
<p>Turns out, this was quite a bit of work to generate all these tests.&#160; At first pass, I can hear someone saying it seems like a lot to do just for two fairly simple methods.&#160; My answer is that I struggled trying to get it right until I finally decided to write the tests.&#160; Now, I’m confident it works correctly.&#160; What is more, if there is a bug in my algorithm, or something else comes up I need to include (which is very likely) I now have a great way to prove to myself (and others) that my class works.&#160; Also, this serves as great documentation for what expectations are for this very important function.</p>
<p>&#160;</p>
<h2>
<p>Appendex – Full Unit Test And Class Source</p>
<p>&#160;</p>
<pre class="csharpcode"><span class="kwrd">using</span> System;
<span class="kwrd">using</span> System.Collections.Generic;
<span class="kwrd">using</span> System.Linq;
<span class="kwrd">using</span> System.Text;

<span class="kwrd">namespace</span> AELib
{
    <span class="kwrd">public</span> <span class="kwrd">class</span> MailServerReadyCheck
    {
        <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// </span>
        <span class="rem">/// &lt;/summary&gt;</span>
        <span class="rem">/// &lt;param name=&quot;currentDateTime&quot;&gt;This is actually todays date and time in UTC, but since </span>
        <span class="rem">///    this method need to be tested, I’m going to pass in now rather just use it directly&lt;/param&gt;</span>
        <span class="rem">/// &lt;param name=&quot;lastActivityOfUserDateTime&quot;&gt; This is used to determine weather the user last activity</span>
        <span class="rem">///      makes that user considered active or inactive.  That is, say this is 60 seconds.</span>
        <span class="rem">///      Then, if the user has been active in the last 60 seconds, then this user is considered active.&lt;/param&gt;</span>
        <span class="rem">/// &lt;param name=&quot;lastEmailSession&quot;&gt;When the last email completed for this user&lt;/param&gt;</span>
        <span class="rem">/// &lt;param name=&quot;defaultSecondsForLastActivityTreshold&quot;&gt;This is used to determine weather the user last activity makes that </span>
        <span class="rem">///      user considered active or inactive.  That is, say this is 60 seconds.  Then, if the user has </span>
        <span class="rem">///      been active in the last 60 seconds, then this user is considered active.&lt;/param&gt;</span>
        <span class="rem">/// &lt;param name=&quot;defaultSecondsBetweenEmailRetryOutsideThreshold&quot;&gt; If the user is not active (as defined by </span>
        <span class="rem">///      DefaultSecondsForLastActivityTreshold) then this is the number of seconds we should wait before </span>
        <span class="rem">///      we should try and contact the users email server again.&lt;/param&gt;</span>
        <span class="rem">/// &lt;param name=&quot;defaultSecondsBetweenEmailRetryInsideThreshold&quot;&gt;If the user is active (as defined by </span>
        <span class="rem">///      DefaultSecondsForLastActivityTreshold) then this is the number of seconds we should wait </span>
        <span class="rem">///      before we should try and contact the users email server again.&lt;/param&gt;</span>
        <span class="rem">/// &lt;returns&gt;returns a boolean indicating weather the mail server needs to be rechecked for a given user.&lt;/returns&gt;</span>
        <span class="kwrd">public</span> <span class="kwrd">bool</span> IsUserReadyToContactEmailServer(DateTime currentDateTime,
            DateTime lastActivityOfUserDateTime,
            DateTime lastEmailSession,
            <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold,
            <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryOutsideThreshold,
            <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryInsideThreshold)
        {
            <span class="kwrd">bool</span> activeUser = IsUserCurrentlyActive(currentDateTime, lastActivityOfUserDateTime,
                                                      defaultSecondsForLastActivityTreshold);
            <span class="kwrd">int</span> thresholdForRetrySeconds =
                activeUser
                    ? defaultSecondsBetweenEmailRetryInsideThreshold
                    : defaultSecondsBetweenEmailRetryOutsideThreshold;

            <span class="rem">// this has to be a double because could be huge number if last email send very long ago</span>
            <span class="kwrd">double</span> timeSinceLastEmailSessionSeconds =
                currentDateTime.Subtract(lastEmailSession).TotalSeconds;

           <span class="kwrd">return</span> timeSinceLastEmailSessionSeconds &gt; thresholdForRetrySeconds;
        }

        <span class="rem">/// &lt;summary&gt;</span>
        <span class="rem">/// This really just a supporting method for the above IsUserReadyToContactEmailServer call</span>
        <span class="rem">/// &lt;/summary&gt;</span>
        <span class="rem">/// &lt;param name=&quot;currentDateTime&quot;&gt;Current datetime (for testing can be anything)&lt;/param&gt;</span>
        <span class="rem">/// &lt;param name=&quot;usersLastActivityDate&quot;&gt;time user was last seen &lt;/param&gt;</span>
        <span class="rem">/// &lt;param name=&quot;defaultSecondsForLastActivityTreshold&quot;&gt;our definition of what makes a user currently active&lt;/param&gt;</span>
        <span class="rem">/// &lt;returns&gt;&lt;/returns&gt;</span>
        <span class="kwrd">public</span> <span class="kwrd">bool</span> IsUserCurrentlyActive(DateTime currentDateTime,
            DateTime usersLastActivityDate,
            <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold
            )
        {
            <span class="kwrd">int</span> secondsSinceLastActivity = Convert.ToInt32(currentDateTime.Subtract(usersLastActivityDate).TotalSeconds);
            <span class="kwrd">bool</span> activeStatus = defaultSecondsForLastActivityTreshold &gt; secondsSinceLastActivity;
            <span class="kwrd">return</span> activeStatus;
        }
    }
}</pre>
<p>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
</p>
<p>And the tests…</p>
<p>&#160;</p>
<pre class="csharpcode"><span class="kwrd">using</span> System;
<span class="kwrd">using</span> AELib;
<span class="kwrd">using</span> Microsoft.VisualStudio.TestTools.UnitTesting;

<span class="kwrd">namespace</span> AEWeb.Tests
{
    [TestClass]
    <span class="kwrd">public</span> <span class="kwrd">class</span> UnitTestEmailReadyCheck
    {

        <span class="rem">///////////////////// IsUserCurrentlyActive follows</span>

        [TestMethod]
        <span class="kwrd">public</span> <span class="kwrd">void</span> UnitTestRecentActivity()
        {
            {
                <span class="rem">// a long time ago</span>
                var currentDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 12, 0, 0); <span class="rem">// assume current time is 3/1/2012 at noon</span>
                var lastActivityOfUserDateTime = <span class="kwrd">new</span> DateTime(2012, 2, 1, 12, 0, 0);
                <span class="rem">// last activity was a month ago, 2/1/2012 at noon</span>
                <span class="rem">// threshhold for calling a user active</span>
                <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold = 60*15; <span class="rem">// let's call this 15 minute threshhold</span>
                <span class="kwrd">bool</span> isActive = <span class="kwrd">new</span> MailServerReadyCheck().
                    IsUserCurrentlyActive(currentDateTime, lastActivityOfUserDateTime,
                                          defaultSecondsForLastActivityTreshold);
                Assert.IsFalse(isActive,
                               <span class="str">&quot;User Should Be Inactive because last activity was 1 month ago and threshhold is 15 minutes&quot;</span>);
            }
        }

        [TestMethod]
        <span class="kwrd">public</span> <span class="kwrd">void</span> UnitTestNoRecentActivity()
        {
            <span class="rem">// a just inside threshold</span>
            var currentDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 12, 0, 0); <span class="rem">// assume current time is 3/1/2012 at noon</span>
            var lastActivityOfUserDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 11, 55, 0);
            <span class="rem">// last activity was 5 minutes before noon on 2/1/2012</span>
            <span class="rem">// threshhold for calling a user active</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold = 60*15; <span class="rem">// let's call this 15 minute threshhold</span>
            <span class="kwrd">bool</span> isActive = <span class="kwrd">new</span> MailServerReadyCheck().
                IsUserCurrentlyActive(currentDateTime, lastActivityOfUserDateTime,
                                      defaultSecondsForLastActivityTreshold);
            Assert.IsTrue(isActive,
                          <span class="str">&quot;User Should Be active because last activity was 5 minutes ago and threshhold is 15 minutes&quot;</span>);
        }

        [TestMethod]
        <span class="kwrd">public</span> <span class="kwrd">void</span> UnitTestCurrentDateBehindActivity()
        {
            <span class="rem">// impossible cause of last activity being after current time. just need to</span>
            <span class="rem">// make sure it returns true and does not crash</span>
            var currentDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 12, 0, 0); <span class="rem">// assume current time is 3/1/2012 at noon</span>
            var lastActivityOfUserDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 18, 0, 0);
            <span class="rem">// last activity was 5 minutes after noon on 2/1/2012</span>
            <span class="rem">// threshhold for calling a user active</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold = 60*15; <span class="rem">// let's call this 15 minute threshhold</span>
            <span class="kwrd">bool</span> isActive = <span class="kwrd">new</span> MailServerReadyCheck().
                IsUserCurrentlyActive(currentDateTime, lastActivityOfUserDateTime,
                                      defaultSecondsForLastActivityTreshold);
            Assert.IsTrue(isActive,
                          <span class="str">&quot;User Should Be active because last activity was 5 minutes after current time&quot;</span>);
        }

        <span class="rem">///////////////////// IsUserReadyToContactEmailServer follows</span>

        [TestMethod]
        <span class="kwrd">public</span> <span class="kwrd">void</span> UnitTestIsUserReadyToContactEmailServerLongTimeAgoUserInActive()
        {
            <span class="rem">// let's use these parameters for every test</span>

            <span class="rem">// let's call this 30 minute threshhold. no contact within 30 minutes, than user is inactive</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold = 60 * 30;

            <span class="rem">// if active user, check email every 2 minutes</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryInsideThreshold = 60 * 2;

            <span class="rem">// if inactive user, check email every 20 minutes</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryOutsideThreshold = 60 * 20; 

            <span class="rem">// user checked email 5 hours ago and email has not been run for 3 hours</span>
            var currentDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 12, 0, 0); <span class="rem">// assume current time is 3/1/2012 at noon</span>
            var lastEmailSessionDateTime = <span class="kwrd">new</span> DateTime(2011,3,1,2, 0, 0); <span class="rem">// email session 2am of 1 year ago (very long ago)</span>
            var lastActivityOfUserDateTime = <span class="kwrd">new</span> DateTime(2011, 3, 1, 7, 0, 0); <span class="rem">// user checked email at 7am a year ago</span>

            <span class="kwrd">bool</span> emailSessionNeeded = <span class="kwrd">new</span> MailServerReadyCheck().IsUserReadyToContactEmailServer(currentDateTime,
                lastActivityOfUserDateTime,lastEmailSessionDateTime,defaultSecondsForLastActivityTreshold,
                defaultSecondsBetweenEmailRetryOutsideThreshold,
                defaultSecondsBetweenEmailRetryInsideThreshold);

            Assert.IsTrue(emailSessionNeeded,
                          <span class="str">&quot;user checked email 5 hours ago and email has not been run for 3 hours. Should have needed email session&quot;</span>);
        }

        [TestMethod]
        <span class="kwrd">public</span> <span class="kwrd">void</span> UnitTestIsUserReadyToContactEmailServerLongTimeAgoUserActive()
        {
            <span class="rem">// let's use these parameters for every test</span>
            <span class="rem">// let's call this 30 minute threshhold. no contact within 30 minutes, than user is inactive</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold = 60 * 30;

            <span class="rem">// if active user, check email every 2 minutes</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryInsideThreshold = 60 * 2;

            <span class="rem">// if inactive user, check email every 20 minutes</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryOutsideThreshold = 60 * 20;

            <span class="rem">// user checked email 1 minute ago and email has not been run for 3 hours</span>
            var currentDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 12, 0, 0); <span class="rem">// assume current time is 3/1/2012 at noon</span>
            var lastEmailSessionDateTime = <span class="kwrd">new</span> DateTime(2011, 3, 1, 2, 0, 0); <span class="rem">// email session 2am of 1 year ago (very long ago)</span>
            var lastActivityOfUserDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 11, 59, 0); <span class="rem">// user checked email 1 minute ago</span>

            <span class="kwrd">bool</span> emailSessionNeeded = <span class="kwrd">new</span> MailServerReadyCheck().IsUserReadyToContactEmailServer(currentDateTime,
                lastActivityOfUserDateTime, lastEmailSessionDateTime, defaultSecondsForLastActivityTreshold,
                defaultSecondsBetweenEmailRetryOutsideThreshold,
                defaultSecondsBetweenEmailRetryInsideThreshold);

            Assert.IsTrue(emailSessionNeeded,
                          <span class="str">&quot;user checked email  1 minute ago and email not been run for 1 year. should have run email again&quot;</span>);
        }

        [TestMethod]
        <span class="kwrd">public</span> <span class="kwrd">void</span> UnitTestIsUserReadyToContactEmailServerRecentlyUserActive()
        {
            <span class="rem">// let's use these parameters for every test</span>
            <span class="rem">// let's call this 30 minute threshhold. no contact within 30 minutes, than user is inactive</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold = 60 * 30;

            <span class="rem">// if active user, check email every 2 minutes</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryInsideThreshold = 60 * 2;

            <span class="rem">// if inactive user, check email every 20 minutes</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryOutsideThreshold = 60 * 20;

            <span class="rem">// user checked email 1 minute ago and email has not been run for 3 hours</span>
            var currentDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 12, 0, 0); <span class="rem">// assume current time is 3/1/2012 at noon</span>
            var lastEmailSessionDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 11, 50, 0); <span class="rem">// email session 10 minutes ago</span>
            var lastActivityOfUserDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 11, 59, 0); <span class="rem">// user checked email 1 minute ago</span>

            <span class="kwrd">bool</span> emailSessionNeeded = <span class="kwrd">new</span> MailServerReadyCheck().IsUserReadyToContactEmailServer(currentDateTime,
                lastActivityOfUserDateTime, lastEmailSessionDateTime, defaultSecondsForLastActivityTreshold,
                defaultSecondsBetweenEmailRetryOutsideThreshold,
                defaultSecondsBetweenEmailRetryInsideThreshold);

            Assert.IsTrue(emailSessionNeeded,
                          <span class="str">&quot;active user, email checked 10 minutes ago but since active should check again&quot;</span>);
        }

        [TestMethod]
        <span class="kwrd">public</span> <span class="kwrd">void</span> UnitTestIsUserReadyToContactEmailServerRecentlyUserInActive()
        {
            <span class="rem">// let's use these parameters for every test</span>
            <span class="rem">// let's call this 30 minute threshhold. no contact within 30 minutes, than user is inactive</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold = 60 * 30;

            <span class="rem">// if active user, check email every 2 minutes</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryInsideThreshold = 60 * 2;

            <span class="rem">// if inactive user, check email every 20 minutes</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryOutsideThreshold = 60 * 20;

            <span class="rem">// user checked email 1 minute ago and email has not been run for 3 hours</span>
            var currentDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 12, 0, 0); <span class="rem">// assume current time is 3/1/2012 at noon</span>
            var lastEmailSessionDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 11, 50, 0); <span class="rem">// email session 10 minutes ago</span>
            var lastActivityOfUserDateTime = <span class="kwrd">new</span> DateTime(2011, 3, 1, 11, 59, 0); <span class="rem">// user checked email 1 year ago</span>

            <span class="kwrd">bool</span> emailSessionNeeded = <span class="kwrd">new</span> MailServerReadyCheck().IsUserReadyToContactEmailServer(currentDateTime,
                lastActivityOfUserDateTime, lastEmailSessionDateTime, defaultSecondsForLastActivityTreshold,
                defaultSecondsBetweenEmailRetryOutsideThreshold,
                defaultSecondsBetweenEmailRetryInsideThreshold);

            Assert.IsFalse(emailSessionNeeded,
                          <span class="str">&quot;inactive user, but email checked very recently so should not be checking again&quot;</span>);
        }

        [TestMethod]
        <span class="kwrd">public</span> <span class="kwrd">void</span> UnitTestIsUserReadyToContactEmailServerEmailLastSession10MinutesagoActive()
        {
            <span class="rem">// let's use these parameters for every test</span>
            <span class="rem">// let's call this 30 minute threshhold. no contact within 30 minutes, than user is inactive</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsForLastActivityTreshold = 60 * 30;

            <span class="rem">// if active user, check email every 2 minutes</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryInsideThreshold = 60 * 2;

            <span class="rem">// if inactive user, check email every 20 minutes</span>
            <span class="kwrd">const</span> <span class="kwrd">int</span> defaultSecondsBetweenEmailRetryOutsideThreshold = 60 * 20;

            <span class="rem">// user checked email 1 minute ago and email has not been run for 3 hours</span>
            var currentDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 12, 0, 0); <span class="rem">// assume current time is 3/1/2012 at noon</span>
            var lastEmailSessionDateTime = <span class="kwrd">new</span> DateTime(2012, 3, 1, 1, 11, 50); <span class="rem">// email session 10 minutes ago</span>
            var lastActivityOfUserDateTime = <span class="kwrd">new</span> DateTime(2011, 3, 1, 11, 59, 0); <span class="rem">// user checked email 5 minutes ago</span>

            <span class="kwrd">bool</span> emailSessionNeeded = <span class="kwrd">new</span> MailServerReadyCheck().IsUserReadyToContactEmailServer(currentDateTime,
                lastActivityOfUserDateTime, lastEmailSessionDateTime, defaultSecondsForLastActivityTreshold,
                defaultSecondsBetweenEmailRetryOutsideThreshold,
                defaultSecondsBetweenEmailRetryInsideThreshold);

            Assert.IsTrue(emailSessionNeeded,
                          <span class="str">&quot;active user, but email checked very recently so should be checking again&quot;</span>);
        }

    }
}</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
</h2>
<p>&#160;</p>
<p>And the results of all tests running <img class="wlEmoticon wlEmoticon-smile" style="border-top-style: none; border-bottom-style: none; border-right-style: none; border-left-style: none" alt="Smile" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/wlEmoticon-smile.png" /></p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image13.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image_thumb13.png" width="463" height="279" /></a></p>
<p>&#160;</p>
<p>And, if you are still reading, I’ve got 100% of these two methods covered from these unit tests using <a href="http://msdn.microsoft.com/en-us/library/dd299398(v=vs.90).aspx">Code Coverage</a>.</p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image14.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/03/image_thumb14.png" width="490" height="121" /></a></p>
<p>&#160;</p>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>&#160;</p>
<ul>
<li></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2012/03/18/test-first-development-walk-through-with-visual-studio-2011-preview/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Create A Simple MVC Button Click Listener in SenchaTouch2</title>
		<link>http://peterkellner.net/2012/01/23/create-a-simple-mvc-button-click-listener-in-senchatouch2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=create-a-simple-mvc-button-click-listener-in-senchatouch2</link>
		<comments>http://peterkellner.net/2012/01/23/create-a-simple-mvc-button-click-listener-in-senchatouch2/#comments</comments>
		<pubDate>Tue, 24 Jan 2012 04:14:39 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[Sencha]]></category>
		<category><![CDATA[SenchaMVC]]></category>
		<category><![CDATA[visu]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2012/01/23/create-a-simple-mvc-button-click-listener-in-senchatouch2/</guid>
		<description><![CDATA[&#160;
I do not even want to admit how much time I spent today in a more complex application trying to get a button to respond to a tap event in SenchaTouch 2.0 Beta.&#160; I did notice several people like me on the forums with similar issues, but I did not find one concrete example that [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p>I do not even want to admit how much time I spent today in a more complex application trying to get a <a href="http://www.sencha.com/learn/layout-faq/">button</a> to respond to a tap event in <a href="http://www.sencha.com/products/touch/">SenchaTouch 2.0 Beta</a>.&#160; I did notice several people like me on the forums with similar issues, but I did not find one concrete example that made the simple “click a <a href="http://miamicoder.com/2010/creating-sencha-touch-toolbar-buttons/">button application</a>”.&#160; Of course I’m building this in <a href="http://www.microsoft.com/visualstudio/en-us">Microsoft Visual Studio 2010</a> and debugging with <a href="https://chrome.google.com/">Chrome</a>.&#160; Let me start at the end.&#160; When you are done, you will have a simple two button page that you can click on either button and have your MVC controller react to either button as well as trap the button hit inside the actual Panel.</p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/01/image12.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/01/image_thumb10.png" width="431" height="255" /></a></p>
<p>&#160;</p>
<p>And for those of you that have read enough, here is the source:&#160;&#160;
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:6cf571a4-5052-4531-9334-dc07fcce1ced" class="wlWriterEditableSmartContent">
<p> <a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/01/Sencha2MVCButtons1.zip" target="_blank">ZIP File Of Project With 2 Buttons and Listeners</a></p>
</div>
<p>&#160;</p>
<p>&#160;</p>
<h2>The Nitty Gritty</h2>
<p>Creating an SenchaTouch project with MVC is a little tricky to setup.&#160; You need to get all the directories right.&#160; My favorite way to do this is to use the Sencha Designer to create the project, then abandon it if I want to strike out on my own.&#160; That is what I’ve done here.&#160; I don’t even think you can tell.</p>
<p>Here is the file layout as seen from Visual Studio:</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/01/image13.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2012/01/image_thumb11.png" width="213" height="170" /></a></p>
<p>5 Files and 3 directories.</p>
<p>Let’s first take a look at the Panel that contains the buttons.&#160; Here is the code:</p>
<pre class="csharpcode">Ext.define(<span class="str">'MyApp.view.MyPanel'</span>, {
    extend: <span class="str">'Ext.Panel'</span>,
    alias: <span class="str">'widget.mypanel'</span>,
    config: {
        items: [
            {
                xtype: <span class="str">'button'</span>,
                itemId: <span class="str">'mybutton1'</span>,
                text: <span class="str">'MyButton1'</span>,
                action: <span class="str">'b1'</span>
            },
            {
                xtype: <span class="str">'button'</span>,
                itemId: <span class="str">'mybutton2'</span>,
                text: <span class="str">'MyButton2'</span>,
                action: <span class="str">'b2'</span>
            }
        ],

        listeners: [
            {
                fn: <span class="str">'onMybutton1Tap'</span>,
                <span class="kwrd">event</span>: <span class="str">'tap'</span>,
                <span class="kwrd">delegate</span>: <span class="str">'#mybutton1'</span>
            }
        ]
    },
    onMybutton1Tap: <span class="kwrd">function</span> (button, e, options) {
        console.log(<span class="str">'tap from mybutton1'</span>);
    }
});</pre>
<p>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Just a couple things to notice.&#160; 1) I’m defining an itemId which we will only be using in the local event (onMybutton1Tap).&#160; If you notice the listener has a property delegate which looks for that.&#160; I’m not sure if this is the best way to do this.&#160; I’ve notice several forum people saying use the “on” syntax.&#160; I did not find an example of that so this is what we have and it works.&#160; Also notice the action property.&#160; If you try and look that up on the SenchaTouch doc’s you will not find it.&#160; I just made it up out of thin air (really!, well, I followed others examples).&#160; I actually asked about this in the forums and I was told that is OK.&#160; As a type safe language guy, it hurts, but I did it.</p>
<p>&#160;</p>
<p>That’s it for the view.&#160; Let’s move over to the controller.&#160; Here is the source for that:</p>
<pre class="csharpcode">Ext.define(<span class="str">'MyApp.controller.MyController'</span>, {
    extend: <span class="str">'Ext.app.Controller'</span>,

    views: [
        <span class="str">'MyPanel'</span>
    ],

    refs: [
        {
            <span class="kwrd">ref</span>: <span class="str">'MyPanel'</span>,
            selector: <span class="str">'mypanel'</span>
        }],

    init: <span class="kwrd">function</span> () {

        <span class="kwrd">this</span>.control({
            <span class="str">'button[action=b1]'</span>: {
                tap: <span class="kwrd">this</span>.onButtonTap1
            },
            <span class="str">'button[action=b2]'</span>: {
                tap: <span class="kwrd">this</span>.onButtonTap2
            }
        });
    },

    onLaunch: <span class="kwrd">function</span> () {
        console.log(<span class="str">'onLaunch'</span>);
    },

    onButtonTap1: <span class="kwrd">function</span> () {
        console.log(<span class="str">'controller: onButtonTap1'</span>);
    },

    onButtonTap2: <span class="kwrd">function</span> () {
        console.log(<span class="str">'controller: onButtonTap2'</span>);
    },

    config: {

    }
});</pre>
<p>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>Basically, what this is doing is filtering all buttons, then looking at just the buttons that have an action of b1 or b2.&#160; If this was a large application, we’d probably want to do some more fancy component querying, but for now, this is OK.&#160; If you keep your controllers small, this should always work.</p>
<p>&#160;</p>
<p>That’s it for now.&#160; Download the source and run it.&#160; I hope it saves you a ton of time.&#160; </p>
<p>As always any experts want to comment and improve or correct what I’ve done, please feel free to comment.&#160; </p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2012/01/23/create-a-simple-mvc-button-click-listener-in-senchatouch2/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Using The DataContext In EntityFramework To Retrieve Nested Objects</title>
		<link>http://peterkellner.net/2011/11/27/using-the-datacontext-in-entityframework-to-retrieve-nested-objects/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-the-datacontext-in-entityframework-to-retrieve-nested-objects</link>
		<comments>http://peterkellner.net/2011/11/27/using-the-datacontext-in-entityframework-to-retrieve-nested-objects/#comments</comments>
		<pubDate>Sun, 27 Nov 2011 18:41:22 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeFirst]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/11/27/using-the-datacontext-in-entityframework-to-retrieve-nested-objects/</guid>
		<description><![CDATA[I’m always somewhat amazed when I read something from documentation that is not straight forward and it actually works.&#160; So amazed and excited that I often feel the need to blog about it.
So, here is the problem.&#160; I created a CodeFirst implementation of EntityFramework in Visual Studio and created a simple hierarchy in my data [...]]]></description>
			<content:encoded><![CDATA[<p>I’m always somewhat amazed when I read something from documentation that is not straight forward and it actually works.&#160; So amazed and excited that I often feel the need to blog about it.</p>
<p>So, here is the problem.&#160; I created a <a href="http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx">CodeFirst</a> implementation of <a href="http://msdn.microsoft.com/en-us/data/aa937723">EntityFramework</a> in <a href="http://msdn.microsoft.com/en-us/vstudio/aa718325">Visual Studio</a> and created a simple hierarchy in my data model.&#160; That is, without showing all the code, here is what I have:</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Person{    [Key]    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Id { get; set; }

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> List&lt;EmailAddressInfo&gt; EmailAddressInfoList { get; set; }    ..}

<span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> EmailAddressInfo{    [Key]    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Id { get; set; }

    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> EmailAddress { get; set; }

}</pre>
<p></div>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> EmailAccount{    [Key]    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Id { get; set; }    ...}</pre>
<p></div>
<p>Now, if you execute a LINQ command like the following:</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">var recs = (from data <span style="color: #0000ff">in</span> db.Persons                            select data);                <span style="color: #0000ff">foreach</span> (var rec <span style="color: #0000ff">in</span> recs)                {                    <span style="color: #0000ff">foreach</span> (var email <span style="color: #0000ff">in</span> rec.EmailAddressInfoList)                    {                        Console.WriteLine(email);                    }                }</pre>
<p></div>
<p>You get the error:</p>
<blockquote>
<p><b>Exception Details: </b><em>System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.</em></p>
</blockquote>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/11/image17.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/11/image_thumb16.png" width="579" height="181" /></a></p>
<p>&#160;</p>
<p>To solve this, you need to tell EntityFramework/CodeFirst to include the EmailAddressInfoList in the query.&#160; All you need to do is change the db.Persons to db.Persons.Include(&quot;EmailAddressInfoList&quot;) as follows:</p>
<p>&#160;</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">var recs = (from data <span style="color: #0000ff">in</span> db.Persons.Include(<span style="color: #006080">&quot;EmailAddressInfoList&quot;</span>)            select data);</pre>
<p></div>
<p>&#160;</p>
<p>Then, it all works!</p>
<p>Hope this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/11/27/using-the-datacontext-in-entityframework-to-retrieve-nested-objects/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>With Visual Studio 2010 (and vs2011 preview) Fast Launch Not Helpful Often</title>
		<link>http://peterkellner.net/2011/11/12/with-visual-studio-2010-and-vs2011-preview-fast-launch-not-helpful-often/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=with-visual-studio-2010-and-vs2011-preview-fast-launch-not-helpful-often</link>
		<comments>http://peterkellner.net/2011/11/12/with-visual-studio-2010-and-vs2011-preview-fast-launch-not-helpful-often/#comments</comments>
		<pubDate>Sat, 12 Nov 2011 17:06:58 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/11/12/with-visual-studio-2010-and-vs2011-preview-fast-launch-not-helpful-often/</guid>
		<description><![CDATA[&#160;
Let’s say I’ve created two Visual Studio Projects with the same name “WebApp” but reside in different directories as shown below (c:\temp\Proj1 and c:\temp\Proj2).&#160; I often call the project WebApp, however I want to keep the projects totally separate.&#160; Below are the File/New Project dialog examples.
&#160;


&#160;
Now, when I click start (this is with the vs2011 [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p>Let’s say I’ve created two Visual Studio Projects with the same name “WebApp” but reside in different directories as shown below (c:\temp\Proj1 and c:\temp\Proj2).&#160; I often call the project WebApp, however I want to keep the projects totally separate.&#160; Below are the File/New Project dialog examples.</p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/11/image10.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/11/image_thumb9.png" width="558" height="145" /></a></p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/11/image11.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/11/image_thumb10.png" width="564" height="137" /></a></p>
<p>&#160;</p>
<p>Now, when I click start (this is with the vs2011 preview I’m demonstrating), I see the following:</p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/11/image12.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/11/image_thumb11.png" width="322" height="392" /></a></p>
<p>&#160;</p>
<p>I actually have to hover over each “WebApp.sln” to tell which project is which.&#160; I think that information should be in the pinned app itself, not require a hover over to tell which project it is.</p>
<p>Agree?&#160; Add your comment below and go to the following link to vote up my connect suggestion.</p>
<p><a href="https://connect.microsoft.com/VisualStudio/feedback/details/679081/should-have-vs-launch-shortcuts-inside-vs-recent-projects-because-run-as-admin">https://connect.microsoft.com/VisualStudio/feedback/details/679081/should-have-vs-launch-shortcuts-inside-vs-recent-projects-because-run-as-admin</a></p>
<p>From the comments, Microsoft said they would consider it for the next release, but it looks like it has not made it.&#160; Let’s try and change that!</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/11/12/with-visual-studio-2010-and-vs2011-preview-fast-launch-not-helpful-often/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Microsoft ASP.NET presentations at Silicon Valley Code Camp on 10/8 and 10/9</title>
		<link>http://peterkellner.net/2011/10/02/microsoft-asp-net-presentations-at-silicon-valley-code-camp-on-108-and-109/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=microsoft-asp-net-presentations-at-silicon-valley-code-camp-on-108-and-109</link>
		<comments>http://peterkellner.net/2011/10/02/microsoft-asp-net-presentations-at-silicon-valley-code-camp-on-108-and-109/#comments</comments>
		<pubDate>Sun, 02 Oct 2011 22:01:19 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[VS2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/10/02/microsoft-asp-net-presentations-at-silicon-valley-code-camp-on-108-and-109/</guid>
		<description><![CDATA[&#160;
SV Code Camp is happening next weekend with well over 200 sessions.&#160; The conference is really technology agnostic and try hard to keep it that way, but I am an ASP.NET guy these days so I think it reasonable I take my Code Camp Coordinator hat off for the moment and put on my ASP.NET [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p><a href="http://www.siliconvalley-codecamp.com/">SV Code Camp</a> is happening next weekend with well over 200 sessions.&#160; The conference is really technology agnostic and try hard to keep it that way, but I am an ASP.NET guy these days so I think it reasonable I take my Code Camp Coordinator hat off for the moment and put on my ASP.NET hat.&#160; As an ASP.NET Microsoft MVP, I feel I’m in a good position to give a tour of some sessions in ASP.NET and throw in my own 2 cents.&#160; Below are a list of these sessions in no particular order.</p>
<p>&#160;</p>
<h3><a href="http://www.siliconvalley-codecamp.com/Sessions.aspx?OnlyOne=true&amp;id=687">Demystifying ASP.NET MVC, $(Lino).MVC{getknowledge()}</a></h3>
<p><strong>Sunday, October 9th 9:45AM</strong></p>
<p>MVC (Model View Controller) is one of the hottest patterns around building web sites these days no matter what the technology. When Microsoft first introduced Web programming to Visual Studio, they did it such that almost anyone who had done windows programming (VB or other) could quickly come up to speed and build a web site. Personally, this what sucked me into web programming. Drag a button, drag a textbox, double click on the button and add some code to set the text box and WHAM, you have a web site. Unfortunately, as things get more complicated, this model does not scale so well. <a href="http://www.asp.net/mvc">ASP.NET&#8217;s MVC implementation</a> does scale, it takes a little bit of flipping your mind around if you are Web Forms programmer, but definitely worth learning about and seeing if it&#8217;s right for you.</p>
<p><a href="http://www.siliconvalley-codecamp.com/Speakers.aspx?AttendeeId=529">Lino Tadros</a> is one of my favorite presenters and I&#8217;m sure will give you a much better understanding of MVC and how it fits into the ASP.NET programming model. I strongly recommend this one. Lino is the CEO for one of the top Application development and training companies in the world that partners with <a href="http://www.microsoft.com/en-us/default.aspx">Microsoft</a>, <a href="http://falafel.com/default.aspx">Falafel Software</a>. In other words, GO TO THIS SESSION!</p>
<p>&#160;</p>
<h3><a href="http://www.siliconvalley-codecamp.com/Sessions.aspx?OnlyOne=true&amp;id=823">Pragmatic JavaScript, jQuery &amp; Ajax with ASP.NET</a></h3>
<p>Saturday, October 9th 1:45PM</p>
<p><a href="http://www.siliconvalley-codecamp.com/Speakers.aspx?AttendeeId=6788">Damian Edwards</a> is a Program Manager on the Microsoft ASP.NET team.&#160; That’s right, he is flying here from Redmond just to present to us.&#160; He’s also an awesome presenter and we are extremely lucky to have him at Code Camp!&#160;&#160; Damien will tak about using JQuery while building sites with the Microsoft ASP.NET framework.&#160; Something Damien knows very very well.&#160; For those who don’t know, Damien is also leading the next generation efforts around ASP.NET WebForms which is the technology 90% of who use ASP.NET use (it’s what the <a href="http://www.siliconvalley-codecamp.com/">silicon valley code camp</a> web site is built with).</p>
<p>DON’T MISS THIS PRESENTATION!</p>
<p>&#160;</p>
<p> <a href="http://www.siliconvalley-codecamp.com/Sessions.aspx?OnlyOne=true&amp;id=627"><br />
<h3><a href="http://www.siliconvalley-codecamp.com/Sessions.aspx?OnlyOne=true&amp;id=627">WebMatrix In Depth</a></h3>
<p> </a>
<p>Saturday, October 9th&#160; 5:00PM</p>
<p><a href="http://www.siliconvalley-codecamp.com/Speakers.aspx?AttendeeId=6011">John Sheehan</a>, one of the best guys I know at explaining complex things is presenting this session on WebMatrix.&#160; I’m not sure everyone would agree with me, but for those people that really don’t like the heavy weight feeling of building web applications with Visual Studio, <a href="http://www.asp.net/webmatrix">WebMatrix</a> will make you feel right at home. It’s much lighter weight and also gives tons of punch.&#160; Come see John and ask him a million questions!&#160; He loves that.</p>
<p>&#160;</p>
<p>All the sessions Tagged ASP.NET can be found here:&#160; <a title="http://www.siliconvalley-codecamp.com/Sessions.aspx?sortby=title&amp;by=category&amp;tag=3" href="http://www.siliconvalley-codecamp.com/Sessions.aspx?sortby=title&amp;by=category&amp;tag=3">http://www.siliconvalley-codecamp.com/Sessions.aspx?sortby=title&amp;by=category&amp;tag=3</a></p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/10/02/microsoft-asp-net-presentations-at-silicon-valley-code-camp-on-108-and-109/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>With VS2010 Debugger, Step Into, From a Windows .Net App Directly into a Windows Azure Web Role Hosting a WCF Service In The Developer Fabric</title>
		<link>http://peterkellner.net/2011/06/07/with-vs2010-debugger-step-into-from-a-windows-net-app-directly-into-a-windows-azure-web-role-hosting-a-wcf-service-in-the-developer-fabric/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=with-vs2010-debugger-step-into-from-a-windows-net-app-directly-into-a-windows-azure-web-role-hosting-a-wcf-service-in-the-developer-fabric</link>
		<comments>http://peterkellner.net/2011/06/07/with-vs2010-debugger-step-into-from-a-windows-net-app-directly-into-a-windows-azure-web-role-hosting-a-wcf-service-in-the-developer-fabric/#comments</comments>
		<pubDate>Tue, 07 Jun 2011 21:52:09 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Azure Blob Storage]]></category>
		<category><![CDATA[Azure Deployment]]></category>
		<category><![CDATA[Azure Web Role]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/06/07/with-vs2010-debugger-step-into-from-a-windows-net-app-directly-into-a-windows-azure-web-role-hosting-a-wcf-service-in-the-developer-fabric/</guid>
		<description><![CDATA[&#160;
OK, I think this is the longest post title I’ve ever made, but if you understand it, you’ll know why it needs to be so long.&#160; I discovered this totally by accident.&#160; I would never ever have pressed F11 (step into) from a client side proxy and expect to get into anything but a bunch [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<p>OK, I think this is the longest post title I’ve ever made, but if you understand it, you’ll know why it needs to be so long.&#160; I discovered this totally by accident.&#160; I would never ever have pressed F11 (step into) from a client side proxy and expect to get into anything but a bunch of ugly machine generated proxy code (especially with <a href="http://www.microsoft.com/windowsazure/">Azure</a>).&#160; To my total surprise, I landed right inside my <a href="http://msdn.microsoft.com/en-us/netframework/aa663324.aspx">WCF</a> service as if I had started the <a href="http://www.microsoft.com/en-us/cloud/developer/resource.aspx?resourceId=what-is-windows-azure&amp;fbid=flWeS4xmu1C&amp;WT.srch=1&amp;WT.mc_id=85C588D4-A353-4E3D-8824-823A9551AA51&amp;CR_SCC=200028978">Windows Azure Developer fabric</a> in debug mode and set a break point.</p>
<p>So, I’ll step through the process an show screen shots on the outside chance I was dreaming and can’t reproduce it. If I can, now I will have proof so I can do it again.&#160; Sorry for the work in progress code you will see.&#160; The point here is really the debugger and not the code I’m showing so try and ignore that.</p>
<p>OK, here we go.</p>
<p>Let’s assume you have a completely configured azure web role that is hosting a simple <a href="http://en.wikipedia.org/wiki/Windows_Communication_Foundation">WCF</a> service.&#160; When you start that web role and point at the service, you’ll get something like this: (just a note that I started the app fabric by deploying directly from <a href="http://www.microsoft.com/en-us/default.aspx">Microsoft</a> <a href="http://www.microsoft.com/visualstudio/en-us">Visual Studio 2010</a> with the start/run.&#160; I am using a debug profile but am <strong>NOT </strong>running in debug mode.&#160; If I do run the app fabric in debug mode, this does not work.</p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/06/image4.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/06/image_thumb4.png" width="480" height="366" /></a></p>
<p>  <span id="more-1508"></span>
<p>&#160;</p>
<p>OK, now let’s run my app that has a <a href="http://visualstudiomagazine.com/articles/2011/06/01/pcnet_wcf-and-soa.aspx">WCF</a> client configured to connect to this service.&#160; The configuration is the normal “Add Service Reference” you would expect but I’m showing it any how just to be clear.</p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/06/image5.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/06/image_thumb5.png" width="485" height="473" /></a></p>
<p>&#160;</p>
<p>Next, we simply Launch <a href="http://www.microsoft.com/visualstudio/en-us">Visual Studio 2010</a> in <a href="http://msdn.microsoft.com/en-us/library/sc65sadd.aspx">debug</a> mode and put a break point just before we cross over into the WCF service by calling the above proxy code.&#160; Here is what that screen looks like after the break point is hit and I’m about to step INTO.</p>
<p>&#160;</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/06/image6.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/06/image_thumb6.png" width="616" height="198" /></a></p>
<p><font size="6">F11</font></p>
<p>&#160;</p>
<p>and…..</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/06/image7.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/06/image_thumb7.png" width="657" height="265" /></a></p>
<p><font size="2"></font></p>
<p>There we are!!! right inside the source code that is my WCF service with everything working as if I had launched the site on it’s own, set a break point and waited.</p>
<p>&#160;</p>
<p>Truly amazing!!!&#160; I have nothing more to say.</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/06/07/with-vs2010-debugger-step-into-from-a-windows-net-app-directly-into-a-windows-azure-web-role-hosting-a-wcf-service-in-the-developer-fabric/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Fiddler to See you WCF Traffic</title>
		<link>http://peterkellner.net/2011/04/25/getting-fiddler-to-see-you-wcf-traffic/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-fiddler-to-see-you-wcf-traffic</link>
		<comments>http://peterkellner.net/2011/04/25/getting-fiddler-to-see-you-wcf-traffic/#comments</comments>
		<pubDate>Tue, 26 Apr 2011 00:48:15 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[Fiddler]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/04/25/getting-fiddler-to-see-you-wcf-traffic/</guid>
		<description><![CDATA[There are lots of articles on the internet if you search for WCF Fiddler however it’s not clear what the simplest path to follow is. For me, it turns out that just sprinkling a couple lines of code at the bottom of my windows forms app’s app.config file is all it took.&#160; I got the [...]]]></description>
			<content:encoded><![CDATA[<p>There are lots of articles on the internet if you search for <a href="http://msdn.microsoft.com/en-us/netframework/aa663324">WCF</a> <a href="http://fiddler2.com/fiddler2/">Fiddler</a> however it’s not clear what the simplest path to follow is. For me, it turns out that just sprinkling a couple lines of code at the bottom of my windows forms app’s app.config file is all it took.&#160; I got the tip from this post:&#160; <a title="http://www.fiddler2.com/fiddler/help/hookup.asp" href="http://www.fiddler2.com/fiddler/help/hookup.asp">http://www.fiddler2.com/fiddler/help/hookup.asp</a></p>
<p>The magic lines are as follows:</p>
<p>&lt;<b>system.net</b>&gt;    <br />&#160;&#160;&#160; &lt;defaultProxy&gt;    <br />&#160;&#160;&#160;&#160;&#160; &lt;proxy bypassonlocal=&quot;false&quot; usesystemdefault=&quot;true&quot; /&gt;    <br />&#160;&#160;&#160; &lt;/defaultProxy&gt;    <br />&#160; &lt;/<b>system.net</b>&gt;    <br />&lt;/configuration&gt;</p>
<p>That’s it!&#160; Now, Fiddler just sees the traffic.&#160; I’m a happy camper.</p>
<p><a href="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/04/image2.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://PetersBlogCDN.s3.amazonaws.com/wp/wp/wp-content/uploads/2011/04/image_thumb2.png" width="721" height="357" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/04/25/getting-fiddler-to-see-you-wcf-traffic/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MVC3 Unable To Return Large JSON Even with Web Config Patch, Using LargeJsonResult Instead</title>
		<link>http://peterkellner.net/2011/03/21/mvc3-unable-to-return-large-json-even-with-web-config-patch-using-largejsonresult-instead/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mvc3-unable-to-return-large-json-even-with-web-config-patch-using-largejsonresult-instead</link>
		<comments>http://peterkellner.net/2011/03/21/mvc3-unable-to-return-large-json-even-with-web-config-patch-using-largejsonresult-instead/#comments</comments>
		<pubDate>Mon, 21 Mar 2011 18:11:25 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[ASP.NET 4.0]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/03/21/mvc3-unable-to-return-large-json-even-with-web-config-patch-using-largejsonresult-instead/</guid>
		<description><![CDATA[I’m trying to download a base64 encoded image that is about 4 Megabytes embedded inside a JSON object.  When I first tried this from my MVC3 Web application, I got the error:

Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.


I then tried [...]]]></description>
			<content:encoded><![CDATA[<p>I’m trying to download a base64 encoded image that is about 4 Megabytes embedded inside a <a href="http://json.org/">JSON</a> object.  When I first tried this from my <a href="http://www.asp.net/mvc/mvc3">MVC3 Web application</a>, I got the error:</p>
<blockquote>
<h4><em>Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.</em></h4>
</blockquote>
<p><span id="more-1450"></span></p>
<p>I then tried to set the web.config property that everyone seems to reference on the web as follows:</p>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">system.web.extensions</span><span style="color: #0000ff;">&gt;</span>
    <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">scripting</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">webServices</span><span style="color: #0000ff;">&gt;</span>
            <span style="color: #0000ff;">&lt;</span><span style="color: #800000;">jsonSerialization</span> <span style="color: #ff0000;">maxJsonLength</span><span style="color: #0000ff;">="2147483645"</span> <span style="color: #ff0000;">recursionLimit</span><span style="color: #0000ff;">="100"</span><span style="color: #0000ff;">&gt;</span>
            <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">jsonSerialization</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">webServices</span><span style="color: #0000ff;">&gt;</span>
        <span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">scripting</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">system.web.extensions</span><span style="color: #0000ff;">&gt;</span></pre>
</div>
<p>However, I kept getting the same error over and over.  So, I used the technique suggested in this article</p>
<p><a title="http://thoughtfulcode.wordpress.com/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/" href="http://thoughtfulcode.wordpress.com/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/">http://thoughtfulcode.wordpress.com/2011/01/03/custom-jsonresult-class-for-asp-net-mvc-to-avoid-maxjsonlength-exceeded-exception/</a></p>
<p>by <a href="http://brianreiter.org/about/">Brian Reiter</a> to create an override of the JsonResult class.  That actually worked for me, however unfortunately, it does not show the shape of the data that is currently being returned by MVC3.  That is, MVC3 currently returns a JsonResult as follows:</p>
<div>
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">return</span> Json(<span style="color: #0000ff;">new</span>
{
    total = blobs.Count,
    data = blobs.ToList()
}, JsonRequestBehavior.AllowGet);</pre>
</div>
<div>Brian suggest in his article to return his LargeJsonResult, do the following:</div>
<div>
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">new</span> LargeJsonResult() { Data = output, MaxJsonLength = <span style="color: #0000ff;">int</span>.MaxValue };</pre>
</div>
<div>This does not quite work for what I want.  For my result to work, I needed to modify the result a little and return it as follows:</div>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="text-align: left; line-height: 12pt; background-color: #f4f4f4; margin: 0em; width: 100%; font-family: 'Courier New', courier, monospace; direction: ltr; color: black; font-size: 8pt; overflow: visible; border-style: none; padding: 0px;"><span style="color: #0000ff;">return</span>
   <span style="color: #0000ff;">new</span> LargeJsonResult
       {
           MaxJsonLength = 20000000,
           JsonRequestBehavior = JsonRequestBehavior.AllowGet,
           Data = <span style="color: #0000ff;">new</span>
                      {
                          total = blobs.Count,
                          data = blobs.ToList()
                      }
       };</pre>
</div>
<div>Not much different, but does solve my problem.</div>
<div>Thanks Brian for the nice contribution!</div>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/03/21/mvc3-unable-to-return-large-json-even-with-web-config-patch-using-largejsonresult-instead/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How To Gracefully Stop and Windows Azure Deployment From Visual Studio 2010</title>
		<link>http://peterkellner.net/2011/03/09/how-to-gracefully-stop-and-windows-azure-deployment-from-visual-studio-2010/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-gracefully-stop-and-windows-azure-deployment-from-visual-studio-2010</link>
		<comments>http://peterkellner.net/2011/03/09/how-to-gracefully-stop-and-windows-azure-deployment-from-visual-studio-2010/#comments</comments>
		<pubDate>Wed, 09 Mar 2011 17:00:16 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Azure Web Role]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[VS2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/03/09/how-to-gracefully-stop-and-windows-azure-deployment-from-visual-studio-2010/</guid>
		<description><![CDATA[The way I do Windows Azure development is that I have OneClick Deployment setup on my Azure host.&#160; This means I can simply and quickly update what I have on my Azure WebRole and test it without going through the 10 to 20 minute deployment process.&#160; Sometimes, when I’m tired, I accidentally press the “Debug” [...]]]></description>
			<content:encoded><![CDATA[<p>The way I do <a href="http://www.microsoft.com/windowsazure/">Windows Azure</a> development is that I have <a href="http://social.technet.microsoft.com/Forums/en/windowsazuredata/thread/aedbfdd4-0092-4b10-a84f-ba816438ed54">OneClick Deployment</a> setup on my Azure host.&#160; This means I can simply and quickly update what I have on my Azure WebRole and test it without going through the 10 to 20 minute deployment process.&#160; Sometimes, when I’m tired, I accidentally press the “Debug” or “Run” button on my <a href="http://www.microsoft.com/visualstudio/en-us/products/2010-editions">Visual Studio 2010</a> cloud project.&#160; This launches a 2 or 3 minute process that I have trouble killing.</p>
<p>Today, I accidentally figured out a way to stop it.&#160; Simply go to the web project and right mouse button on your project and say “Publish”. You will be immediately presented with an option to stop the deployment!</p>
<p>That’s it.&#160; Simple, but pretty undiscoverable IMHO.</p>
<p>&#160;</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/How-To-Gracefully-Stop-and-Windows-Azure_7C9D/image.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/How-To-Gracefully-Stop-and-Windows-Azure_7C9D/image_thumb.png" width="244" height="95" /></a></p>
<p>&#160;</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/How-To-Gracefully-Stop-and-Windows-Azure_7C9D/SNAGHTMLc6db08a.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="SNAGHTMLc6db08a" border="0" alt="SNAGHTMLc6db08a" src="http://peterkellner.net/FilesForWebDownload/How-To-Gracefully-Stop-and-Windows-Azure_7C9D/SNAGHTMLc6db08a_thumb.png" width="287" height="369" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/03/09/how-to-gracefully-stop-and-windows-azure-deployment-from-visual-studio-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pinning Watch Variables While Debugging Visual Studio 2010, Awesome Feature</title>
		<link>http://peterkellner.net/2010/12/08/pinning-watch-variables-while-debugging-visual-studio-2010/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=pinning-watch-variables-while-debugging-visual-studio-2010</link>
		<comments>http://peterkellner.net/2010/12/08/pinning-watch-variables-while-debugging-visual-studio-2010/#comments</comments>
		<pubDate>Wed, 08 Dec 2010 18:11:25 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/12/08/pinning-watch-variables-while-debugging-visual-studio-2010/</guid>
		<description><![CDATA[I just have to blog about what a helpful feature this is that the VS team added in VS2010.&#160; That is, as you are debugging, you can set a watch variable and pin it to your source code.&#160; In previous versions, you needed to basically go through the hassle of setting conditional break points to [...]]]></description>
			<content:encoded><![CDATA[<p>I just have to blog about what a helpful feature this is that the VS team added in <a href="http://www.microsoft.com/visualstudio/en-us/products/2010-editions">VS2010</a>.&#160; That is, as you are <a href="http://weblogs.asp.net/scottgu/archive/2010/08/18/debugging-tips-with-visual-studio-2010.aspx">debugging</a>, you can set a watch variable and pin it to your source code.&#160; In previous versions, you needed to basically go through the hassle of setting conditional break points to do simple debugging or be prepared to do lots of mouse clicking.&#160; In the latest version, all you have to do is click the little thumb tack icon and drag the variable to where you want to see it. Here is what your screen looks like.</p>
<p>&#160;</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/Pinning-Watch-Variables-While-Debugging-_87E9/image.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/Pinning-Watch-Variables-While-Debugging-_87E9/image_thumb.png" width="430" height="174" /></a></p>
<p>  <span id="more-1420"></span>
<p>The way I got here was to first hover over the variable I wanted to look at.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/Pinning-Watch-Variables-While-Debugging-_87E9/image_3.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/Pinning-Watch-Variables-While-Debugging-_87E9/image_thumb_3.png" width="355" height="68" /></a></p>
<p>Then, expand the details and press the push pin with the red arrow pointing at it.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/Pinning-Watch-Variables-While-Debugging-_87E9/image_4.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/Pinning-Watch-Variables-While-Debugging-_87E9/image_thumb_4.png" width="712" height="208" /></a></p>
<p>Pressing the push pin puts the variable on your desktop and lets you drag it where ever you want.&#160; The other nice thing is it stays there after you close visual studio and bring it up again (for those of us that require more than one debugging session to find a problem).</p>
<p>A very nice feature!&#160; </p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/12/08/pinning-watch-variables-while-debugging-visual-studio-2010/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A Simple MultiThreading Pattern For C# With No Statics.  Shows Compressing A File With GZip</title>
		<link>http://peterkellner.net/2010/12/04/simple-multithreading-pattern-for-c-sharp-with-no-statics-and-gzip-compression/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=simple-multithreading-pattern-for-c-sharp-with-no-statics-and-gzip-compression</link>
		<comments>http://peterkellner.net/2010/12/04/simple-multithreading-pattern-for-c-sharp-with-no-statics-and-gzip-compression/#comments</comments>
		<pubDate>Sat, 04 Dec 2010 15:09:03 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[gzip]]></category>
		<category><![CDATA[threading]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[VS2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/12/04/simple-multithreading-pattern-for-c-sharp-with-no-statics-and-gzip-compression/</guid>
		<description><![CDATA[Background
This post shows a very simple technique for processing a gzip compression on a background thread using c# with Visual Studio 2010.&#160; What is unique here is we are using no statics to do it.&#160; I’m not totally against using statics, but in general, it is best to avoid them.&#160; I’ve heard the notorious Ward [...]]]></description>
			<content:encoded><![CDATA[<h2>Background</h2>
<p>This post shows a very simple technique for processing a <a href="http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx">gzip</a> compression on a background <a href="http://msdn.microsoft.com/en-us/library/aa645740(VS.71).aspx">thread</a> using <a href="http://msdn.microsoft.com/en-us/vcsharp/default.aspx">c#</a> with <a href="http://msdn.microsoft.com/en-us/vstudio/default.aspx">Visual Studio 2010</a>.&#160; What is unique here is we are using no statics to do it.&#160; I’m not totally against using statics, but in general, it is best to avoid them.&#160; I’ve heard the notorious <a href="http://www.neverindoubtnet.blogspot.com/">Ward Bell</a> say <a href="http://msdn.microsoft.com/en-us/library/98f28cdx.aspx">statics</a> are evil and have had many cases where they have bitten me.&#160; Since I heard Ward say this, I’ve been trying to avoid them where I can.</p>
<p>&#160;</p>
<h2>The Simple Problem</h2>
<p>The problem is to simply compress a file to bytes and return to us the compressed, uncompressed and byte array of the result.&#160; We can pass parameters into a thread, however we can not return them (when I say thread, I mean the anonymous method that processes our data).</p>
<p>  <span id="more-1418"></span><br />
<h2>Some Code</h2>
<p>So, to that end, Let’s create a main method as below.&#160; Notice that it creates a very simple anonymous method which executes the code cryptoCopress.CompressFile(…), then simply starts that thread.&#160; Once the thread starts, it simply waits for the thread to end by looping every 50 milliseconds on the thread.IsAlive method.&#160; Finally, when it returns, it simply looks at the cryptCompress object for the results.&#160; No Statics!</p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:8eb9d37f-1541-4f29-b6f4-1eea890d4876:dd5555c2-7bcc-4715-9cc2-b94a7ef4604b" class="wlWriterEditableSmartContent">
<div>Code:<a href="http://peterkellner.net/FilesForWebDownload/A-Simple-MultiThreading-Pattern-For-C_6021/MultiThreadingInCSharpSimple.zip" target="_self">MultiThreadingInCSharpSimple.zip</a></div>
</p>
</div>
<p>&#160;</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span>[] args){    var cryptCompress =        <span style="color: #0000ff">new</span> CryptCompress();    var thread =        <span style="color: #0000ff">new</span> Thread(            () =&gt;            cryptCompress.CompressFile                (<span style="color: #006080">@&quot;G:\NoBackup\ext-3.2.1\ext-all-debug-w-comments.js&quot;</span>));    thread.Start();

    <span style="color: #0000ff">int</span> cnt = 0;    <span style="color: #0000ff">while</span> (thread.IsAlive)    {        cnt++;        Console.WriteLine(<span style="color: #006080">&quot;Waiting... &quot;</span> + cnt);        Thread.Sleep(50);    }    Console.WriteLine(<span style="color: #006080">&quot;Before Compression KBytes: {0}&quot;</span>,                        cryptCompress.BeforeCompressionBytes/1000);    Console.WriteLine(<span style="color: #006080">&quot;After Compression KBytes: {0}&quot;</span>,                        cryptCompress.AfterCompressionBytes/1000);}</pre>
<p></div>
<p>Now, Lets look at the CryptCompress class.&#160; Notice that it’s basically got one public method (CompressFile) and 3 public properties that will be used to hold the return values.&#160; This way, the main method that started the thread can get the results.&#160; Again, notice that there is no word static any place in this project.</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> CryptCompress{<span style="color: #0000ff">public</span> <span style="color: #0000ff">byte</span>[] CompressedBytes { get; set; }<span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> BeforeCompressionBytes { get; set; }<span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> AfterCompressionBytes { get; set; }

<span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> CompressFile(<span style="color: #0000ff">string</span> fileName){    <span style="color: #0000ff">using</span> (var fileStream =         <span style="color: #0000ff">new</span> FileStream(fileName, FileMode.Open, FileAccess.Read))    {        var uncompressedBytes = <span style="color: #0000ff">new</span> <span style="color: #0000ff">byte</span>[fileStream.Length];        fileStream.Read(uncompressedBytes, 0,             (<span style="color: #0000ff">int</span>) fileStream.Length);        CompressedBytes = CompressGzip(uncompressedBytes);        BeforeCompressionBytes = fileStream.Length;        AfterCompressionBytes = CompressedBytes.Length;        fileStream.Close();    }}

<span style="color: #008000">/// &lt;summary&gt;</span><span style="color: #008000">/// Take a simple stream of uncompressed bytes and compress them</span><span style="color: #008000">/// &lt;/summary&gt;</span><span style="color: #008000">/// &lt;param name=&quot;uncompressedBytes&quot;&gt;&lt;/param&gt;</span><span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">byte</span>[] CompressGzip(<span style="color: #0000ff">byte</span>[] uncompressedBytes){    <span style="color: #0000ff">using</span> (var memory = <span style="color: #0000ff">new</span> MemoryStream())    {        <span style="color: #0000ff">using</span>             (var gZipStream =                 <span style="color: #0000ff">new</span> GZipStream(memory, CompressionMode.Compress, <span style="color: #0000ff">true</span>))        {            gZipStream.Write                (uncompressedBytes, 0, uncompressedBytes.Length);        }        <span style="color: #0000ff">return</span> memory.ToArray();    }}}</pre>
</div>
<p>&#160;</p>
<h2>The Results</h2>
<p>When we run this, notice that it takes 3 iterations (or 150 milliseconds) to complete. I’m only compressing a small file so no surprise.&#160; The file is actually 2.7 Megabytes and compress to .7 Megabytes.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/A-Simple-MultiThreading-Pattern-For-C_6021/image.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/A-Simple-MultiThreading-Pattern-For-C_6021/image_thumb.png" width="244" height="116" /></a></p>
<p>&#160;</p>
<p>That’s it for now! Hope this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/12/04/simple-multithreading-pattern-for-c-sharp-with-no-statics-and-gzip-compression/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using SHA256Managed to Generate SHA256 Hash with Microsoft .Net Cryptography Namespace / Memory Usage Implications</title>
		<link>http://peterkellner.net/2010/12/03/using-sha256managed-to-generate-sha256-hash/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=using-sha256managed-to-generate-sha256-hash</link>
		<comments>http://peterkellner.net/2010/12/03/using-sha256managed-to-generate-sha256-hash/#comments</comments>
		<pubDate>Fri, 03 Dec 2010 19:33:10 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Cryptography]]></category>
		<category><![CDATA[SHA256]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/?p=1415</guid>
		<description><![CDATA[&#160;
Background And Conclusion
In my very last post I talked about how using SHA256 seemed to not be affected by whether you use Buffered or Not Buffered streams.&#160; An astute reader (Samuel Jack) referenced an article by one of my favorite ex MIcrosoft employees (Brad Abrams) saying that almost all the .Net streams have buffering built [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<h2>Background And Conclusion</h2>
<p>In my <a href="http://peterkellner.net/2010/11/24/efficiently-generating-sha256-checksum-for-files-using-csharp/">very last post</a> I talked about how using <a href="http://msdn.microsoft.com/en-us/library/d0hxa5e7.aspx">SHA256</a> seemed to not be affected by whether you use Buffered or Not Buffered streams.&#160; An astute reader (<a href="http://blog.functionalfun.net/">Samuel Jack</a>) referenced <a href="http://blogs.msdn.com/b/brada/archive/2004/04/15/114329.aspx">an article</a> by one of my favorite ex MIcrosoft employees (<a href="http://bradabrams.com/">Brad Abrams</a>) saying that almost all the .Net streams have buffering built in.</p>
<p>Well, I think SHA256Managed does not.&#160; One of the challenges I’ve been facing lately with my current project is to provide feedback while doing all kinds of byte piping (stream stuff).&#160; While figuring out how to do this, I inadvertently figured out that buffering makes a huge difference.&#160; Briefly, let me show my results first, then talk about the code that went into it.</p>
<p>Notice that when I use a 64MB Buffer, the memory used for the process is 77MB and when I use an 8MB buffer, the memory used is 17MB.&#160; Clearly the buffer allocated matters.&#160; Just for information, I’ve also included the code that does not break the SHA256 hash into blocks, and it has the same results based on the size of the Memory Buffer declared.&#160; That is, small buffer, small use, big buffer, big use.</p>
<p>  <span id="more-1415"></span>
<p><a href="http://peterkellner.net/FilesForWebDownload/Using_9C01/image.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/Using_9C01/image_thumb.png" width="402" height="220" /></a></p>
<p><a href="http://peterkellner.net/FilesForWebDownload/Using_9C01/image_3.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/Using_9C01/image_thumb_3.png" width="405" height="276" /></a></p>
<h2>The Code</h2>
<p>The code that I wrote to do this basically does a nice job of splitting up the processing so that you can log progress as it runs. I’m attaching the <a href="http://msdn.microsoft.com/en-us/vstudio/default.aspx">Visual Studio 2010</a> project here as well as pasting the relevant source code.&#160; It’s a good base to experiment with.&#160; Feel free to comment on interesting things you find experimenting with it.</p>
<p>Visual Studio 2010 Project:&#160;&#160;&#160;&#160;&#160;&#160;
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:8eb9d37f-1541-4f29-b6f4-1eea890d4876:3633437a-182a-48b1-90bc-e4ae3be04a84" class="wlWriterEditableSmartContent">
<div><a href="http://peterkellner.net/FilesForWebDownload/Using_9C01/SHA512BufferedConsoleProject.zip" target="_self">SHA512BufferedConsoleProject.zip</a></div>
</p>
</div>
<p>&#160;</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #008000">/// &lt;summary&gt;</span><span style="color: #008000">/// Used so we can get MD5Hash and get progress on calculation</span><span style="color: #008000">/// &lt;/summary&gt;</span><span style="color: #008000">/// &lt;param name=&quot;streamIn&quot;&gt;&lt;/param&gt;</span><span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> GetSha512Buffered(Stream streamIn){    Process process = Process.GetCurrentProcess();    <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> bufferSizeForMd5Hash = 1024*1024*8; <span style="color: #008000">// 8MB</span>    Console.WriteLine(<span style="color: #006080">&quot;-----------------------------------&quot;</span>);    Console.WriteLine(<span style="color: #006080">&quot;Memory Buffer Size {0} MB&quot;</span>, bufferSizeForMd5Hash/1000);    Console.WriteLine(<span style="color: #006080">&quot;-----------------------------------&quot;</span>);    <span style="color: #0000ff">string</span> hashString;    <span style="color: #0000ff">using</span> (var md5Prov = <span style="color: #0000ff">new</span> SHA256Managed())    {        <span style="color: #0000ff">int</span> readCount;        <span style="color: #0000ff">long</span> bytesTransfered = 0;        var buffer = <span style="color: #0000ff">new</span> <span style="color: #0000ff">byte</span>[bufferSizeForMd5Hash];        <span style="color: #0000ff">while</span> ((readCount = streamIn.Read(buffer, 0, buffer.Length)) != 0)        {            <span style="color: #008000">// Need to figure out if this is final block</span>            <span style="color: #0000ff">if</span> (bytesTransfered + readCount == streamIn.Length)            {                md5Prov.TransformFinalBlock(buffer, 0, readCount);            }            <span style="color: #0000ff">else</span>            {                md5Prov.TransformBlock(buffer, 0, bufferSizeForMd5Hash, buffer, 0);            }            bytesTransfered += readCount;            Console.WriteLine(<span style="color: #006080">&quot;GetSha512Buffered:{0}MB/{1}MB.   Memory Used: {2}MB&quot;</span>,                                bytesTransfered/1000000,                                streamIn.Length/1000000,                                process.PrivateMemorySize64/1000000);        }        hashString = BitConverter.ToString(md5Prov.Hash).Replace(<span style="color: #006080">&quot;-&quot;</span>, String.Empty);        md5Prov.Clear();    }    <span style="color: #0000ff">return</span> hashString;}</pre>
<p></div>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/12/03/using-sha256managed-to-generate-sha256-hash/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Building Job Ads Management Module With LightSwitch Beta 1 For Silicon Valley Code Camp</title>
		<link>http://peterkellner.net/2010/09/30/building-job-ads-management-module-with-lightswitch-beta-1-for-silicon-valley-code-camp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-job-ads-management-module-with-lightswitch-beta-1-for-silicon-valley-code-camp</link>
		<comments>http://peterkellner.net/2010/09/30/building-job-ads-management-module-with-lightswitch-beta-1-for-silicon-valley-code-camp/#comments</comments>
		<pubDate>Thu, 30 Sep 2010 15:34:29 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[LightSwitch]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/?p=1370</guid>
		<description><![CDATA[&#160;
The End Result
&#160; 
&#160;
Motivation
As you can imagine, The Silicon Valley Code Camp web site has lots of “back end” functions that need to be done. That is, things like doing mailings, assigning roles to users, making schedules, allocating rooms and times and literally&#160; hundreds of other tasks like that.&#160; Over the 5 years of code [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>
<h2>The End Result</h2>
<p>&#160;<a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_17.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_17.png" width="398" height="318" /></a> </p>
<p>&#160;</p>
<h2>Motivation</h2>
<p>As you can imagine, The <a href="http://www.siliconvalley-codecamp.com/" target="_blank">Silicon Valley Code Camp</a> web site has lots of “back end” functions that need to be done. That is, things like doing mailings, assigning roles to users, making schedules, allocating rooms and times and literally&#160; hundreds of other tasks like that.&#160; Over the 5 years of code camp, I’ve always built simple protected asp.net web pages to do this.&#160; I’ve always used the simplest asp.net control I could find, such as GridView, DetailsView, DropDownList, and SqlDataSource.&#160; The interfaces usually basically work but are very clumsy and lacking in both functionality and aesthetics.</p>
<p>&#160;</p>
<h2>Why Now</h2>
<p>I’ve seen lots of short demos on <a href="http://msdn.microsoft.com/en-us/lightswitch/default.aspx" target="_blank">LightSwitch for Visual Studio</a> and recently read on someone else&#8217;s blog that they are now building all their simple applications using LIghtSwitch.&#160; Also, my friend <a href="http://blogs.msdn.com/b/bethmassi/" target="_blank">Beth Massi</a> has been running around the world espousing the greatness of this product and I knew if I ran into any dumb issues that she’d bail me out (I’m the king of running into dumb issues.&#160; I’ve found that given two choices that seem right, I always pick the wrong one which is what actually happened here along the way, and Beth did bail me out).</p>
<p> <span id="more-1370"></span>
<p>&#160;</p>
<h2>First Blood</h2>
<p>First thing to do (after installing LightSwitch) is to say “File/New/Project”.&#160; My plan is to add this project right off my SV Codecamp solution.&#160; So, here goes.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb.png" width="309" height="180" /></a> </p>
<p>So far so good.&#160; Next step is to choose attach to an external database</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_3.png" width="329" height="113" /></a> </p>
<p>Continuing, but don’t get tripped up here like I did.&#160; You will use WCF RIA Services under the covers but you don’t want to select that choice.&#160; You want to say that you want to connect to a database and let LightSwitch do the work for you.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_4.png" width="341" height="230" /></a> </p>
<p>PIck your database and connection strings.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_5.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_5.png" width="217" height="310" /></a> </p>
<p>Now, pick the tables you plan on working with.&#160; If this were Linq2sql, I’d be choosing them all, but now that I’m in RIA Services land, I’m hoping I can&#160; have separate “Domains” and not have to reference all the tables all the time.&#160; Jury is still out on that one but for now, I’m following the advice of the team and just picking the tables I want to manage now.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_6.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_6.png" width="327" height="114" /></a> </p>
<p>And, I’m going to name the Data Source “svcodecampDataJobs”.&#160; I’ll have to see how this goes and report later.&#160; I’m doing this live so I really don’t know where I’ll end up.</p>
<p>click finish, then rebuild all and it&#160; all works.&#160; It comes up with this screen showing me my relationship between the tables. It is showing me a Company table with a link to a JobListing table which is what I have. Here is what LightSwitch shows me.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_7.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_7.png" width="384" height="384" /></a> </p>
<p>The reality of my database is that I also have a JobListingDates table that is now shown here.&#160; Taking a step backwards to explain my database, I have a simple company table, the company has a detail table associated with it called JobListings, and the JobListings table has a details file associated with it called JobListingsDates.&#160; That is, a company may run an ad for 30 days, take it down for 30,and bring it back up again.</p>
<p>Here is what that schema actually looks like in SqlManager from EMS.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_8.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_8.png" width="370" height="336" /></a> </p>
<p>&#160;</p>
<p>One thing I like about great software is that it has things that are discoverable.&#160; So, just now, I double clicked on the little table called JobListings and the view changed to having JobListing as primary and if you look on the bottom right, it shows JobListingDate.&#160; Very cool.&#160; I have no idea where this is all going but I’m starting to get excited.&#160; Here is what I’m looking at now.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_9.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_9.png" width="377" height="377" /></a> </p>
<p>&#160;</p>
<h2>Building a Screen</h2>
<p>&#160;</p>
<p>So now, let’s push the “Screen” button and see what happens (while looking at the Company View).</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_10.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_10.png" width="323" height="102" /></a> </p>
<p>This is nice, I get a list of sample screens.&#160; How about if we build an Editable Grid Screen with the hope of editing and adding new Companies.&#160; Notice that I’m naming it EditableGridCompany and chosing the Company for the data in the dropdown.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_11.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_11.png" width="362" height="266" /></a></p>
<p>You now get a screen that is a little scary looking so rather than actually try and understand it, I thought “maybe I’m done, maybe this will just run”.&#160; So, here goes, Debug/Start Without Debug.&#160; Here is the scary screen, followed by what happens after the run.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_12.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_12.png" width="399" height="344" /></a> </p>
<p>And the Run:</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_13.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_13.png" width="427" height="303" /></a> </p>
<p>Wow!&#160; Paging, Fancy editing including date pickers, exporting to Excel, Inserts, Updates and Deletes on the company table.&#160; This is amazing.&#160; Let me add another Grid so that I can add JobListings to the company.&#160; To do that, go back to the solution explorer and choose add screen.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_14.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_14.png" width="218" height="244" /></a> </p>
<p>Then again, I have choices.</p>
<p>I choose Details Screen and check Company in the dropdown, Company Details and Company JobListings for the additional data.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_15.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_15.png" width="399" height="292" /></a> </p>
<p>Another intimidating screen, but simply do Debug/Debug Start.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_16.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/BuildingJobAdsManagementWithLightSwitchB_13C17/image_thumb_16.png" width="415" height="229" /></a></p>
<p>&#160;</p>
<p>Well, that’s it for now.&#160; Code Camp is 4 days away and I don’t really have time to take this to the next level.&#160; You can see from the screen at the top of the post that this is pretty amazing for the effort!&#160; I’m sure I’ll be back to this.</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/09/30/building-job-ads-management-module-with-lightswitch-beta-1-for-silicon-valley-code-camp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How To View Your Blob Storage in Windows Azure</title>
		<link>http://peterkellner.net/2010/09/02/viewing-azure-blog-storage-in-visual-studio-2010/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=viewing-azure-blog-storage-in-visual-studio-2010</link>
		<comments>http://peterkellner.net/2010/09/02/viewing-azure-blog-storage-in-visual-studio-2010/#comments</comments>
		<pubDate>Thu, 02 Sep 2010 13:20:35 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[Azure]]></category>
		<category><![CDATA[Azure Blob Storage]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/09/02/viewing-azure-blog-storage-in-visual-studio-2010/</guid>
		<description><![CDATA[I’m just starting to use Windows Azure for a project and plan on using the Azure Blob Storage part.&#160; I won’t go into the details here, but let’s say I figured it out far enough so that I have pushed some piles of data into the blog storage.&#160;&#160; Now, I want to see them.&#160; I [...]]]></description>
			<content:encoded><![CDATA[<p>I’m just starting to use Windows Azure for a project and plan on using the <a href="http://msdn.microsoft.com/en-us/library/dd135733.aspx">Azure Blob Storage</a> part.&#160; I won’t go into the details here, but let’s say I figured it out far enough so that I have pushed some piles of data into the blog storage.&#160;&#160; Now, I want to see them.&#160; I assumed that from the Azure portal, there would be some interface where I could see what I actual did and am being billed from.&#160; I posted to the forums and basically was told that’s not really the case.&#160; Here is the post with the answer saying you need some other software to do it (details below).</p>
<p>My friend <a href="http://robindotnet.wordpress.com/">RobinDotNet</a> suggested I look at his <a href="http://blogs.msdn.com/b/jnak/archive/2010/06/10/windows-azure-storage-browser-in-the-visual-studio-server-explorer.aspx">article</a> about how to use Visual Studio 2010 to view the blob storage.&#160; Sure enough, in VS, I can set my account and key in server explorer and see the blob storage!&#160; Thanks Robin.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/HowToViewYourBlobStorageinWindowsAzure_5925/image.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/HowToViewYourBlobStorageinWindowsAzure_5925/image_thumb.png" width="283" height="232" /></a> </p>
<p> <span id="more-1360"></span>
<p>&#160;</p>
<p><a href="http://social.msdn.microsoft.com/Forums/en-US/windowsazure/thread/eb624e14-9f89-4846-89d5-d17d78c6c87f/">Link to Thread with details below:</a></p>
<p><em>Reply:</em></p>
<p><em>The billing portal will give you a fairly detailed report on your storage consumption (it&#8217;s about 4 hours behind realtime). You can get to this from the Azure portal (should be near the upper-right by your Live ID). This won&#8217;t give you per-container breakdowns or anything that detailed though.</em></p>
<p><em>This Azure Storage Team </em><a href="http://blogs.msdn.com/b/windowsazurestorage/archive/2010/07/09/understanding-windows-azure-storage-billing-bandwidth-transactions-and-capacity.aspx"><em>blog post</em></a><em> gives a fairly detailed breakdown of how consumption is computed. Knowing this, you could create a more detailed storage consumption report than the billing report by enumerating your containers, then enumerating each blob within the containers and reading each blob&#8217;s size (via CloudBlob.Properties.Length).</em></p>
<p><em>There&#8217;s nothing I know of that will give you a bandwidth breakdown for each container. For that, you&#8217;d need to create some sort of service tier, where you could then retrieve the blob in question and accumulate your own stats within that service tier.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/09/02/viewing-azure-blog-storage-in-visual-studio-2010/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Creating New Overloaded Methods in Visual Studio C# is Really Easy with CodeRush</title>
		<link>http://peterkellner.net/2010/07/25/refactor-overload-coderush-visualstudio-2010/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=refactor-overload-coderush-visualstudio-2010</link>
		<comments>http://peterkellner.net/2010/07/25/refactor-overload-coderush-visualstudio-2010/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 01:48:35 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[CodeRush]]></category>
		<category><![CDATA[DevExpress]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/07/25/refactor-overload-coderush-visualstudio-2010/</guid>
		<description><![CDATA[So, you want to add a couple extra parameters to an existing method in Visual Studio, while not having to change all your existing code to call the new method signature?&#160; It’s easy with CodeRush from Devexpress.&#160; In this post, I’ll start with a simple method and add some parameters to it, then do the [...]]]></description>
			<content:encoded><![CDATA[<p>So, you want to add a couple extra parameters to an existing method in <a href="http://www.microsoft.com/visualstudio/en-us/">Visual Studio</a>, while not having to change all your existing code to call the new method signature?&#160; It’s easy with <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">CodeRush</a> from <a href="http://www.devexpress.com/">Devexpress</a>.&#160; In this post, I’ll start with a simple method and add some parameters to it, then do the magic refactor.</p>
<p>Here is the starting code.</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> SynchronizeScopesAsyncStart(  <span style="color: #0000ff">int</span> dbSyncPairId, <span style="color: #0000ff">string</span> schemaName,   <span style="color: #0000ff">string</span> connectionStringLeft,  <span style="color: #0000ff">string</span> scopeLeft, <span style="color: #0000ff">string</span> connectionStringRight,   <span style="color: #0000ff">string</span> scopeRight, <span style="color: #0000ff">bool</span> skipDbLogging)    {...</pre>
<p></div>
<p><span id="more-1344"></span></p>
<p>So, We need to add a couple new parameters as follows.</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> SynchronizeScopesAsyncStart           (<span style="color: #0000ff">int</span> dbSyncPairId, <span style="color: #0000ff">string</span> schemaName,            <span style="color: #0000ff">string</span> connectionStringLeft,            <span style="color: #0000ff">string</span> scopeLeft, <span style="color: #0000ff">string</span> connectionStringRight,            <span style="color: #0000ff">string</span> scopeRight, <span style="color: #0000ff">bool</span> skipDbLogging,            <span style="color: #0000ff">int</span> pauseMsScopeLeft,<span style="color: #0000ff">int</span> pauseMsScopeRight)        {</pre>
<p></div>
<p>So now, we want to create an overloaded method with the two new parameters (pauseMsScopeLeft and pauseMsScopeRight) left out.&#160; </p>
<p>The way we do this is first, hover the cursor over the method name (SynchronizeScopeAsynStart) as follows.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/CreatingNewOverloadedMethodsinCisReallyE_1086D/image.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/CreatingNewOverloadedMethodsinCisReallyE_1086D/image_thumb.png" width="491" height="166" /></a> </p>
<p>Notice the helper message telling us</p>
<blockquote>
<p>Create an overloaded method similar to this one with fewer parameters.</p>
</blockquote>
<p>This is exactly what we want to do so we chose this by pressing enter.&#160; CodeRush now gives us a red arrow pointer telling us where it will put the overloaded method by default as well as giving us some instructions on how to put the method someplace else.&#160; In our case, I’m happy to have it right below where the original method is, so I just press enter.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/CreatingNewOverloadedMethodsinCisReallyE_1086D/image_3.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/CreatingNewOverloadedMethodsinCisReallyE_1086D/image_thumb_3.png" width="519" height="375" /></a> </p>
<p>Now, is the really clever part.&#160; I can simply follow the little helper window and move through the parameter list and exclude parameters I don’t want in the parameter list of my new overloaded method.&#160; In my case, I want to get rid of the last two parameters.&#160; It highlights the last one to start, so I simply press “space” as it says, then I press Shift-Tab to get to the previous parameter, space bar again to exclude that one and I’m almost done.&#160; Here is the screen I’m now looking at.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/CreatingNewOverloadedMethodsinCisReallyE_1086D/image_4.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/CreatingNewOverloadedMethodsinCisReallyE_1086D/image_thumb_4.png" width="536" height="353" /></a> </p>
<p>Finally, I press enter to commit my changes and I have a new overloaded method that looks like the following.</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/CreatingNewOverloadedMethodsinCisReallyE_1086D/image_5.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/CreatingNewOverloadedMethodsinCisReallyE_1086D/image_thumb_5.png" width="562" height="227" /></a> </p>
<p>You can continue with the refactoring and tell CodeRush to “inline” your temporary variables, but I think this is enough for now.</p>
<p>Hope this helps!</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/07/25/refactor-overload-coderush-visualstudio-2010/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Another Nice DevExpress CodeRush Refactoring</title>
		<link>http://peterkellner.net/2010/07/04/coderush-factorings-introduce-lambda-using-statements/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=coderush-factorings-introduce-lambda-using-statements</link>
		<comments>http://peterkellner.net/2010/07/04/coderush-factorings-introduce-lambda-using-statements/#comments</comments>
		<pubDate>Sun, 04 Jul 2010 16:42:26 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[CodeRush]]></category>
		<category><![CDATA[Refactor]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/07/04/coderush-factorings-introduce-lambda-using-statements/</guid>
		<description><![CDATA[For the last few days, I’ve been using DevExpress CodeRush and am finding some very useful refactorings.&#160; Many I’m not blogging about, but there are a few that I really like.&#160; In this post, I’m going to show just two of those refactorings that have been making my code much nicer and easier to write.&#160; [...]]]></description>
			<content:encoded><![CDATA[<p>For the last few days, I’ve been using <a href="http://devexpress.com/">DevExpress</a> <a href="http://devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">CodeRush</a> and am finding some very useful refactorings.&#160; Many I’m not blogging about, but there are a few that I really like.&#160; In this post, I’m going to show just two of those refactorings that have been making my code much nicer and easier to write.&#160; One is the “Introduce Using” refactoring, and the other is “Convert to Lambda Expression”.</p>
<p>Before I go into the details, I’d just like to disclose that when I was first writing the <a href="http://www.siliconvalley-codecamp.com/">Silicon Valley Code Camp</a> web site, I was an asp.net and c# newby.&#160; I’m not claiming wizard status now, but I have to admit that when I go back and look at some of the code I wrote back then (including what I’m showing below before the refactoring), it’s a little embarrassing.&#160; Silicon Valley Code Camp for me as “when I’m not doing real work” web site so I don’t really have the time to go back and clean things up.&#160; Now, with CodeRush, it’s easy to clean things up when I see them with very little effort.</p>
<p> <span id="more-1337"></span><br />
<h2>Introduce Using Statement Refactoring</h2>
<p>Here is the code I wrote 5+ years ago:</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">SqlConnection conn = <span style="color: #0000ff">new</span> SqlConnection(connectionString);conn.Open();

<span style="color: #008000">// First, make a quick list of counts for each id that is unavailable and show</span><span style="color: #008000">// not be shown</span><span style="color: #0000ff">string</span> selectCount = <span style="color: #006080">@&quot;select VistaSlotsId FROM attendees                      WHERE VistaSlotsId &gt;= 2 AND VistaSlotsId &lt;= 5 Group By VistaSlotsId                      HAVING COUNT(*) &gt;= @MaxPerSlot  &quot;</span>;List&lt;<span style="color: #0000ff">int</span>&gt; unavailableSlotsList = <span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">int</span>&gt;();SqlDataReader readerUnavailable = <span style="color: #0000ff">null</span>;SqlCommand cmdUnAvailable = <span style="color: #0000ff">new</span> SqlCommand(selectCount, conn);cmdUnAvailable.Parameters.Add(<span style="color: #006080">&quot;@MaxPerSlot&quot;</span>, SqlDbType.Int).Value = maxPerSlot;readerUnavailable = cmdUnAvailable.ExecuteReader();<span style="color: #0000ff">try</span>{    <span style="color: #0000ff">while</span> (readerUnavailable.Read())    {        <span style="color: #0000ff">int</span> id = readerUnavailable.GetInt32(0);        unavailableSlotsList.Add(id);    }}<span style="color: #0000ff">finally</span>{    <span style="color: #0000ff">if</span> (readerUnavailable != <span style="color: #0000ff">null</span>) readerUnavailable.Close();}</pre>
<p></div>
<p>By placing the cursor over the “conn” on the top line of the code above, CodeRush gives us the following result:</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/AnotherNiceDevExpressCodeRushRefactoring_8873/image.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AnotherNiceDevExpressCodeRushRefactoring_8873/image_thumb.png" width="507" height="233" /></a> </p>
<p>There are actually quite a few things happening here.</p>
<ol>
<li>It is showing us in the top textbox what the using syntax will be </li>
<li>It is crossing out the lines of code it will change </li>
<li>It is teaching us a little about the using statement </li>
</ol>
<p>If I accept the changes, I get a nice refactoring.&#160; I can continue doing this with “using” for SqlCommand and SqlReader.&#160; I did need to move the SqlReader declaration inside the SqlCommand codeblock for this to work.&#160; Here is the final refactored code that took about 10 seconds.&#160; By hand, I’d say it would have taken me 3 minutes and I may have gotten it wrong which is why I would never do it before.&#160; Now, I’m confident I did not break my code.</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">using</span> (SqlConnection conn = <span style="color: #0000ff">new</span> SqlConnection(connectionString)){    conn.Open();    <span style="color: #008000">// First, make a quick list of counts for each id that is unavailable and show</span>    <span style="color: #008000">// not be shown</span>    <span style="color: #0000ff">string</span> selectCount = <span style="color: #006080">@&quot;select VistaSlotsId FROM attendees                                  WHERE VistaSlotsId &gt;= 2 AND VistaSlotsId &lt;= 5 Group By VistaSlotsId                                  HAVING COUNT(*) &gt;= @MaxPerSlot  &quot;</span>;    List&lt;<span style="color: #0000ff">int</span>&gt; unavailableSlotsList = <span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">int</span>&gt;();    <span style="color: #0000ff">using</span> (SqlCommand cmdUnAvailable = <span style="color: #0000ff">new</span> SqlCommand(selectCount, conn))    {        cmdUnAvailable.Parameters.Add(<span style="color: #006080">&quot;@MaxPerSlot&quot;</span>, SqlDbType.Int).Value = maxPerSlot;        <span style="color: #0000ff">using</span> (SqlDataReader readerUnavailable = cmdUnAvailable.ExecuteReader())        {            <span style="color: #0000ff">try</span>            {                <span style="color: #0000ff">while</span> (readerUnavailable.Read())                {                    <span style="color: #0000ff">int</span> id = readerUnavailable.GetInt32(0);                    unavailableSlotsList.Add(id);                }            }            <span style="color: #0000ff">finally</span>            {                <span style="color: #0000ff">if</span> (readerUnavailable != <span style="color: #0000ff">null</span>)                    readerUnavailable.Close();            }        }    }</pre>
<p>&#160; </p></div>
<div>
  </div>
<h2>Compress To Lambda Expression Refactoring</h2>
<p>A lot of the Silicon Valley Code Camp web site was written prior to the introduction of Lamba Expressions.&#160; If you recall, I wrote an MSDN article back in 2006 entitled <a href="http://peterkellner.net/2006/03/13/adding-personalization-via-profiles-to-the-objectdatasource-in-aspnet-20/">Adding Personalization via Profiles to the ObjectDataSource in ASP.NET 2.0.</a>&#160; <a href="http://blogs.tedneward.com/">Ted Neward</a> inspired a construct to cleverly sort the result list using delegates as shown in the screen shot (form that above post).</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/AnotherNiceDevExpressCodeRushRefactoring_8873/image_3.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AnotherNiceDevExpressCodeRushRefactoring_8873/image_thumb_3.png" width="569" height="353" /></a> </p>
<p>&#160;</p>
<p>The code below is similar to the above but not quite the same.&#160; This is how it looks before the refactoring:</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">Comparison&lt;DataObjectSessionsOverview&gt; comparison = <span style="color: #0000ff">null</span>;    <span style="color: #0000ff">switch</span> (sortDataBase)    {        <span style="color: #0000ff">case</span> <span style="color: #006080">&quot;Userfirstname&quot;</span>:            comparison = <span style="color: #0000ff">new</span> Comparison&lt;DataObjectSessionsOverview&gt;(               <span style="color: #0000ff">delegate</span>(DataObjectSessionsOverview lhs, DataObjectSessionsOverview rhs)               {                   <span style="color: #0000ff">return</span> lhs.Userfirstname.CompareTo(rhs.Userfirstname);               }             );            <span style="color: #0000ff">break</span>;</pre>
<p></div>
<p>CodeRush suggests:</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/AnotherNiceDevExpressCodeRushRefactoring_8873/image_4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AnotherNiceDevExpressCodeRushRefactoring_8873/image_thumb_4.png" width="551" height="245" /></a> </p>
<p>Which then give us:</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">switch</span> (sortDataBase){   <span style="color: #0000ff">case</span> <span style="color: #006080">&quot;Userfirstname&quot;</span>:       comparison = (lhs, rhs) =&gt; lhs.Userfirstname.CompareTo(rhs.Userfirstname);       <span style="color: #0000ff">break</span>;</pre>
<p></div>
<p>I like it!</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/07/04/coderush-factorings-introduce-lambda-using-statements/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A Handy Refactoring with CodeRush (InLine Temp)</title>
		<link>http://peterkellner.net/2010/07/02/coderush-refactoring-inline-temp/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=coderush-refactoring-inline-temp</link>
		<comments>http://peterkellner.net/2010/07/02/coderush-refactoring-inline-temp/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 14:34:56 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[CodeRush]]></category>
		<category><![CDATA[Refactor]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/07/02/coderush-refactoring-inline-temp/</guid>
		<description><![CDATA[I’ve recently started using CodeRush with Visual Studio 2010 and am so far very impressed with the convenience it adds to coding.&#160; One thing that is very clear is that the creators of CodeRush are real programmers and look very hard for patterns that us developers are constantly doing.&#160; As I run into these things [...]]]></description>
			<content:encoded><![CDATA[<p>I’ve recently started using <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">CodeRush</a> with <a href="http://msdn.microsoft.com/en-us/vstudio/default.aspx">Visual Studio 2010</a> and am so far very impressed with the convenience it adds to coding.&#160; One thing that is very clear is that the creators of <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">CodeRush</a> are real programmers and look very hard for patterns that us developers are constantly doing.&#160; As I run into these things that get my attention, I plan on blogging them.&#160; Some are just earth shattering, and others, just nice to have.&#160; This particular one is a nice to have.</p>
<p>So, say you have code like this:</p>
<p>&#160;</p>
<div>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">var sessionAttendeeOds =   <span style="color: #0000ff">new</span> SessionAttendeeODS();listSessionAttendees =   sessionAttendeeOds.GetByUsername(Context.User.Identity.Name);</pre>
</div>
<div>&#160;</div>
<p><span id="more-1335"></span></p>
<div>When I first wrote code, I often do it like this thinking that I may have more methods I’m going to call against the instantiated object.&#160; In this case, many years later, I’m looking at the code and want to condense it.&#160; With <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/">CodeRush</a>, it show me three little dots under the variable sessionAttendeeOds as follows.&#160; When I click on these three dots, I get the following screen.</div>
<p><a href="http://peterkellner.net/FilesForWebDownload/AHandyRefactoringwithCodeRushInLineTemp_6A97/image.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AHandyRefactoringwithCodeRushInLineTemp_6A97/image_thumb.png" width="405" height="156" /></a> </p>
<p>When I chose “InlineTemp”, the code changes to the following, make it easier to read.</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">listSessionAttendees =                 <span style="color: #0000ff">new</span> SessionAttendeeODS().GetByUsername(Context.User.Identity.Name);</pre>
<p></div>
<p>Again, this is not a huge deal, but small fixes like this will add up over time and make my code cleaner and more readable.&#160; CodeRush makes it so easy that I’m sure I’ll do this kind of thing more and more.</p>
<p>HTH’s.</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/07/02/coderush-refactoring-inline-temp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A VS2010 Project Made From Post: How to: Host a WCF Service in a Managed Windows Service</title>
		<link>http://peterkellner.net/2010/06/18/wcf-service-in-managed-windows-service-vs2010-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=wcf-service-in-managed-windows-service-vs2010-2</link>
		<comments>http://peterkellner.net/2010/06/18/wcf-service-in-managed-windows-service-vs2010-2/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 14:37:33 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[ASP.NET 4.0]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[VS2010]]></category>
		<category><![CDATA[WCF]]></category>
		<category><![CDATA[Windows Service]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/06/18/wcf-service-in-managed-windows-service-vs2010-2/</guid>
		<description><![CDATA[MSDN has a very nice article on how to create a windows service that hosts a Windows Communication Foundation (WCF) service.&#160; It explains all the details of doing this in a step by step fashion.&#160; One thing that I often find missing from these articles is the actual Visual Studio project that I can download [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://msdn.microsoft.com/en-us/library/ms733069.aspx" target="_blank">MSDN has a very nice article</a> on how to create a windows service that hosts a <a href="http://msdn.microsoft.com/en-us/netframework/aa663324.aspx" target="_blank">Windows Communication Foundation (WCF)</a> service.&#160; It explains all the details of doing this in a step by step fashion.&#160; One thing that I often find missing from these articles is the actual Visual Studio project that I can download and play with.&#160; What I usually do is put that together myself (which I’m sure is the author’s intent).</p>
<p>To save anyone some time who wants to do the same thing, I’ve created a VS2010 project from the example, added a very simple Windows C# console application that consumes the service, as well as made some small changes in a very nice Windows Presentation Foundation (WPF) <a href="http://code.google.com/p/wpf-mvvm-calculator/" target="_blank">calculator project</a> so that the calculator does it operations inside the windows service rather than in the calculator itself.</p>
<p>In this article, I’ve attached the source code (with my small changes and additions) for you to work with and change as you like.</p>
<p> <span id="more-1332"></span>
<p>First, here is the project: </p>
<div style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px" id="scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:d3a970ab-d487-417a-bf52-fd64c206e6d2" class="wlWriterEditableSmartContent">
<p>Visual Studio 2010 Project <a href="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/WCFServiceInManagedWindowsService_3.zip" target="_blank">Project Zip Here</a></p>
</div>
<p>Now, let’s talk about the details</p>
<p>&#160;</p>
<h2>The Visual Studio 2010 Solution Itself</h2>
<p><a href="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_11.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_thumb_9.png" width="490" height="381" /></a> </p>
<p>There are three projects in this solution.&#160; The service itself which is called WCFServiceInmanagedWindowsService, Console Application and Calculator.</p>
<p>&#160;</p>
<h3>WCFServiceInManagedWindowsService Project</h3>
<p>&#160;</p>
<p>This project is really what is taken from the <a href="http://msdn.microsoft.com/en-us/library/ms733069.aspx" target="_blank">MSDN article</a>. It’s got almost no change and is primarily created by following the directions in the article.&#160; There are a couple batch files added for creating and deleting the service itself in the root of that project directory, but that’s about it.&#160; All the code is in a file called service.cs.</p>
<p>To Add the service, go to the “Service Reference”/”Add Service” Dialog and enter the address of the service (you can find it in the app.config file).</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_12.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_thumb_10.png" width="408" height="337" /></a> </p>
<p><a href="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_13.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_thumb_11.png" width="395" height="85" /></a> </p>
<p><a title="http://localhost:8000/ServiceModelSamples/service" href="http://localhost:8000/ServiceModelSamples/service">http://localhost:8000/ServiceModelSamples/service</a></p>
<p>Notice that the methods exposed are Add/Divide/Multiple and Subtract.</p>
<p>To start the actual service, after rebuilding the project, execute the bat file InstallService.bat in the root directory.&#160; Make sure you build the release version because this script installs the service from the release directory. Once started, you will see it in the services application as follows:</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_14.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_thumb_12.png" width="354" height="178" /></a> </p>
<p>Then, start the service by running the command: “net start WCFWindowsServiceSample”&#160; If you get the error: <em>“No connection could be made because the target machine actively refused it 127.0.0.1:8000”,</em> this likely means you did not start your service.</p>
<p>&#160;</p>
<h3>The Console Application</h3>
<p>The Console application is new and very simple. All you have to do is create a new windows c# console project, use the “Add Service” DialWCFWindowsServiceSampleog and point it at&#160; ( <a title="http://localhost:8000/ServiceModelSamples/service" href="http://localhost:8000/ServiceModelSamples/service">http://localhost:8000/ServiceModelSamples/service</a> ) as follows:</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_15.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_thumb_13.png" width="288" height="317" /></a> </p>
<p>Now, you can simply write a console app with the following code and you will be calling the service correctly.&#160; Here is the code:</p>
<p>&#160;</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">class</span> Program    {        <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span>[] args)        {            var calculatorClient = <span style="color: #0000ff">new</span> CalculatorClient();

            var answer = calculatorClient.Add(5, 4);            Console.WriteLine(<span style="color: #006080">&quot;Answer to Adding 5 + 4: {0}&quot;</span>, answer);            Console.ReadKey();

        }    }</pre>
<p></div>
<p>And, when we run it, now surprise, we get 9!</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_16.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_thumb_14.png" width="244" height="120" /></a> </p>
<p>…</p>
<h3>The Calculator Application</h3>
<p>Just to show a real life use of the service, I grabbed the codeplex project <a title="http://code.google.com/p/wpf-mvvm-calculator/" href="http://code.google.com/p/wpf-mvvm-calculator/">http://code.google.com/p/wpf-mvvm-calculator/</a>.</p>
<p><a href="http://code.google.com/p/wpf-mvvm-calculator/"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_17.png" width="372" height="157" /></a> </p>
<p>Then, modifying a small section of code inside (after adding the service reference of course, just like we did in the above console project, we now have a calculator that adds by calling a service for the answer.&#160; Here is the modified code:</p>
<div id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"><span style="color: #0000ff">try</span>           {               <span style="color: #008000">// Establish the connection to the Service</span>               var calculatorClient = <span style="color: #0000ff">new</span> CalculatorClient();               var firstOperand = Convert.ToDouble(FirstOperand);               var secondOperand = Convert.ToDouble(SecondOperand);

               var stopwatch = <span style="color: #0000ff">new</span> Stopwatch();               stopwatch.Start();

               <span style="color: #0000ff">switch</span> (Operation)               {                   <span style="color: #0000ff">case</span> (<span style="color: #006080">&quot;+&quot;</span>):                       result = calculatorClient.Add(firstOperand, secondOperand).ToString();                       <span style="color: #0000ff">break</span>;

                   <span style="color: #0000ff">case</span> (<span style="color: #006080">&quot;-&quot;</span>):                       result = calculatorClient.Subtract(firstOperand, secondOperand).ToString();                       <span style="color: #0000ff">break</span>;

                   <span style="color: #0000ff">case</span> (<span style="color: #006080">&quot;*&quot;</span>):                       result = calculatorClient.Multiply(firstOperand, secondOperand).ToString();                       <span style="color: #0000ff">break</span>;

                   <span style="color: #0000ff">case</span> (<span style="color: #006080">&quot;/&quot;</span>):                       result = calculatorClient.Divide(firstOperand, secondOperand).ToString();                       <span style="color: #0000ff">break</span>;               }

               stopwatch.Stop();</pre>
<p></div>
<p>And, when we run the calculator, it looks like this:</p>
<p><a href="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_18.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://peterkellner.net/FilesForWebDownload/AVS2010ProjectMadeFromPostHowtoHostaWCFS_C58D/image_thumb_15.png" width="333" height="232" /></a>&#160;</p>
<p>&#160;</p>
<h2>Conclusions</h2>
<p>In this post, we simply implemented the source as a <a href="http://www.microsoft.com/visualstudio/en-us">Visual Studio 2010</a> project from the MSDN article on how to build a windows service using WCF.&#160; It’s been pointed out that we are better off using named pipes for this kind of application for better performance, but our purpose here was just to elaborate on the existing application.</p>
<p>Hope this helps.</p>
]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/06/18/wcf-service-in-managed-windows-service-vs2010-2/feed/</wfw:commentRss>
		<slash:comments>21</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 14/36 queries in 0.014 seconds using disk: basic
Content Delivery Network via Amazon Web Services: S3: PetersBlogCDN.s3.amazonaws.com

Served from: peterkellner.net @ 2012-05-23 00:48:51 -->
