<?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; ASP.NET 4.0</title>
	<atom:link href="http://peterkellner.net/category/asp-net-4-0/feed/" rel="self" type="application/rss+xml" />
	<link>http://peterkellner.net</link>
	<description>Microsoft Focussed, JavaScript (ExtJS, SenchaTouch &#38; Windows 8 Metro)</description>
	<lastBuildDate>Tue, 07 Feb 2012 21:14:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Need To Get Static JSON File with POST verb on IIS 7.5?</title>
		<link>http://peterkellner.net/2011/10/30/need-to-get-static-json-file-with-post-verb-on-iis-7-5/</link>
		<comments>http://peterkellner.net/2011/10/30/need-to-get-static-json-file-with-post-verb-on-iis-7-5/#comments</comments>
		<pubDate>Mon, 31 Oct 2011 01:36:18 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[ASP.NET 4.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Handler]]></category>
		<category><![CDATA[Web.Config]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/10/30/need-to-get-static-json-file-with-post-verb-on-iis-7-5/</guid>
		<description><![CDATA[Normally, when we stick a JSON file up on an IIS web server, all we have to do to get to is is to set the Mime type.&#160; One easy way to do it is to add to your web.config the following:  &#160;     &#60;system.webServer&#62;    &#60;validation validateIntegratedModeConfiguration=&#34;false&#34; /&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>Normally, when we stick a JSON file up on an IIS web server, all we have to do to get to is is to set the Mime type.&#160; One easy way to do it is to add to your web.config the following:</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"><span style="color: #0000ff">&lt;</span><span style="color: #800000">system.webServer</span><span style="color: #0000ff">&gt;</span><br />    <span style="color: #0000ff">&lt;</span><span style="color: #800000">validation</span> <span style="color: #ff0000">validateIntegratedModeConfiguration</span><span style="color: #0000ff">=&quot;false&quot;</span> <span style="color: #0000ff">/&gt;</span><br />    <span style="color: #0000ff">&lt;</span><span style="color: #800000">modules</span> <span style="color: #ff0000">runAllManagedModulesForAllRequests</span><span style="color: #0000ff">=&quot;true&quot;</span> <span style="color: #0000ff">/&gt;</span><br />    <span style="color: #0000ff">&lt;</span><span style="color: #800000">staticContent</span><span style="color: #0000ff">&gt;</span><br />      <span style="color: #0000ff">&lt;</span><span style="color: #800000">mimeMap</span> <span style="color: #ff0000">fileExtension</span><span style="color: #0000ff">=&quot;.json&quot;</span> <span style="color: #ff0000">mimeType</span><span style="color: #0000ff">=&quot;application/json&quot;</span>  <span style="color: #0000ff">/&gt;</span><br />    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">staticContent</span><span style="color: #0000ff">&gt;</span><br />    ...</pre>

  <br /></div>

<p>&#160;</p>

<p>This works great as long as the GET verb is used (or just enter the on the url like http://mysite.com/myfile.json).</p>

<p>So, what if you &quot;need” to use the POST keyword.&#160; Say for example, you can not change the JavaScript file to use GET instead of POST to get the file.&#160; I was hoping to find some simple web.config parameter to set but I had no luck.&#160; I even <a href="http://stackoverflow.com/questions/7943270/how-to-serve-json-files-from-iis7-when-request-is-post">posted to StackOverflow</a> and so far, the answers have been less than helpful.&#160; Who knows, maybe by the time you read this, there will be a better answer then my solution which is to write a simple asp.net handler and register it to type <a href="http://json.org">json</a>.</p>

<p>&#160;</p>

<p>So, here is the simpler handler that does the trick:</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"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> JSONHandler : IHttpHandler<br />    {<br /><br />        <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> ProcessRequest(HttpContext context)<br />        {<br />            context.Response.ContentType = <span style="color: #006080">&quot;application/json&quot;</span>;<br />            <span style="color: #0000ff">string</span> output = System.IO.File.ReadAllText(context.Request.PhysicalPath);<br />            context.Response.Write(output);<br /><br />        }<br /><br />        <span style="color: #0000ff">public</span> <span style="color: #0000ff">bool</span> IsReusable<br />        {<br />            get<br />            {<br />                <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;<br />            }<br />        }<br />    }</pre>

  <br /></div>

<p>&#160;</p>

<p>and the associated web.config entries</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">&lt;httpHandlers&gt;<br />     &lt;add verb=<span style="color: #006080">&quot;*&quot;</span> path=<span style="color: #006080">&quot;*.json&quot;</span> validate=<span style="color: #006080">&quot;false&quot;</span> type=<span style="color: #006080">&quot;JSONHandler&quot;</span> /&gt;<br />   &lt;/httpHandlers&gt;<br /> &lt;/system.web&gt;<br /><br /> &lt;system.webServer&gt;<br />   &lt;validation validateIntegratedModeConfiguration=<span style="color: #006080">&quot;false&quot;</span> /&gt;<br />   &lt;modules runAllManagedModulesForAllRequests=<span style="color: #006080">&quot;true&quot;</span> /&gt;<br />   &lt;staticContent&gt;<br />     &lt;mimeMap fileExtension=<span style="color: #006080">&quot;.json&quot;</span> mimeType=<span style="color: #006080">&quot;application/json&quot;</span>  /&gt;<br />   &lt;/staticContent&gt;<br />   &lt;handlers&gt;<br />     &lt;add verb=<span style="color: #006080">&quot;*&quot;</span> path=<span style="color: #006080">&quot;*.json&quot;</span> name=<span style="color: #006080">&quot;JSONHandler&quot;</span> type=<span style="color: #006080">&quot;JSONHandler&quot;</span>/&gt;<br />   &lt;/handlers&gt;<br /> &lt;/system.webServer&gt;</pre>

  <br /></div>

<p>I’m assuming there is a simple way, but for now, this works for me.&#160; Please post a better solution and reference it or just tell me in the comments.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/10/30/need-to-get-static-json-file-with-post-verb-on-iis-7-5/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>How To Safely Display Data From ASP.NET TextField Inputs</title>
		<link>http://peterkellner.net/2011/10/16/how-to-safely-display-data-from-asp-net-textfield-inputs/</link>
		<comments>http://peterkellner.net/2011/10/16/how-to-safely-display-data-from-asp-net-textfield-inputs/#comments</comments>
		<pubDate>Sun, 16 Oct 2011 23:35:00 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[ASP.NET 4.0]]></category>
		<category><![CDATA[Best Practices]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/10/16/how-to-safely-display-data-from-asp-net-textfield-inputs/</guid>
		<description><![CDATA[Let’s say you have a user input field that you want the user to type data into which will be later displayed back to the user.&#160; You don’t want the user putting in their own html or other things (like javascript tags) because that could cause bad things to happen on your page.&#160;   [...]]]></description>
			<content:encoded><![CDATA[<p>Let’s say you have a user input field that you want the user to type data into which will be later displayed back to the user.&#160; You don’t want the user putting in their own html or other things (like javascript tags) because that could cause bad things to happen on your page.&#160; </p>  <p>The easiest thing to do is to set the page attribute to not do request validation</p>  <div id="codeSnippetWrapper">&#160;</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="background-color: #ffff00">&lt;%@ Page Title=&quot;&quot; Language=&quot;C#&quot; MasterPageFile=&quot;~/DefaultNoColumns.master&quot; AutoEventWireup=&quot;true&quot; ValidateRequest=&quot;false&quot;<br />    CodeFile=&quot;SponsorInformationEdit.aspx.cs&quot; Inherits=&quot;SponsorInformationEdit&quot; %&gt;</span></pre>

  <br /></div>

<div id="codeSnippetWrapper">
  <br /></div>

<p>Then, store whatever the user types in the textbox including the nasty things like &lt;script …</p>

<p>When you get around to displaying the data back, simply encode it like this:</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">LabelShortDescription.Text = HttpUtility.HtmlEncode(rec.CompanyDescriptionShort);</pre>

  <br /></div>



<p>Then, if the user put a bold tag in the html, they will get this displayed back:</p>

<p>&#160;</p>

<p><a href="http://peterkellner.net/wp/wp/wp-content/uploads/2011/10/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://peterkellner.net/wp/wp/wp-content/uploads/2011/10/image_thumb2.png" width="402" height="104" /></a></p>

<p>and no harm will occur.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/10/16/how-to-safely-display-data-from-asp-net-textfield-inputs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silicon Valley Code Camp Login Failing with IE10 (Fixed Now)</title>
		<link>http://peterkellner.net/2011/09/20/silicon-valley-code-camp-login-failing-with-ie10-fixed-now/</link>
		<comments>http://peterkellner.net/2011/09/20/silicon-valley-code-camp-login-failing-with-ie10-fixed-now/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 17:10:29 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[.Net 2.0]]></category>
		<category><![CDATA[ASP.NET 2.0]]></category>
		<category><![CDATA[ASP.NET 4.0]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/09/20/silicon-valley-code-camp-login-failing-with-ie10-fixed-now/</guid>
		<description><![CDATA[&#160;  Scott Hanselman posted a while back about a browsers definition problem with some ASP.NET sites that causes problems.&#160; Microsoft has articles on this also.&#160; Having just received my BUILD tablet that includes IE10, I tested the Silicon Valley Code Camp web site and discovered that when you login, then switch to another page, [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>  <p><a href="http://www.hanselman.com/blog/BugAndFixASPNETFailsToDetectIE10CausingDoPostBackIsUndefinedJavaScriptErrorOrMaintainFF5ScrollbarPosition.aspx">Scott Hanselman posted</a> a while back about a browsers definition problem with some ASP.NET sites that causes problems.&#160; Microsoft has <a href="http://support.microsoft.com/kb/2600088">articles</a> on this also.&#160; Having just received my <a href="http://www.buildwindows.com/">BUILD</a> tablet that includes IE10, I tested the <a href="http://www.siliconvalley-codecamp.com/">Silicon Valley Code Camp web site</a> and discovered that when you login, then switch to another page, the login did not stick.&#160; After uploading the two files Scott mentions (<a href="http://msdn.microsoft.com/en-us/library/ms228122.aspx">firefox.browser and ie.browser</a>), the problem goes away.</p>  <p>Thanks Scott for the heads up.</p>  <p>&#160;</p>  <p><a href="http://peterkellner.net/wp/wp/wp-content/uploads/2011/09/image3.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/wp/wp/wp-content/uploads/2011/09/image_thumb2.png" width="657" height="350" /></a></p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/09/20/silicon-valley-code-camp-login-failing-with-ie10-fixed-now/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>With ASP.NET WebForms, How to Keep / Stop a Data Control Like GridView From Retrieving Data</title>
		<link>http://peterkellner.net/2011/08/01/with-asp-net-webforms-how-to-keep-stop-a-data-control-like-gridview-from-retrieving-data/</link>
		<comments>http://peterkellner.net/2011/08/01/with-asp-net-webforms-how-to-keep-stop-a-data-control-like-gridview-from-retrieving-data/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 21:35:00 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[ASP.NET 2.0]]></category>
		<category><![CDATA[ASP.NET 3.5]]></category>
		<category><![CDATA[ASP.NET 4.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[ObjectDataSource]]></category>
		<category><![CDATA[WebForms]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/08/01/with-asp-net-webforms-how-to-keep-stop-a-data-control-like-gridview-from-retrieving-data/</guid>
		<description><![CDATA[The What and Where  Something that may not be obvious is if are creating an asp.net WebForms project and you put a datasource such as SqlDataSource or ObjectDataSource for example on the page, how can you prevent the SqlSelect associated with that datasource from being triggered.  The answer is to set the control’s [...]]]></description>
			<content:encoded><![CDATA[<h2>The What and Where</h2>  <p>Something that may not be obvious is if are creating an asp.net <a href="http://msdn.microsoft.com/en-us/library/ms973868.aspx">WebForms</a> project and you put a datasource such as <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.aspx">SqlDataSource</a> or <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.aspx">ObjectDataSource</a> for example on the page, how can you prevent the SqlSelect associated with that datasource from being triggered.</p>  <p>The answer is to set the control’s visible property to false.&#160; That’s it!</p>  <span id="more-1529"></span>  <p>&#160;</p>  <h2>The Why</h2>  <p>The reason you might want to do this is for a case where you have a public facing web page that might be easily subjected to a denial of service attach.&#160; If that web page is always causing some SqlDataSource to fire, you could easily find your <a href="http://www.microsoft.com/sqlserver/en/us/default.aspx">SqlServer</a> overloaded.&#160; At least by checking to see a user is logged in before firing the sql statement, you buy yourself a little bit of protection.</p>  <p>&#160;</p>  <h2>The Code</h2>  <pre class="csharpcode"><span class="asp">&lt;%@ Page Language=&quot;C#&quot; %&gt;</span>

<span class="kwrd">&lt;!</span><span class="html">DOCTYPE</span> <span class="attr">html</span> <span class="attr">PUBLIC</span> <span class="kwrd">&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span> 
<span class="kwrd">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</span><span class="kwrd">&gt;</span>

<span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span><span class="kwrd">&gt;</span>

    <span class="kwrd">protected</span> <span class="kwrd">void</span> 
        GridView1_SelectedIndexChanged(<span class="kwrd">object</span> sender, EventArgs e)
    {

    }

    <span class="kwrd">protected</span> <span class="kwrd">void</span> Page_Init(<span class="kwrd">object</span> sender, EventArgs e)
    {
        <span class="kwrd">if</span> (!Context.User.Identity.IsAuthenticated)
        {
            GridView1.Visible = <span class="kwrd">false</span>;
        }
    }
<span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span>

<span class="kwrd">&lt;</span><span class="html">html</span> <span class="attr">xmlns</span><span class="kwrd">=&quot;http://www.w3.org/1999/xhtml&quot;</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">head</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">title</span><span class="kwrd">&gt;&lt;/</span><span class="html">title</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">head</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;</span><span class="html">body</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">form</span> <span class="attr">id</span><span class="kwrd">=&quot;form1&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">div</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">asp:SqlDataSource</span> <span class="attr">ID</span><span class="kwrd">=&quot;SqlDataSource1&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> 
            <span class="attr">ConnectionString</span><span class="kwrd">=&quot;&lt;%$ ConnectionStrings:CodeCampSV06 %&gt;&quot;</span> 
            <span class="attr">SelectCommand</span>=
            <span class="kwrd">&quot;SELECT [Id], [Username], [Email] FROM [Attendees] ORDER BY [Id] DESC&quot;</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;/</span><span class="html">asp:SqlDataSource</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;</span><span class="html">asp:GridView</span> <span class="attr">ID</span><span class="kwrd">=&quot;GridView1&quot;</span> <span class="attr">runat</span><span class="kwrd">=&quot;server&quot;</span> <span class="attr">AutoGenerateColumns</span><span class="kwrd">=&quot;False&quot;</span> 
            <span class="attr">DataKeyNames</span><span class="kwrd">=&quot;Id&quot;</span> <span class="attr">DataSourceID</span><span class="kwrd">=&quot;SqlDataSource1&quot;</span> 
            <span class="attr">onselectedindexchanged</span><span class="kwrd">=&quot;GridView1_SelectedIndexChanged&quot;</span><span class="kwrd">&gt;</span>
            <span class="kwrd">&lt;</span><span class="html">Columns</span><span class="kwrd">&gt;</span>
                <span class="kwrd">&lt;</span><span class="html">asp:BoundField</span> <span class="attr">DataField</span><span class="kwrd">=&quot;Id&quot;</span> <span class="attr">HeaderText</span><span class="kwrd">=&quot;Id&quot;</span> <span class="attr">InsertVisible</span><span class="kwrd">=&quot;False&quot;</span> 
                    <span class="attr">ReadOnly</span><span class="kwrd">=&quot;True&quot;</span> <span class="attr">SortExpression</span><span class="kwrd">=&quot;Id&quot;</span> <span class="kwrd">/&gt;</span>
                <span class="kwrd">&lt;</span><span class="html">asp:BoundField</span> <span class="attr">DataField</span><span class="kwrd">=&quot;Username&quot;</span> <span class="attr">HeaderText</span><span class="kwrd">=&quot;Username&quot;</span> 
                    <span class="attr">SortExpression</span><span class="kwrd">=&quot;Username&quot;</span> <span class="kwrd">/&gt;</span>
                <span class="kwrd">&lt;</span><span class="html">asp:BoundField</span> <span class="attr">DataField</span><span class="kwrd">=&quot;Email&quot;</span> <span class="attr">HeaderText</span><span class="kwrd">=&quot;Email&quot;</span> 
                <span class="attr">SortExpression</span><span class="kwrd">=&quot;Email&quot;</span> <span class="kwrd">/&gt;</span>
            <span class="kwrd">&lt;/</span><span class="html">Columns</span><span class="kwrd">&gt;</span>
        <span class="kwrd">&lt;/</span><span class="html">asp:GridView</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">form</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">body</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">html</span><span class="kwrd">&gt;</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>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/08/01/with-asp-net-webforms-how-to-keep-stop-a-data-control-like-gridview-from-retrieving-data/feed/</wfw:commentRss>
		<slash:comments>0</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/</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.&#160; 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 [...]]]></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.&#160; 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><i>Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.</i></h4> </blockquote>  <p>&#160;</p>  <span id="more-1450"></span>  <p>I then tried to set the web.config property that everyone seems to reference on the web as follows:</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">&lt;</span><span style="color: #800000">system.web.extensions</span><span style="color: #0000ff">&gt;</span> <br />    <span style="color: #0000ff">&lt;</span><span style="color: #800000">scripting</span><span style="color: #0000ff">&gt;</span> <br />        <span style="color: #0000ff">&lt;</span><span style="color: #800000">webServices</span><span style="color: #0000ff">&gt;</span> <br />            <span style="color: #0000ff">&lt;</span><span style="color: #800000">jsonSerialization</span> <span style="color: #ff0000">maxJsonLength</span><span style="color: #0000ff">=&quot;2147483645&quot;</span> <span style="color: #ff0000">recursionLimit</span><span style="color: #0000ff">=&quot;100&quot;</span><span style="color: #0000ff">&gt;</span> <br />            <span style="color: #0000ff">&lt;/</span><span style="color: #800000">jsonSerialization</span><span style="color: #0000ff">&gt;</span> <br />        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">webServices</span><span style="color: #0000ff">&gt;</span> <br />        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">scripting</span><span style="color: #0000ff">&gt;</span> <br /><span style="color: #0000ff">&lt;/</span><span style="color: #800000">system.web.extensions</span><span style="color: #0000ff">&gt;</span> <br /><br /></pre>

  <br /></div>

<p>However, I kept getting the same error over and over.&#160; 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://thoughtfulcode.wordpress.com/about/">Brian Reiter</a> to create an override of the JsonResult class.&#160; That actually worked for me, however unfortunately, it does not show the shape of the data that is currently being returned by MVC3.&#160; That is, MVC3 currently returns a JsonResult as follows:</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"><span style="color: #0000ff">return</span> Json(<span style="color: #0000ff">new</span><br />{<br />    total = blobs.Count,<br />    data = blobs.ToList()<br />}, JsonRequestBehavior.AllowGet);</pre>
</div>

<div>&#160;</div>

<div>Brian suggest in his article to return his LargeJsonResult, do the following:</div>

<div>&#160;</div>

<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"><span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> LargeJsonResult() { Data = output, MaxJsonLength = <span style="color: #0000ff">int</span>.MaxValue };<br /></pre>
</div>

<div>&#160;</div>

<div>This does not quite work for what I want.&#160; For my result to work, I needed to modify the result a little and return it as follows:</div>

<div>&#160;</div>

<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">return</span><br />   <span style="color: #0000ff">new</span> LargeJsonResult<br />       {<br />           MaxJsonLength = 20000000,<br />           JsonRequestBehavior = JsonRequestBehavior.AllowGet,<br />           Data = <span style="color: #0000ff">new</span><br />                      {<br />                          total = blobs.Count,<br />                          data = blobs.ToList()<br />                      }<br />       };</pre>

  <br /></div>

<div>&#160;</div>

<div>Not much different, but does solve my problem.</div>

<div>&#160;</div>

<div>Thanks Brian for the nice contribution! 
  <br /></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>1</slash:comments>
		</item>
		<item>
		<title>Installing NuGet on VS2010 (first blood)</title>
		<link>http://peterkellner.net/2011/03/02/installing-nuget-on-vs2010-first-blood/</link>
		<comments>http://peterkellner.net/2011/03/02/installing-nuget-on-vs2010-first-blood/#comments</comments>
		<pubDate>Thu, 03 Mar 2011 00:11:34 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[ASP.NET 4.0]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[NuGet]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/03/02/installing-nuget-on-vs2010-first-blood/</guid>
		<description><![CDATA[So, here are the steps necessary to install NuGet on my windows laptop computer.&#160; I’ve got vs2010 (not sp1) installed, I’ve disabled both Resharper and .net Reflector just to be safe.  First step, is to go to the NuGet.org web site and click on Install.    &#160;  Accept the default:  [...]]]></description>
			<content:encoded><![CDATA[<p>So, here are the steps necessary to install <a href="http://nuget.codeplex.com/">NuGet</a> on my windows laptop computer.&#160; I’ve got vs2010 (not sp1) installed, I’ve disabled both Resharper and .net Reflector just to be safe.</p>  <p>First step, is to go to the <a href="http://nuget.org/">NuGet.org</a> web site and click on Install.</p>  <p><a href="http://nuget.org/"><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/Installing-NuGet-on-VS2010-first-blood_D77F/image.png" width="449" height="234" /></a></p>  <p>&#160;</p>  <p>Accept the default:</p>  <p>&#160;</p>  <p><a href="http://peterkellner.net/FilesForWebDownload/Installing-NuGet-on-VS2010-first-blood_D77F/image_3.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/Installing-NuGet-on-VS2010-first-blood_D77F/image_thumb.png" width="244" height="182" /></a></p>  <p>&#160;</p>  <p>Run Visual Studio 2010 again, then go into Tool/Extension Manager and select “Automatically check for updates to installed extensions” as follows:</p>  <p>&#160;</p>  <p><a href="http://peterkellner.net/FilesForWebDownload/Installing-NuGet-on-VS2010-first-blood_D77F/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/Installing-NuGet-on-VS2010-first-blood_D77F/image_thumb_3.png" width="515" height="398" /></a></p>  <p>&#160;</p>  <p>Then, go to View / Other Windows / Package Manager and you will get the Nu-Get prompt.</p>  <p><a href="http://peterkellner.net/FilesForWebDownload/Installing-NuGet-on-VS2010-first-blood_D77F/image_5.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/Installing-NuGet-on-VS2010-first-blood_D77F/image_thumb_4.png" width="466" height="401" /></a></p>  <p>&#160;</p>  <p>Now, you&#8217; have the NuGet prompt as follows.</p>  <p>&#160;</p>  <p><a href="http://peterkellner.net/FilesForWebDownload/Installing-NuGet-on-VS2010-first-blood_D77F/image_6.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/Installing-NuGet-on-VS2010-first-blood_D77F/image_thumb_5.png" width="420" height="181" /></a></p>    <p>At the PM prompt, to install scaffolding, start typing the following:</p>  <p>Install-Package mvc&#160; {then press tab for auto expansion}</p>  <p>You’ll get a list of all packages that begin with mvc as follows:</p>  <p><a href="http://peterkellner.net/FilesForWebDownload/Installing-NuGet-on-VS2010-first-blood_D77F/image_7.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/Installing-NuGet-on-VS2010-first-blood_D77F/image_thumb_6.png" width="498" height="323" /></a></p>    <p>I choose MvcScaffolding, press enter and.. you’ll get the error that you need to have a project open.&#160; Of course, because it wants to add it to your project.</p>  <p>&#160;</p>  <p><a href="http://peterkellner.net/FilesForWebDownload/Installing-NuGet-on-VS2010-first-blood_D77F/image_8.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/Installing-NuGet-on-VS2010-first-blood_D77F/image_thumb_7.png" width="368" height="379" /></a></p>  <p>&#160;</p>  <p>Now, let me add a real project with a simple class called MyTeam</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> MyTeam<br />    {<br />        <br />        [Key]<br />        <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> ID { get; set; }<br /><br />        [Required]<br />        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Name { get; set; }<br />        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> City { get; set; }<br />        <span style="color: #0000ff">public</span> DateTime Founded { get; set; }<br />    }</pre>

  <br /></div>



<p>Now, when I re-run the last command, I get all my Controllers and Views.</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/Installing-NuGet-on-VS2010-first-blood_D77F/image_9.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/Installing-NuGet-on-VS2010-first-blood_D77F/image_thumb_8.png" width="610" height="251" /></a></p>



<p>Hope this helps, I have a lot more to learn now.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/03/02/installing-nuget-on-vs2010-first-blood/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>SqlAzure and a Best Practices way to deal with the Required Retries on Connections</title>
		<link>http://peterkellner.net/2011/01/21/sqlazure-connection-retry-problem-using-best-practices/</link>
		<comments>http://peterkellner.net/2011/01/21/sqlazure-connection-retry-problem-using-best-practices/#comments</comments>
		<pubDate>Fri, 21 Jan 2011 22:24:44 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[ASP.NET 4.0]]></category>
		<category><![CDATA[Azure]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Connection]]></category>
		<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SqlAzure]]></category>
		<category><![CDATA[Web.Config]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/01/21/sqlazure-connection-retry-problem-using-best-practices/</guid>
		<description><![CDATA[Introduction  If you’ve started using SqlAzure for your SqlServer with your Azure application, you’ve probably discovered that you get a reasonable number of connection failures.&#160; The advice from the Azure team is add retry logic to all your connections to SqlAzure. There is a long discussion posted by the Azure team here.  The [...]]]></description>
			<content:encoded><![CDATA[<h2>Introduction</h2>  <p>If you’ve started using <a href="http://msdn.microsoft.com/en-us/library/ee336279.aspx">SqlAzure</a> for your <a href="http://www.microsoft.com/sqlserver/2008/en/us/r2.aspx">SqlServer</a> with your Azure application, you’ve probably discovered that you get a reasonable number of <a href="http://msdn.microsoft.com/en-us/library/aa175395(v=sql.80).aspx">connection</a> failures.&#160; The advice from the Azure team is add retry logic to all your connections to SqlAzure. There is a long discussion posted by the Azure team <a href="http://blogs.msdn.com/b/bartr/archive/2010/06/18/sql-azure-connection-retry.aspx?utm_source=feedburner&amp;utm_medium=twitter&amp;utm_campaign=Feed%3A+Microsoft%2FMSDN-Blogs+(MSDN+Blogs)">here</a>.</p>  <p>The key paragraph states the problem as follows:</p>  <blockquote>   <p><strong>The Problem       <br /></strong>One of the things that SQL Azure does to deliver high availability is it sometimes closes <a href="http://msdn.microsoft.com/en-us/library/ee336245.aspx">connections</a>. SQL Azure does some pretty cool stuff under the covers to minimize the impact, but this is a key difference in SQL Azure development vs. SQL Server development.</p> </blockquote>  <p>Basically, what this means is that you must be able to deal with connections failing when you call SqlAzure.&#160; Something that all of probably should have been doing forever, but because most of the time SqlServer is running on your local LAN and the likelihood if a connection failing was next to zero unless something else was going terribly wrong.&#160; Certainly not something we had to do on regular basis.&#160; To emphasize that even more, most of the controls built into asp.net that open connections to sqlserver don’t even do this and that’s from Microsoft itself.</p>  <p>The solution proposed in the thread mentioned above basically has you add tons of code to everyplace you access a connection object.&#160; Personally, I don’t like that because I have hundreds if not thousands of places I open connections and inserting tens of thousands of lines of extra new untested code is a little scary.</p>  <p>So, what to do?</p>  <p>Fortunately, another team at Microsoft, known as the <a href="http://blogs.msdn.com/b/appfabriccat">Windows Server AppFabric Customer Advisory Team</a> published a general purpose solution using Extension Methods and some darn clever coding wrote a <a href="http://blogs.msdn.com/b/appfabriccat/archive/2010/12/11/sql-azure-and-entity-framework-connection-fault-handling.aspx">great article and published code</a> including <a href="http://blogs.msdn.com/b/appfabriccat/archive/2010/10/28/best-practices-for-handling-transient-conditions-in-sql-azure-client-applications.aspx">azure examples</a> that solves this problem very elegantly without requiring a lot of changes to your existing code base.</p>  <p>In this article I plan on giving an example and publishing a sample project that uses this code with SqlAzure to solve the connection retry problem.&#160; My goal here is not to simply restate what they published but to simply have a very simple concrete example of using their library.</p>  <span id="more-1429"></span>  <h2>Design Goal</h2>  <p>We have two goals.</p>  <ol>   <li>     <div align="left">Change as little code as possible</div>   </li>    <li>     <div align="left">Log Connection Errors when they happen With Locations</div>   </li>    <li>     <div align="left">Make sure not to trap errors that are NOT connection related such as bad column names</div>   </li> </ol>  <p align="left">&#160;</p>  <h2 align="left">Incorrect Code</h2>  <p align="left">So, this is what the original code looks like that will fail because it does not have connection retry logic:</p>  <p align="left">&#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">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> UsersIdFromUserNameNoConnectionRetry(<span style="color: #0000ff">string</span> userName)<br />{<br />    var retUsersId = 0;<br />    <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> sql =<br />        <span style="color: #006080">@&quot;SELECT Id FROM Users<br />            WHERE Username = @Username&quot;</span>;<br /><br />    <span style="color: #0000ff">using</span> (var sqlConnection = <span style="color: #0000ff">new</span> SqlConnection(<br />            ConfigurationManager.ConnectionStrings[<span style="color: #006080">&quot;CRStorageWebConnectionString&quot;</span>].<br />            ConnectionString))<br />    {<br />        sqlConnection.Open();<br />        <span style="color: #0000ff">using</span> (var sqlCommand = <span style="color: #0000ff">new</span> SqlCommand(sql, sqlConnection))<br />        {<br />            sqlCommand.Parameters.Add(<span style="color: #006080">&quot;@Username&quot;</span>, SqlDbType.NVarChar).Value = userName;<br />            <span style="color: #0000ff">using</span> (var reader = sqlCommand.ExecuteReader())<br />            {<br />                <span style="color: #0000ff">while</span> (reader.Read())<br />                {<br />                    retUsersId = reader.IsDBNull(0) ? 0 : reader.GetInt32(0);<br />                }<br />            }<br />        }<br />    }<br />    <span style="color: #0000ff">return</span> retUsersId;<br />}</pre>

  <br /></div>

<p align="left">So, if there is a connection, an exception will get thrown and will need to be caught, but then the method will not have done it’s job.</p>

<p align="left">&#160;</p>

<h2 align="left">Correct Code With Retries</h2>

<p align="left">So, now take a look at the revised code after the library is setup and used.&#160; The setup is non-trivial, but you just have to do that once and then you can simply fix all your other code with very few changes.&#160; Below is the new code with connection retry logic built in.</p>

<p align="left">&#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">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">int</span> UsersIdFromUserName(<span style="color: #0000ff">string</span> userName)<br />{<br />    var retUsersId = 0;<br />    <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> sql =<br />        <span style="color: #006080">@&quot;SELECT Id FROM Users<br />            WHERE Username = @Username&quot;</span>;<br /><br />    <span style="color: #0000ff">using</span> (var sqlConnection =<br />        <span style="color: #0000ff">new</span> ReliableSqlConnection(<br />            ConfigurationManager.ConnectionStrings[<span style="color: #006080">&quot;CRStorageWebConnectionString&quot;</span>].<br />            ConnectionString,<br />            <span style="color: #0000ff">new</span> RetryUtils(<span style="color: #006080">&quot;&quot;</span>, <span style="color: #006080">&quot;UsersIdFromUserName&quot;</span>).GetRetryPolicy()))<br />    {<br />        sqlConnection.Open();<br />        <span style="color: #0000ff">using</span> (var sqlCommand = <span style="color: #0000ff">new</span> SqlCommand(sql, sqlConnection.Current))<br />        {<br />            sqlCommand.Parameters.Add(<span style="color: #006080">&quot;@Username&quot;</span>, SqlDbType.NVarChar).Value = userName;<br />            <span style="color: #0000ff">using</span> (var reader = sqlCommand.ExecuteReader())<br />            {<br />                <span style="color: #0000ff">while</span> (reader.Read())<br />                {<br />                    retUsersId = reader.IsDBNull(0) ? 0 : reader.GetInt32(0);<br />                }<br />            }<br />        }<br />    }<br />    <span style="color: #0000ff">return</span> retUsersId;<br />}</pre>
</div>

<div>&#160;</div>

<div>There are basically two changes.&#160; </div>

<div>&#160;</div>

<ol>
  <li>The first is instead of create an SqlConnection(…) we are creating a ReliableSqlConnection(…).&#160; The ReliableSqlConnection takes an extra parameter which basically wraps the retry logic used, as well as labelling this connection so when it fails, it gets logged.&#160; In the log, there will be the comment “UsersIdFromUserName” so we know what method threw the retry.</li>

  <li>The second is slight change we when we create the SqlCommand, we have to add the property .Current to it so we know we are talking about the currently executing connection.&#160; There may be a default way to handle this but I could not figure it out.</li>
</ol>

<div>That’s it!&#160; you are new connection safe for retrying failed connections.</div>

<div>&#160;</div>

<h2>The Setup Pieces</h2>

<div>&#160;</div>

<div>First, download the library from the article mention above which can be found here in <a href="http://code.msdn.microsoft.com/">Microsoft’s Code Gallery</a></div>

<div>&#160;</div>

<div><a title="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=appfabriccat&amp;DownloadId=14007" href="http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=appfabriccat&amp;DownloadId=14007">http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=appfabriccat&amp;DownloadId=14007</a></div>

<div>&#160;</div>

<p>The project has all kinds of stuff in it that build quite nicely, run and test under vs2010.&#160; All I’m interested in is the ado.net piece and the retry logic around that.&#160; I actually used the 1.2 version, however I now see there is a 1.3 version with some improvements.&#160; I would post my project but I don’t want to post it with old code so I’ll just tell you the steps I went through so you can do the same.</p>

<h3>Build the class library</h3>

<p>Compile the project and make sure you have the dll from the library lincluded in your actual visual studio project.&#160; The library you want is Microsoft.AppFabricCAT.Samples.Azure.TransientFaultHandling.</p>

<p>&#160;</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/SqlAzure-and-a-Best-Practices-way-to-dea_E1CC/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/SqlAzure-and-a-Best-Practices-way-to-dea_E1CC/image_thumb.png" width="347" height="243" /></a></p>

<p>&#160;</p>

<h3>Update Your Web.config</h3>

<p>Add the config section below to your web.config file.</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">&lt;</span><span style="color: #800000">configSections</span><span style="color: #0000ff">&gt;</span><br />    <span style="color: #0000ff">&lt;</span><span style="color: #800000">section</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;RetryPolicyConfiguration&quot;</span> <br />         <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;Microsoft.AppFabricCAT.Samples.Azure.TransientFaultHandling.Configuration.RetryPolicyConfigurationSettings,<br />            Microsoft.AppFabricCAT.Samples.Azure.TransientFaultHandling&quot;</span> <span style="color: #0000ff">/&gt;</span><br /><span style="color: #0000ff">&lt;/</span><span style="color: #800000">configSections</span><span style="color: #0000ff">&gt;</span><br /><br /><span style="color: #0000ff">&lt;</span><span style="color: #800000">RetryPolicyConfiguration</span> <span style="color: #ff0000">defaultPolicy</span><span style="color: #0000ff">=&quot;FixedIntervalDefault&quot;</span> <span style="color: #ff0000">defaultSqlConnectionPolicy</span><span style="color: #0000ff">=&quot;FixedIntervalDefault&quot;</span> <br />         <span style="color: #ff0000">defaultSqlCommandPolicy</span><span style="color: #0000ff">=&quot;FixedIntervalDefault&quot;</span> <span style="color: #ff0000">defaultStoragePolicy</span><span style="color: #0000ff">=&quot;IncrementalIntervalDefault&quot;</span> <br />         <span style="color: #ff0000">defaultCommunicationPolicy</span><span style="color: #0000ff">=&quot;IncrementalIntervalDefault&quot;</span><span style="color: #0000ff">&gt;</span><br />    <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;FixedIntervalDefault&quot;</span> <span style="color: #ff0000">maxRetryCount</span><span style="color: #0000ff">=&quot;10&quot;</span> <span style="color: #ff0000">retryInterval</span><span style="color: #0000ff">=&quot;100&quot;</span> <span style="color: #0000ff">/&gt;</span><br />    <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;IncrementalIntervalDefault&quot;</span> <span style="color: #ff0000">maxRetryCount</span><span style="color: #0000ff">=&quot;10&quot;</span> <span style="color: #ff0000">retryInterval</span><span style="color: #0000ff">=&quot;100&quot;</span> <span style="color: #ff0000">retryIncrement</span><span style="color: #0000ff">=&quot;50&quot;</span> <span style="color: #0000ff">/&gt;</span><br />    <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;ExponentialIntervalDefault&quot;</span> <span style="color: #ff0000">maxRetryCount</span><span style="color: #0000ff">=&quot;10&quot;</span> <span style="color: #ff0000">minBackoff</span><span style="color: #0000ff">=&quot;100&quot;</span> <span style="color: #ff0000">maxBackoff</span><span style="color: #0000ff">=&quot;1000&quot;</span> <span style="color: #ff0000">deltaBackoff</span><span style="color: #0000ff">=&quot;100&quot;</span> <span style="color: #0000ff">/&gt;</span><br /><span style="color: #0000ff">&lt;/</span><span style="color: #800000">RetryPolicyConfiguration</span><span style="color: #0000ff">&gt;</span></pre>

  <br /></div>

<p>This defines a new section in your web.config, then implements it with several different retry policies include a default one which seems quite reasonable to me.</p>

<p>If you remember, in the implementation section above, we used a public class called RetryUtils.&#160; This class is actually one I invented as a convenience class to minimize the code I have to update on each use of the ReliableConnection Object.&#160; Just to refresh your memory, the implementation is this:</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> (var sqlConnection =<br />                <span style="color: #0000ff">new</span> ReliableSqlConnection(<br />                    ConfigurationManager.ConnectionStrings[<span style="color: #006080">&quot;CRStorageWebConnectionString&quot;</span>].<br />                    ConnectionString,<br />                    <span style="color: #0000ff">new</span> RetryUtils(<span style="color: #006080">&quot;&quot;</span>, <span style="color: #006080">&quot;UsersIdFromUserName&quot;</span>).GetRetryPolicy()))</pre>

  <br /></div>

<p>The actual code fo rthe class RetryUtils is below here.&#160; You’ll have to stick this someplace in your project.</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"><span style="color: #0000ff">namespace</span> Utils<br />{<br />   <br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> RetryUtils<br />    {<br />        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> Username { get; set; }<br />        <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> CallersName { get; set; }<br /><br />        <span style="color: #0000ff">public</span> RetryUtils(<span style="color: #0000ff">string</span> username, <span style="color: #0000ff">string</span> callersName)<br />        {<br />            Username = username;<br />            CallersName = callersName;<br />        }<br /><br />        <span style="color: #0000ff">public</span> RetryUtils()<br />        {<br />            Username = <span style="color: #0000ff">string</span>.Empty;<br />            CallersName = <span style="color: #0000ff">string</span>.Empty;<br />        }<br /><br />        <span style="color: #0000ff">public</span> RetryPolicy&lt;SqlAzureTransientErrorDetectionStrategy&gt; GetRetryPolicy()<br />        {<br />            var retryPolicy = <span style="color: #0000ff">new</span> RetryPolicy&lt;SqlAzureTransientErrorDetectionStrategy&gt;<br />                 (RetryPolicy.DefaultClientRetryCount, RetryPolicy.DefaultMinBackoff,<br />                  RetryPolicy.DefaultMaxBackoff,<br />                  RetryPolicy.DefaultClientBackoff);<br /><br />            retryPolicy.RetryOccurred += RetryPolicyRetryOccurred;<br /><br />            <span style="color: #0000ff">return</span> retryPolicy;<br />        }<br /><br />        <span style="color: #0000ff">void</span> RetryPolicyRetryOccurred(<span style="color: #0000ff">int</span> currentRetryCount, Exception lastException, TimeSpan delay)<br />        {<br />            GeneralUtils.GetLog4NetAllDataContext().AddLog4NetAll(<span style="color: #0000ff">new</span> Log4NetAll<br />                                                    {<br />                                                        Date = DateTime.UtcNow,<br />                                                        EllapsedTime = 0,<br />                                                        ExceptionStackTrace = lastException.StackTrace,<br />                                                        Message = <span style="color: #006080">&quot;RetryCount: &quot;</span> + currentRetryCount + <span style="color: #006080">&quot; delay: &quot;</span> + delay.Seconds,<br />                                                        ExceptionMessage = lastException.Message,<br />                                                        Logger = <span style="color: #006080">&quot;&quot;</span>,<br />                                                        Level = <span style="color: #006080">&quot;Error&quot;</span>,<br />                                                        UserName = Username,<br />                                                        PartitionKey = Username,<br />                                                        RowKey = Guid.NewGuid().ToString()<br />                                                    });<br />        }       <br />    }<br />}<br /></pre>
</div>

<div>This code actually has the nice callback delegate that does the logging when a retry actually occurs.&#160; I’m not including my implementation of GeneralUtils.GetLog4NetAllDataContext().AddLog4NetAll, but you can pretty much guess what it does.&#160; It simply logs the retry with all the details of what happened.&#160; My implementation sticks this in an Azure Table, but that’s really for another post.</div>

<div>&#160;</div>

<h2>Non Connection Related Errors</h2>

<div>&#160;</div>

<div>Remember, our second design criteria is that we should only fail on errors that are connection related and not things like data column not found.&#160; With no additional work, this library takes care of this for us.&#160; Actually, in the release notes for the 1.3 release (which I have not used yet) say they have improved that feature by adding additional codes not to fail on.&#160; That is, the last thing you want is your code spending 5 minutes retrying on a problem you’d just like reported immediately. </div>

<div>&#160;</div>

<div>Keep in mind that the library we are using here is a general purpose retry library not designed just for use with ado.net.&#160; The team has provided us with examples using LINQ2SQL, EntityFramework and other technologies.&#160; I spent&#160; a little time reading about using those other technologies but did not get far enough to blog about it.&#160; My current SqlAzure implementation uses 100% ado.net because performance is critical to me and neither EF or LINQ2SQL are quite up to my task yet.</div>

<div>&#160;</div>

<h2>Conclusions</h2>

<div>&#160;</div>

<div>So basically, that’s it!&#160; You know have the tools to implement very nice retry logic in your ado.net code so Azure will not fail on “normal” connection failures.&#160; I suggest that if you are using SqlAzure, you implement this as soon as possible so your code will be solid going forward.</div>

<div>&#160;</div>

<div>Thanks for reading!</div>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/01/21/sqlazure-connection-retry-problem-using-best-practices/feed/</wfw:commentRss>
		<slash:comments>3</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/</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[VS2010]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[Visual Studio 2010]]></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> <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<br />    {<br />        <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> Main(<span style="color: #0000ff">string</span>[] args)<br />        {<br />            var calculatorClient = <span style="color: #0000ff">new</span> CalculatorClient();<br /><br />            var answer = calculatorClient.Add(5, 4);<br />            Console.WriteLine(<span style="color: #006080">&quot;Answer to Adding 5 + 4: {0}&quot;</span>, answer);<br />            Console.ReadKey();<br /><br /><br />            <br />        }<br />    }</pre>

  <br /></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><br />           {<br />               <span style="color: #008000">// Establish the connection to the Service</span><br />               var calculatorClient = <span style="color: #0000ff">new</span> CalculatorClient();<br />               var firstOperand = Convert.ToDouble(FirstOperand);<br />               var secondOperand = Convert.ToDouble(SecondOperand);<br /><br /><br /><br />               var stopwatch = <span style="color: #0000ff">new</span> Stopwatch();<br />               stopwatch.Start();<br /><br />               <span style="color: #0000ff">switch</span> (Operation)<br />               {<br />                   <span style="color: #0000ff">case</span> (<span style="color: #006080">&quot;+&quot;</span>):<br />                       result = calculatorClient.Add(firstOperand, secondOperand).ToString();<br />                       <span style="color: #0000ff">break</span>;<br /><br />                   <span style="color: #0000ff">case</span> (<span style="color: #006080">&quot;-&quot;</span>):<br />                       result = calculatorClient.Subtract(firstOperand, secondOperand).ToString();<br />                       <span style="color: #0000ff">break</span>;<br /><br />                   <span style="color: #0000ff">case</span> (<span style="color: #006080">&quot;*&quot;</span>):<br />                       result = calculatorClient.Multiply(firstOperand, secondOperand).ToString();<br />                       <span style="color: #0000ff">break</span>;<br /><br />                   <span style="color: #0000ff">case</span> (<span style="color: #006080">&quot;/&quot;</span>):<br />                       result = calculatorClient.Divide(firstOperand, secondOperand).ToString();<br />                       <span style="color: #0000ff">break</span>;<br />               }<br /><br />               stopwatch.Stop();</pre>

  <br /></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>18</slash:comments>
		</item>
		<item>
		<title>Is There A Need For End-To-End ExtJS to Microsoft Server (MVC-C#, LOB) 4 Day Class? (Poll Enclosed)</title>
		<link>http://peterkellner.net/2010/05/04/lob-training-poll-using-extjs-dotnet-aspnet-mvc/</link>
		<comments>http://peterkellner.net/2010/05/04/lob-training-poll-using-extjs-dotnet-aspnet-mvc/#comments</comments>
		<pubDate>Wed, 05 May 2010 02:54:13 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[ASP.NET 4.0]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[ExtJS]]></category>
		<category><![CDATA[LINQ to SQL]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[OData]]></category>
		<category><![CDATA[Training]]></category>

		<guid isPermaLink="false">http://peterkellner.net/?p=1289</guid>
		<description><![CDATA[Over the past couple years, the focus of the web development I’ve been doing involves building highly flexible, highly scalable and straight forward web sites to implement and maintain Line of Business (LOB) applications.&#160; As you can probably tell from my posts, I’m very “practical” focused, and at the same time have a desire to [...]]]></description>
			<content:encoded><![CDATA[<p>Over the past couple years, the focus of the web development I’ve been doing involves building highly flexible, highly scalable and straight forward web sites to implement and maintain <a href="http://en.wikipedia.org/wiki/Line_of_business">Line of Business</a> (LOB) applications.&#160; As you can probably tell from my posts, I’m very “practical” focused, and at the same time have a desire to build awesome web applications.</p>  <p>The technology pairing I’ve chosen is <a href="http://www.asp.net/mvc/whatisaspmvc/">Microsoft’s .Net platform with MVC</a> on the server, and <a href="http://www.extjs.com/">ExtJS</a> on the client.&#160; Though it’s possible to still use ExtJS with standard html/aspx pages, I’ve found the best combination is to use 100% <a href="http://www.javascript.com/">JavaScript</a> on the client (ExtJS) and have all the server side technology be 100% service based.&#160; I’ve used <a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx">LINQ2SQL</a> extensively as well as <a href="http://msdn.microsoft.com/en-us/magazine/ee336128.aspx">Entity Framework</a> in the latest <a href="http://msdn.microsoft.com/en-us/vstudio/default.aspx">Visual Studio 2010</a> release.</p>  <p>The learning curve was quite steep to actually be able to efficiently build highly flexible, highly scalable applications using these technologies, but now that I know it, I wouldn’t have it any other way.</p>  <p>I’m considering putting together a series of <strong>4 Day Classes</strong> around the country (or even world) that would basically teach people the methods and patterns I’ve learned and essentially leap frog a development team into being able to quickly do what it has taken me years to figure out.&#160; I’ve been fortunate enough to know the top 1% instructors and I’m sure with the right incentive, can get them to join me in both putting together these classes as well as teaching them.</p> <span id="more-1289"></span>  <p>So, my question is:</p> Note: There is a poll embedded within this post, please visit the site to participate in this post's poll.   <p>I really appreciate your feedback.&#160; If I go down this path, it will be a lot of work and it would be nice to know if people are really interested.&#160; I know for myself, I&#8217; would have gone to something like this 2 years ago and it would have saved me a lot of time.</p>  <p>Thanks for your feedback.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/05/04/lob-training-poll-using-extjs-dotnet-aspnet-mvc/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)
Database Caching 4/11 queries in 0.003 seconds using disk

Served from: peterkellner.net @ 2012-02-10 04:23:06 -->
