<?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; C#</title>
	<atom:link href="http://peterkellner.net/category/c/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>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/</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 [...]]]></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<br />{<br />    [Key]<br />    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Id { get; set; }<br /><br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">virtual</span> List&lt;EmailAddressInfo&gt; EmailAddressInfoList { get; set; }<br />    ..<br />}<br /><br /><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> EmailAddressInfo<br />{<br />    [Key]<br />    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Id { get; set; }<br /><br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> EmailAddress { get; set; }<br /><br />}<br /></pre>

  <br /></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<br />{<br />    [Key]<br />    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Id { get; set; }<br />    ...<br />}</pre>

  <br /></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<br />                            select data);<br />                <span style="color: #0000ff">foreach</span> (var rec <span style="color: #0000ff">in</span> recs)<br />                {<br />                    <span style="color: #0000ff">foreach</span> (var email <span style="color: #0000ff">in</span> rec.EmailAddressInfoList)<br />                    {<br />                        Console.WriteLine(email);<br />                    }<br />                }</pre>

  <br /></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://peterkellner.net/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://peterkellner.net/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>)<br />            select data);</pre>

  <br /></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>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>Using ASP.NET Web Forms, Passing Primary Key to RowCommand Event</title>
		<link>http://peterkellner.net/2011/09/05/using-asp-net-web-forms-passing-primary-key-to-rowcommand-event/</link>
		<comments>http://peterkellner.net/2011/09/05/using-asp-net-web-forms-passing-primary-key-to-rowcommand-event/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 15:01:54 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[DataBinding]]></category>
		<category><![CDATA[GridView]]></category>
		<category><![CDATA[WebForms]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/09/05/using-asp-net-web-forms-passing-primary-key-to-rowcommand-event/</guid>
		<description><![CDATA[&#160;  I know this is old hat, but after recently having a conversation with one of the ASP.NET Team guys about how easy Web Forms is to use, I thought I’d just post a simple example of that.&#160; Here is the scenario.&#160; You have a simple GridView with just three columns (Select,Id,TagName).&#160; You want [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>  <p>I know this is old hat, but after recently having a conversation with one of the <a href="http://www.asp.net/">ASP.NET</a> Team guys about how easy <a href="http://www.asp.net/web-forms">Web Forms</a> is to use, I thought I’d just post a simple example of that.&#160; Here is the scenario.&#160; You have a simple <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.aspx">GridView</a> with just three columns (Select,Id,TagName).&#160; You want to be able to capture what happens when the user pressed the Select command and capture the Id.&#160; It looks like this:</p>  <p>&#160;</p>  <p><a href="http://peterkellner.net/wp/wp/wp-content/uploads/2011/09/image1.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/wp/wp/wp-content/uploads/2011/09/image_thumb.png" width="196" height="244" /></a></p>  <p>&#160;</p>  <p>And has an aspx page like this:</p>  <p>&#160;</p>  <div id="codeSnippetWrapper">   <div 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">asp:GridView</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;GridViewNotSelected&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span> <span style="color: #ff0000">AllowSorting</span><span style="color: #0000ff">=&quot;True&quot;</span> <br />        <span style="color: #ff0000">AutoGenerateColumns</span><span style="color: #0000ff">=&quot;False&quot;</span> <br />        <span style="color: #ff0000">DataSourceID</span><span style="color: #0000ff">=&quot;SqlDataSourceTagsNotSelected&quot;</span> <br />        <span style="color: #ff0000">onrowcommand</span><span style="color: #0000ff">=&quot;GridViewNotSelected_RowCommand&quot;</span><span style="color: #0000ff">&gt;</span><br />    <span style="color: #0000ff">&lt;</span><span style="color: #800000">Columns</span><span style="color: #0000ff">&gt;</span><br />        <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:TemplateField</span> <span style="color: #ff0000">ShowHeader</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">ItemTemplate</span><span style="color: #0000ff">&gt;</span><br />                <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:LinkButton</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;LinkButton1&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span> <br />                    <span style="color: #ff0000">CausesValidation</span><span style="color: #0000ff">=&quot;False&quot;</span> <span style="color: #ff0000">CommandArgument</span><span style="color: #0000ff">='&lt;%# Eval(&quot;Id&quot;) %&gt;'</span><br />                    <span style="color: #ff0000">CommandName</span><span style="color: #0000ff">=&quot;Select&quot;</span> <span style="color: #ff0000">Text</span><span style="color: #0000ff">=&quot;Select&quot;</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">asp:LinkButton</span><span style="color: #0000ff">&gt;</span><br />            <span style="color: #0000ff">&lt;/</span><span style="color: #800000">ItemTemplate</span><span style="color: #0000ff">&gt;</span><br />        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">asp:TemplateField</span><span style="color: #0000ff">&gt;</span><br />        <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:TemplateField</span> <br />            <span style="color: #ff0000">HeaderText</span><span style="color: #0000ff">=&quot;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&amp;amp;nbsp;&quot;</span><span style="color: #0000ff">&gt;</span><br />        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">asp:TemplateField</span><span style="color: #0000ff">&gt;</span><br />        <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:TemplateField</span><span style="color: #0000ff">&gt;</span><br />            <span style="color: #0000ff">&lt;</span><span style="color: #800000">ItemTemplate</span><span style="color: #0000ff">&gt;</span><br />                <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:Label</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;Label1&quot;</span> <span style="color: #ff0000">Text</span><span style="color: #0000ff">='&lt;%# Eval(&quot;TagName&quot;) %&gt;'</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">asp:Label</span><span style="color: #0000ff">&gt;</span><br />            <span style="color: #0000ff">&lt;/</span><span style="color: #800000">ItemTemplate</span><span style="color: #0000ff">&gt;</span><br />        <span style="color: #0000ff">&lt;/</span><span style="color: #800000">asp:TemplateField</span><span style="color: #0000ff">&gt;</span><br />    <span style="color: #0000ff">&lt;/</span><span style="color: #800000">Columns</span><span style="color: #0000ff">&gt;</span><br /><span style="color: #0000ff">&lt;/</span><span style="color: #800000">asp:GridView</span><span style="color: #0000ff">&gt;</span></pre>

    <br /></div>

  <br /></div>

<p>Notice that in the <a href="http://weblogs.asp.net/scottgu/archive/2006/06/04/Supporting-Templates-with-ASP.NET-User-Controls.aspx">TemplateField</a> there is LinkButton with a <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx">CommandArgument</a> that simply extracts the TagName with the current Id of the <a href="http://msdn.microsoft.com/en-us/library/dz12d98w.aspx">SqlDataSource</a>.&#160; Now, all you need is the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand.aspx">RowCommand</a> event and you are done.&#160; Here it is in <a href="http://msdn.microsoft.com/en-us/vcsharp/aa336706">C#.</a></p>

<p>&#160;</p>

<div 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">protected</span> <span style="color: #0000ff">void</span> GridViewNotSelected_RowCommand(<span style="color: #0000ff">object</span> sender, GridViewCommandEventArgs e)<br />{<br />    <span style="color: #0000ff">int</span> tagsId = Convert.ToInt32(e.CommandArgument);<br />}</pre>

  <br /></div>

<p>And finally, the proof!</p>

<p>&#160;</p>

<p><a href="http://peterkellner.net/wp/wp/wp-content/uploads/2011/09/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://peterkellner.net/wp/wp/wp-content/uploads/2011/09/image_thumb1.png" width="627" height="118" /></a></p>

<p>&#160;</p>

<p>Very very simple.&#160; HTH’s.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/09/05/using-asp-net-web-forms-passing-primary-key-to-rowcommand-event/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting ExtJS 4 Date Format To Behave Properly in Grid Panel with ASP.NET MVC3</title>
		<link>http://peterkellner.net/2011/08/24/getting-extjs-4-date-format-to-behave-properly-in-grid-panel-with-asp-net-mvc3/</link>
		<comments>http://peterkellner.net/2011/08/24/getting-extjs-4-date-format-to-behave-properly-in-grid-panel-with-asp-net-mvc3/#comments</comments>
		<pubDate>Wed, 24 Aug 2011 18:52:44 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[DateFormat]]></category>
		<category><![CDATA[MVC3]]></category>
		<category><![CDATA[Sencha]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/08/24/getting-extjs-4-date-format-to-behave-properly-in-grid-panel-with-asp-net-mvc3/</guid>
		<description><![CDATA[&#160;

Introduction and Problem

Converting Data Formats back and forth between browser and back end servers can be a little confusing.&#160; In my case, I have a simple Sencha&#160;ExtJS 4.0 form that the user types a date into.&#160; That date gets passed through an ajax request to the Microsoft asp.net controller, then that dates updates the SqlServer [...]]]></description>
			<content:encoded><![CDATA[<p>&#160;</p>

<h3>Introduction and Problem</h3>

<p>Converting Data Formats back and forth between browser and back end servers can be a little confusing.&#160; In my case, I have a simple <a href="http://www.sencha.com/">Sencha</a>&#160;<a href="http://www.sencha.com/products/extjs/">ExtJS 4.0</a> form that the user types a date into.&#160; That date gets passed through an <a href="http://www.w3schools.com/ajax/default.asp">ajax request</a> to the <a href="http://www.microsoft.com/en-us/default.aspx">Microsoft</a> <a href="http://www.asp.net/">asp.net</a> <a href="http://www.asp.net/mvc/mvc3">controller</a>, then that dates updates the <a href="http://en.wikipedia.org/wiki/Microsoft_SQL_Server">SqlServer</a> Database.&#160; When the Date is displayed on a Grid Panel, again, an ajax request is made that grabs the date from the controller, then formats it using the MS <a href="http://dev.sencha.com/deploy/ext-4.0.2a/docs/#/api/Ext.data.Field-cfg-dateFormat">dateFormat</a> from ExtJS and hopefully, the same date the user typed in gets re-displayed.</p>

<div>
  <div class="peterkellnerpromo"><a href="/open-source-javascript-extjs-and-jquery/" target="_blank">If you have a problem like this involving ASP.NET or Sencha Tools (ExtJS, SenchaTouch), more information about our consulting services are <span>here.</span></a></div>
</div>

<p>So, let’s look at the details.</p>

<h3>Posting the Date To the Server (View From JavaScript)</h3>

<p>First, we need to define an ExtJS Model that has the date in it.&#160; That codes looks like this in my case.</p>

<span id="more-1556"></span>

<p>&#160;</p>

<div 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">Ext.define(<span style="color: #006080">'EvMgr.model.SponsorListJobListing'</span>, {<br />    extend: <span style="color: #006080">'Ext.data.Model'</span>,<br />    fields: [<br />        <span style="color: #006080">'Id'</span>,<br />        <span style="color: #006080">'SponsorListId'</span>,<br />        <span style="color: #006080">'JobName'</span>,<br />        <span style="color: #006080">'JobLocation'</span>,<br />        <span style="color: #006080">'JobURL'</span>,<br />        <span style="color: #006080">'JobBrief'</span>,<br />        <span style="color: #006080">'JobTagline'</span>,<br />        <span style="color: #006080">'JobButtonToolTip'</span>,<br />        { name: <span style="color: #006080">'EnteredDate'</span>, type: <span style="color: #006080">'date'</span>, dateFormat: <span style="color: #006080">'MS'</span> },<br />        <span style="color: #006080">'JobCompanyName'</span>,<br />        { name: <span style="color: #006080">'StartRunDate'</span>, type: <span style="color: #006080">'date'</span>, dateFormat: <span style="color: #006080">'MS'</span> },<br />        { name: <span style="color: #006080">'EndRunDate'</span>, type: <span style="color: #006080">'date'</span>, dateFormat: <span style="color: #006080">'MS'</span> },<br />        { name: <span style="color: #006080">'HideListing'</span>, type: <span style="color: #006080">'boolean'</span> },<br />        <span style="color: #006080">'Notes'</span><br />    ],<br />    idProperty: <span style="color: #006080">'Id'</span>,<br /><br />    proxy: {<br />        type: <span style="color: #006080">'ajax'</span>,<br />        api: {<br />            read: <span style="color: #006080">'SponsorListJobListing/Get'</span>,<br />            update: <span style="color: #006080">'SponsorListJobListing/Update'</span>,<br />            create: <span style="color: #006080">'SponsorListJobListing/Create'</span>,<br />            destroy: <span style="color: #006080">'SponsorListJobListing/Delete'</span><br />        },<br />        reader: {<br />            type: <span style="color: #006080">'json'</span>,<br />            root: <span style="color: #006080">'Data'</span>,<br />            successProperty: <span style="color: #006080">'success'</span><br />        }<br />    },<br /><br /><br />    belongsTo: <span style="color: #006080">'EvMgr.model.SponsorList'</span><br />});</pre>

  <br /></div>

<p>Notice the StartRunDate field has dateFormat of ‘MS”.</p>

<p>Now, we have an ExtJS Window that we use to collect the data from the user.&#160; The code is as follows:</p>

<div>
  <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">Ext.define(<span style="color: #006080">'EvMgr.view.sponsorlist.ListJobListingsEdit'</span>, {<br />    extend: <span style="color: #006080">'Ext.window.Window'</span>,<br />    alias: <span style="color: #006080">'widget.sponsorlistjoblistingsedit'</span>,<br />    title: <span style="color: #006080">'Edit Job Listing'</span>,<br />    layout: <span style="color: #006080">'fit'</span>,<br />    width: 1000,<br />    autoShow: <span style="color: #0000ff">true</span>,<br /><br />    initComponent: <span style="color: #0000ff">function</span>() {<br /><br />        <span style="color: #0000ff">this</span>.items = [<br />            {<br />                xtype: <span style="color: #006080">'form'</span>,<br /><br />                items: [<br />                    {<br />                        xtype: <span style="color: #006080">'textfield'</span>,<br />                        name: <span style="color: #006080">'JobName'</span>,<br />                        fieldLabel: <span style="color: #006080">'JobName'</span>,<br />                        width: 850<br />                    },<br />                    {<br />                        xtype: <span style="color: #006080">'textfield'</span>,<br />                        name: <span style="color: #006080">'JobLocation'</span>,<br />                        fieldLabel: <span style="color: #006080">'JobLocation'</span>,<br />                        width: 850<br />                    },<br />                     {<br />                         xtype: <span style="color: #006080">'textfield'</span>,<br />                         name: <span style="color: #006080">'JobURL'</span>,<br />                         fieldLabel: <span style="color: #006080">'JobURL'</span>,<br />                         width: 850<br />                     },<br />                     {<br />                         xtype: <span style="color: #006080">'textfield'</span>,<br />                         name: <span style="color: #006080">'JobBrief'</span>,<br />                         fieldLabel: <span style="color: #006080">'JobBrief'</span>,<br />                         width: 850<br />                     }, {<br />                         xtype: <span style="color: #006080">'textfield'</span>,<br />                         name: <span style="color: #006080">'JobTagline'</span>,<br />                         fieldLabel: <span style="color: #006080">'JobTagline'</span>,<br />                         width: 850<br />                     },<br />                     {<br />                         xtype: <span style="color: #006080">'textfield'</span>,<br />                         name: <span style="color: #006080">'JobButtonToolTip'</span>,<br />                         fieldLabel: <span style="color: #006080">'JobButtonToolTip'</span>,<br />                         width: 850<br />                     },<br />                     {<br />                         xtype: <span style="color: #006080">'textfield'</span>,<br />                         name: <span style="color: #006080">'JobCompanyName'</span>,<br />                         fieldLabel: <span style="color: #006080">'JobCompanyName'</span>,<br />                         width: 850<br />                     },<br />                     {<br />                         xtype: <span style="color: #006080">'textfield'</span>,  <span style="color: #008000">// checkbox makesthe value not come back for some reason</span><br />                         name: <span style="color: #006080">'HideListing'</span>,<br />                         fieldLabel: <span style="color: #006080">'HideListing'</span>,<br />                         width: 850<br />                     },<br />                     {<br />                         xtype: <span style="color: #006080">'datefield'</span>,<br />                         name: <span style="color: #006080">'StartRunDate'</span>,<br />                         fieldLabel: <span style="color: #006080">'StartRunDate'</span>,<br />                         width: 850<br />                     },<br />                     {<br />                         xtype: <span style="color: #006080">'datefield'</span>,<br />                         name: <span style="color: #006080">'EndRunDate'</span>,<br />                         fieldLabel: <span style="color: #006080">'EndRunDate'</span>,<br />                         width: 850<br />                     },<br />                    {<br />                        xtype: <span style="color: #006080">'textfield'</span>,<br />                        name: <span style="color: #006080">'SponsorListId'</span>,<br />                        fieldLabel: <span style="color: #006080">'SponsorListId'</span>,<br />                        width: 850<br />                    }<br />                ]<br />            }<br />        ];<br /><br />        <span style="color: #0000ff">this</span>.buttons = [<br />            {<br />                text: <span style="color: #006080">'Save'</span>,<br />                action: <span style="color: #006080">'save'</span><br />            }, {<br />                text: <span style="color: #006080">'Cancel'</span>,<br />                scope: <span style="color: #0000ff">this</span>,<br />                handler: <span style="color: #0000ff">this</span>.close<br />            }<br />        ];<br /><br />        <span style="color: #0000ff">this</span>.callParent(arguments);<br />    }<br />});</pre>
</div>

<div>Notice we don’t have to mention the StartRunDate’s format because the model has that already.</div>

<div>&#160;</div>

<div>When we run the app and press the “save” button, the actual post that goes back to the asp.net MVC3 controller looks like the following (notice the screen shot of the app first, then what happens when the Save button is pressed)</div>

<div>&#160;</div>

<div><a href="http://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image4.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://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image_thumb3.png" width="608" height="320" /></a></div>

<div>&#160;</div>

<div><a href="http://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image5.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://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image_thumb4.png" width="466" height="395" /></a></div>

<div>&#160;</div>

<div>You can see that the date is passed back as Date(1313996400000).&#160; This is actually UTC time so we will need to make sure we check for that on the server.&#160; It’s always a good idea to store all dates on the server in UTC time anyhow.</div>

<div>&#160;</div>

<h3>Posting the Date To the Server (View From ASP.NET)</h3>

<div>&#160;</div>

<div>So let’s take a look at what is coming to the server.&#160; Remember that in the model, we defined what REST api is hit by a post from this model.&#160; In this case, it’s “update: &#8216;SponsorListJobListing/Update&#8217;,”.&#160; So, let’s debug into the C# (asp.net) side and see what is actually coming in.&#160; Here is what we see:</div>

<div>&#160;</div>

<div><a href="http://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image6.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://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image_thumb5.png" width="624" height="324" /></a></div>

<div>&#160;</div>

<div>Notice that the date comes in very simply as midnight on 8/22/2011 (the date we put in).&#160; The tricky part is we need to store this date as UTC so that when we end it back, it can get converted back to local.&#160; To do this, in our repository class, we have code to convert to UTC as follows:</div>

<div>&#160;</div>

<div><a href="http://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image7.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://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image_thumb6.png" width="594" height="444" /></a></div>

<div>&#160;</div>

<div>&#160;</div>

<div>Your repository will look different, but this is a good example.&#160; Now, notice the StartRunDate will be 8/22/2011 at 7AM.</div>

<div>&#160;</div>

<div><a href="http://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image8.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://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image_thumb7.png" width="620" height="76" /></a></div>

<p>&#160;</p>

<p>I’m over simplifying a little because it’s calculating UTC based on the Servers time zone, not the browsers.&#160; It would be better to send the timezone of the browser also and then calculate UTC correctly in <a href="http://en.wikipedia.org/wiki/JavaScript">JavaScript</a>, but I just want to give you the idea here.&#160; In my case, I’m always running this app from California and my server is always inCalifornia so at least things stay consistent.&#160; I do realize I’m simplifying and you need to make sure you think this through.</p>

<p>&#160;</p>

<p>Now, when I query the server for the returned data using the same ExtJS data model, the data comes down as follows.&#160; In ASP.NET it looks like this:</p>

<p><a href="http://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image9.png"><img style="background-image: none; border-right-width: 0px; margin: 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://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image_thumb8.png" width="244" height="191" /></a></p>

<p>Which is actually different than what was pushed up.&#160; It seems though that when ExtJS displays this, it converts it to local time.</p>

<p>Our browser Grid shows it like this (as we would expect)</p>

<p><a href="http://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image10.png"><img style="background-image: none; border-right-width: 0px; margin: 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://peterkellner.net/wp/wp/wp-content/uploads/2011/08/image_thumb9.png" width="156" height="112" /></a></p>

<p>Hope this helps!</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/08/24/getting-extjs-4-date-format-to-behave-properly-in-grid-panel-with-asp-net-mvc3/feed/</wfw:commentRss>
		<slash:comments>1</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>How To Read All The Bytes From A File in C# and Put Them Into A Byte Array</title>
		<link>http://peterkellner.net/2011/03/20/how-to-read-all-the-bytes-from-a-file-in-c-and-put-them-into-a-byte-array/</link>
		<comments>http://peterkellner.net/2011/03/20/how-to-read-all-the-bytes-from-a-file-in-c-and-put-them-into-a-byte-array/#comments</comments>
		<pubDate>Sun, 20 Mar 2011 18:32:51 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/03/20/how-to-read-all-the-bytes-from-a-file-in-c-and-put-them-into-a-byte-array/</guid>
		<description><![CDATA[I normally don’t blog about such simple things, but somehow, I’ve always done this in about 5 lines of code and I ran into at eggheadcafe in one line this morning.&#160; Simply using the File static method ReadAllBytes of the File class.  Here it is.&#160; Can’t be much simpler I don’t think.  &#160; [...]]]></description>
			<content:encoded><![CDATA[<p>I normally don’t blog about such simple things, but somehow, I’ve always done this in about 5 lines of code and I ran into at <a href="http://www.eggheadcafe.com/software/aspnet/35803970/read-binary-file-to-a-byte-array.aspx">eggheadcafe</a> in one line this morning.&#160; Simply using the <a href="http://msdn.microsoft.com/en-us/library/system.io.file.aspx">File</a> static method <a href="http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx">ReadAllBytes</a> of the File class.</p>  <p>Here it is.&#160; Can’t be much simpler I don’t think.</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">byte</span>[] downloadByteArray = File.ReadAllBytes(imageLocation);</pre>

  <br /></div>

<p>That’s it! hope this helps someone else.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/03/20/how-to-read-all-the-bytes-from-a-file-in-c-and-put-them-into-a-byte-array/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>What is the Difference Between Path.GetTempFileName and Path.GetTempPath</title>
		<link>http://peterkellner.net/2011/02/17/what-is-the-difference-between-path-gettempfilename-and-path-gettemppath/</link>
		<comments>http://peterkellner.net/2011/02/17/what-is-the-difference-between-path-gettempfilename-and-path-gettemppath/#comments</comments>
		<pubDate>Fri, 18 Feb 2011 00:40:31 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2011/02/17/what-is-the-difference-between-path-gettempfilename-and-path-gettemppath/</guid>
		<description><![CDATA[Getting filenames to use as scratch files is usually very straight forward.&#160; Well, today, I found out “not so much”.&#160; I decided today was a good day to tackle the problem of why is my file synchronization product we are about to release still littering my tmp directory with files when it finishes.&#160; Turns out, [...]]]></description>
			<content:encoded><![CDATA[<p>Getting filenames to use as scratch files is usually very straight forward.&#160; Well, today, I found out “not so much”.&#160; I decided today was a good day to tackle the problem of why is my file synchronization product we are about to release still littering my tmp directory with files when it finishes.&#160; Turns out, it’s the subtle difference between the above two Path member functions GetTempFileName and GetTempPath.</p>  <p>The big difference is that GetTempFileName actually creates a file where as GetTempPath simply tells you where the file is going to be.</p>  <p>Here are some words out of MSDN for <a href="http://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename(v=VS.100).aspx">Path.GetTempFileName</a></p>  <blockquote>   <p>This method creates a temporary file with a .TMP file extension.</p> </blockquote>  <p>And For <a href="http://msdn.microsoft.com/en-us/library/system.io.path.gettemppath.aspx">Path.GetTempPath</a></p>  <blockquote>   <p>The path to the temporary folder, ending with a backslash.</p> </blockquote>  <p>Subtle, but clear when you read the doc.&#160; If you read the community content, there is a humorous discussion with someone saying “you might as well use the other one, it does the same thing”.&#160; All I can say to that is read the docs!&#160; I got stung today, but never again.</p>  <p>I’m now creating my filename with a string as follows which kind of does the same thing, but not really.&#160; </p>  <p>&#160;</p>  <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">string</span> GetTempFileWithGuid(<span style="color: #0000ff">string</span> filePrefix)<br />{<br />    <span style="color: #0000ff">string</span> retFileName = <span style="color: #0000ff">string</span>.Format(<span style="color: #006080">&quot;{0}{1}{2}.crsync&quot;</span>, <br />        Path.GetTempPath(), filePrefix, Guid.NewGuid());<br />    <span style="color: #0000ff">return</span> retFileName;<br />}</pre>
  </div>
</p>

<p>HTH’s.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2011/02/17/what-is-the-difference-between-path-gettempfilename-and-path-gettemppath/feed/</wfw:commentRss>
		<slash:comments>1</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 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/</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[VS2010]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[gzip]]></category>
		<category><![CDATA[threading]]></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 [...]]]></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>  <span id="more-1418"></span>  <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"><p><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)<br />{<br />    var cryptCompress =<br />        <span style="color: #0000ff">new</span> CryptCompress();<br />    var thread =<br />        <span style="color: #0000ff">new</span> Thread(<br />            () =&gt;<br />            cryptCompress.CompressFile<br />                (<span style="color: #006080">@&quot;G:\NoBackup\ext-3.2.1\ext-all-debug-w-comments.js&quot;</span>));<br />    thread.Start();<br /><br />    <span style="color: #0000ff">int</span> cnt = 0;<br />    <span style="color: #0000ff">while</span> (thread.IsAlive)<br />    {<br />        cnt++;<br />        Console.WriteLine(<span style="color: #006080">&quot;Waiting... &quot;</span> + cnt);<br />        Thread.Sleep(50);<br />    }<br />    Console.WriteLine(<span style="color: #006080">&quot;Before Compression KBytes: {0}&quot;</span>,<br />                        cryptCompress.BeforeCompressionBytes/1000);<br />    Console.WriteLine(<span style="color: #006080">&quot;After Compression KBytes: {0}&quot;</span>,<br />                        cryptCompress.AfterCompressionBytes/1000);<br />}</pre>

  <br /></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<br />{<br /><span style="color: #0000ff">public</span> <span style="color: #0000ff">byte</span>[] CompressedBytes { get; set; }<br /><span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> BeforeCompressionBytes { get; set; }<br /><span style="color: #0000ff">public</span> <span style="color: #0000ff">long</span> AfterCompressionBytes { get; set; }<br /><br /><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> CompressFile(<span style="color: #0000ff">string</span> fileName)<br />{<br />    <span style="color: #0000ff">using</span> (var fileStream = <br />        <span style="color: #0000ff">new</span> FileStream(fileName, FileMode.Open, FileAccess.Read))<br />    {<br />        var uncompressedBytes = <span style="color: #0000ff">new</span> <span style="color: #0000ff">byte</span>[fileStream.Length];<br />        fileStream.Read(uncompressedBytes, 0, <br />            (<span style="color: #0000ff">int</span>) fileStream.Length);<br />        CompressedBytes = CompressGzip(uncompressedBytes);<br />        BeforeCompressionBytes = fileStream.Length;<br />        AfterCompressionBytes = CompressedBytes.Length;<br />        fileStream.Close();<br />    }<br />}<br /><br /><span style="color: #008000">/// &lt;summary&gt;</span><br /><span style="color: #008000">/// Take a simple stream of uncompressed bytes and compress them</span><br /><span style="color: #008000">/// &lt;/summary&gt;</span><br /><span style="color: #008000">/// &lt;param name=&quot;uncompressedBytes&quot;&gt;&lt;/param&gt;</span><br /><span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span><br /><span style="color: #0000ff">public</span> <span style="color: #0000ff">byte</span>[] CompressGzip(<span style="color: #0000ff">byte</span>[] uncompressedBytes)<br />{<br />    <span style="color: #0000ff">using</span> (var memory = <span style="color: #0000ff">new</span> MemoryStream())<br />    {<br />        <span style="color: #0000ff">using</span> <br />            (var gZipStream = <br />                <span style="color: #0000ff">new</span> GZipStream(memory, CompressionMode.Compress, <span style="color: #0000ff">true</span>))<br />        {<br />            gZipStream.Write<br />                (uncompressedBytes, 0, uncompressedBytes.Length);<br />        }<br />        <span style="color: #0000ff">return</span> memory.ToArray();<br />    }<br />}<br />}</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/</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 [...]]]></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>  <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"><p><div><a href="http://peterkellner.net/FilesForWebDownload/Using_9C01/SHA512BufferedConsoleProject.zip" target="_self">SHA512BufferedConsoleProject.zip</a></div></p></div> </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: #008000">/// &lt;summary&gt;</span><br /><span style="color: #008000">/// Used so we can get MD5Hash and get progress on calculation</span><br /><span style="color: #008000">/// &lt;/summary&gt;</span><br /><span style="color: #008000">/// &lt;param name=&quot;streamIn&quot;&gt;&lt;/param&gt;</span><br /><span style="color: #008000">/// &lt;returns&gt;&lt;/returns&gt;</span><br /><span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> GetSha512Buffered(Stream streamIn)<br />{<br />    Process process = Process.GetCurrentProcess();<br />    <span style="color: #0000ff">const</span> <span style="color: #0000ff">int</span> bufferSizeForMd5Hash = 1024*1024*8; <span style="color: #008000">// 8MB</span><br />    Console.WriteLine(<span style="color: #006080">&quot;-----------------------------------&quot;</span>);<br />    Console.WriteLine(<span style="color: #006080">&quot;Memory Buffer Size {0} MB&quot;</span>, bufferSizeForMd5Hash/1000);<br />    Console.WriteLine(<span style="color: #006080">&quot;-----------------------------------&quot;</span>);<br />    <span style="color: #0000ff">string</span> hashString;<br />    <span style="color: #0000ff">using</span> (var md5Prov = <span style="color: #0000ff">new</span> SHA256Managed())<br />    {<br />        <span style="color: #0000ff">int</span> readCount;<br />        <span style="color: #0000ff">long</span> bytesTransfered = 0;<br />        var buffer = <span style="color: #0000ff">new</span> <span style="color: #0000ff">byte</span>[bufferSizeForMd5Hash];<br />        <span style="color: #0000ff">while</span> ((readCount = streamIn.Read(buffer, 0, buffer.Length)) != 0)<br />        {<br />            <span style="color: #008000">// Need to figure out if this is final block</span><br />            <span style="color: #0000ff">if</span> (bytesTransfered + readCount == streamIn.Length)<br />            {<br />                md5Prov.TransformFinalBlock(buffer, 0, readCount);<br />            }<br />            <span style="color: #0000ff">else</span><br />            {<br />                md5Prov.TransformBlock(buffer, 0, bufferSizeForMd5Hash, buffer, 0);<br />            }<br />            bytesTransfered += readCount;<br />            Console.WriteLine(<span style="color: #006080">&quot;GetSha512Buffered:{0}MB/{1}MB.   Memory Used: {2}MB&quot;</span>,<br />                                bytesTransfered/1000000,<br />                                streamIn.Length/1000000,<br />                                process.PrivateMemorySize64/1000000);<br />        }<br />        hashString = BitConverter.ToString(md5Prov.Hash).Replace(<span style="color: #006080">&quot;-&quot;</span>, String.Empty);<br />        md5Prov.Clear();<br />    }<br />    <span style="color: #0000ff">return</span> hashString;<br />}</pre>

  <br /></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>Efficiently Generating SHA256 Checksum For Files Using C#</title>
		<link>http://peterkellner.net/2010/11/24/efficiently-generating-sha256-checksum-for-files-using-csharp/</link>
		<comments>http://peterkellner.net/2010/11/24/efficiently-generating-sha256-checksum-for-files-using-csharp/#comments</comments>
		<pubDate>Wed, 24 Nov 2010 18:01:00 +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>

		<guid isPermaLink="false">http://peterkellner.net/2010/11/24/efficiently-generating-sha256-checksum-for-files-using-csharp/</guid>
		<description><![CDATA[* I just added another post on similar topic and added more details on performance and a case where buffered memory usage really did matter:&#160; http://peterkellner.net/2010/12/03/using-sha256managed-to-generate-sha256-hash/  I’m building a file synchronization with the cloud application and I want to store a checksum with the file so that I can verify later that the file [...]]]></description>
			<content:encoded><![CDATA[<p><font style="background-color: #ffff00">* I just added another post on similar topic and added more details on performance and a case where buffered memory usage really did matter:&#160; </font><a title="http://peterkellner.net/2010/12/03/using-sha256managed-to-generate-sha256-hash/" href="http://peterkellner.net/2010/12/03/using-sha256managed-to-generate-sha256-hash/"><font style="background-color: #ffff00">http://peterkellner.net/2010/12/03/using-sha256managed-to-generate-sha256-hash/</font></a></p>  <p>I’m building a <a href="http://connectionroad.com/">file synchronization</a> with the cloud application and I want to store a checksum with the file so that I can verify later that the file is what I think it is. I’m not a crypto guy so I searched around the internet for a solution.&#160;&#160; I found lots of examples and settled on this one that uses <a href="http://msdn.microsoft.com/en-us/library/system.security.cryptography.sha256.aspx">SHA256</a> for the job.&#160; I also found some comments saying that it would be more efficient to wrap it in a <a href="http://msdn.microsoft.com/en-us/library/system.io.bufferedstream.aspx">BufferedStream</a> rather than processing the entire file at once.</p>  <p>Example Links: <a title="http://efreedom.com/Question/1-1345851/MD5-File-Processing" href="http://efreedom.com/Question/1-1345851/MD5-File-Processing">http://efreedom.com/Question/1-1345851/MD5-File-Processing</a> ; <a title="http://stackoverflow.com/questions/1177607/what-is-the-fastest-way-to-create-a-checksum-for-large-files-in-c/1177744#1177744" href="http://stackoverflow.com/questions/1177607/what-is-the-fastest-way-to-create-a-checksum-for-large-files-in-c/1177744#1177744">http://stackoverflow.com/questions/1177607/what-is-the-fastest-way-to-create-a-checksum-for-large-files-in-c/1177744#1177744</a></p>  <p>My intention for this post was to show how much more efficient it would be to use BufferedStream, however my results don’t show that.&#160; I’m guessing that somehow, the efficiency is happening under the covers in a place I don’t see.</p>  <p>If anyone knows this space well, please feel free to comment and suggest a better method.&#160; I’m publishing my source below for the test and my surprisingly similar results whether I used buffering or not.</p>  <p>Looking forward to the responses.</p>  <span id="more-1414"></span>  <p>&#160;</p>  <table border="1" cellspacing="0" cellpadding="2" width="599"><tbody>     <tr>       <td valign="top" width="158">File Size In Megabytes</td>        <td valign="top" width="152">Non-Buffered</td>        <td valign="top" width="168">Buffered</td>        <td valign="top" width="119">Max Memory</td>     </tr>      <tr>       <td valign="top" width="158">.8 MB</td>        <td valign="top" width="152">.088 Seconds</td>        <td valign="top" width="168">.082 Seconds</td>        <td valign="top" width="119">&#160;</td>     </tr>      <tr>       <td valign="top" width="158">851 MB (buffer not set)</td>        <td valign="top" width="152">30.3 Seconds</td>        <td valign="top" width="168">30.4 Seconds</td>        <td valign="top" width="119">1700MB</td>     </tr>      <tr>       <td valign="top" width="158">851 MB (buffer 1MB)</td>        <td valign="top" width="160">29.2 Seconds</td>        <td valign="top" width="189">29.6 Seconds</td>        <td valign="top" width="152">1450MB</td>     </tr>   </tbody></table>  <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">using</span> System;<br /><span style="color: #0000ff">using</span> System.Diagnostics;<br /><span style="color: #0000ff">using</span> System.IO;<br /><span style="color: #0000ff">using</span> System.Security.Cryptography;<br /><br /><span style="color: #0000ff">namespace</span> ConsoleApplicationMD5test<br />{<br />    <span style="color: #0000ff">internal</span> <span style="color: #0000ff">class</span> Program<br />    {<br />        <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)<br />        {<br />            <span style="color: #008000">//const string fileName = @&quot;g:\tempjunk\BlobSyncClient.zip&quot;;</span><br />            <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> fileName = <br />            <span style="color: #006080">@&quot;g:\msdn\en_visual_studio_2008_service_pack_1_x86_dvd_x15-12962.iso&quot;</span>;<br /><br />            var stopwatch1 = <span style="color: #0000ff">new</span> Stopwatch();<br />            stopwatch1.Start();<br />            <span style="color: #0000ff">string</span> str1 = <span style="color: #006080">&quot;&quot;</span>; <span style="color: #008000">// GetChecksum(fileName);</span><br />            stopwatch1.Stop();<br /><br />            var stopwatch2 = <span style="color: #0000ff">new</span> Stopwatch();<br />            stopwatch2.Start();<br />            var fileStream = <span style="color: #0000ff">new</span> FileStream(fileName, FileMode.OpenOrCreate,<br />                FileAccess.Read);<br />            <span style="color: #0000ff">string</span> str2 = GetChecksumBuffered(fileStream);<br />            stopwatch2.Stop();<br /><br />            Console.WriteLine(str1 + <span style="color: #006080">&quot; &quot;</span> + stopwatch1.ElapsedMilliseconds);<br />            Console.WriteLine(str2 + <span style="color: #006080">&quot; &quot;</span> + stopwatch2.ElapsedMilliseconds);<br /><br />            Console.ReadLine();<br />        }<br /><br />        <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> GetChecksum(<span style="color: #0000ff">string</span> file)<br />        {<br />            <span style="color: #0000ff">using</span> (FileStream stream = File.OpenRead(file))<br />            {<br />                var sha = <span style="color: #0000ff">new</span> SHA256Managed();<br />                <span style="color: #0000ff">byte</span>[] checksum = sha.ComputeHash(stream);<br />                <span style="color: #0000ff">return</span> BitConverter.ToString(checksum).Replace(<span style="color: #006080">&quot;-&quot;</span>, String.Empty);<br />            }<br />        }<br /><br />        <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">string</span> GetChecksumBuffered(Stream stream)<br />        {<br />            <span style="color: #0000ff">using</span> (var bufferedStream = <span style="color: #0000ff">new</span> BufferedStream(stream, 1024 * 32))<br />            {<br />                var sha = <span style="color: #0000ff">new</span> SHA256Managed();<br />                <span style="color: #0000ff">byte</span>[] checksum = sha.ComputeHash(bufferedStream);<br />                <span style="color: #0000ff">return</span> BitConverter.ToString(checksum).Replace(<span style="color: #006080">&quot;-&quot;</span>, String.Empty);<br />            }<br />        }<br />    }<br />}</pre>

  <br /></div>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/11/24/efficiently-generating-sha256-checksum-for-files-using-csharp/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Building a Simple Azure Blob Tree Viewer With Azure StorageClient API</title>
		<link>http://peterkellner.net/2010/11/12/azure-storage-treeviewer-directory-browser/</link>
		<comments>http://peterkellner.net/2010/11/12/azure-storage-treeviewer-directory-browser/#comments</comments>
		<pubDate>Sat, 13 Nov 2010 01:36:30 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[Azure]]></category>
		<category><![CDATA[Azure Blob Storage]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[StorageClient]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/11/12/azure-storage-treeviewer-directory-browser/</guid>
		<description><![CDATA[Understanding how Azure Blob Storage can be used to simulate directory structures is a little tricky to say the least.&#160; I’ve got a long forum thread on the Windows Azure Community site&#160; now discussing the details.&#160; As always, Steve Marx has been a big help here with a bunch of code. Steve’s got a great [...]]]></description>
			<content:encoded><![CDATA[<p>Understanding how <a href="http://msdn.microsoft.com/en-us/windowsazure/cc974146.aspx">Azure</a> <a href="http://www.microsoft.com/windowsazure/storage/default.aspx">Blob Storage</a> can be used to simulate directory structures is a little tricky to say the least.&#160; I’ve got a long <a href="http://social.msdn.microsoft.com/Forums/en-US/windowsazuredata/thread/256cfc0f-bccc-4bf7-b7eb-cb7c7aca0c8a">forum thread</a> on the Windows Azure Community site&#160; now discussing the details.&#160; As always, <a href="http://blog.smarx.com/">Steve Marx</a> has been a big help here with a bunch of code. Steve’s got a great blog where he provides lots of examples and insights.&#160; Neil Mackenzie has also contributed here to getting to the answer.</p>  <p>Just so we now have an example, I’ve put together a simple windows form app that let’s you set a few variables in your app.config to point at your azure storage and container, let you view your app as a tree as well as see the code how it can be done.&#160; I have not commented the code much, just thought it would be good to get it out there.&#160; The running application shows you the data as follows.</p>  <p><a href="http://peterkellner.net/FilesForWebDownload/Building-a-Simple-Azure-Blob-Tree-Viewer_F37A/image.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://peterkellner.net/FilesForWebDownload/Building-a-Simple-Azure-Blob-Tree-Viewer_F37A/image_thumb.png" width="341" height="254" /></a></p>  <span id="more-1410"></span>  <p>So, for the details, I’m pasting below the meet of the code.&#160; Basically, it does what you would expect in terms of iterating through the directories recursively to build the list.&#160; Again, just set your parameters in your app.config as follows:</p>  <p><a href="http://peterkellner.net/FilesForWebDownload/Building-a-Simple-Azure-Blob-Tree-Viewer_F37A/image_3.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://peterkellner.net/FilesForWebDownload/Building-a-Simple-Azure-Blob-Tree-Viewer_F37A/image_thumb_3.png" width="281" height="115" /></a></p>  <p>and you can run it for yourself and see how it goes.</p>  <p>I’m pasting some code below, as well as including the project source code.&#160; The main reason I’m pasting the code is so that the search engines can find it better.&#160; It’s really just easier to download the project and load it into <a href="http://www.microsoft.com/visualstudio/en-us/visual-studio-2010-launch?WT.mc_id=SEARCH&amp;WT.srch=1">Visual Studio 2010</a> and run it yourself.</p>  <p>&#160;</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:40251c5f-e5d4-447d-80ca-5c6ba51b4cef" class="wlWriterEditableSmartContent"><p><div>Visual Studio 2010   <a href="http://peterkellner.net/FilesForWebDownload/Building-a-Simple-Azure-Blob-Tree-Viewer_F37A/AzureBlobTreeViewer_3.zip" target="_self">AzureBlobTreeViewer.zip</a></div></p></div>  <p>&#160;</p>  <p>Good Luck, and hope this helps!</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:6a0a0214-7d94-482e-9b3f-436063c63866" class="wlWriterEditableSmartContent"><p><div>Visual Studio 2010   <a href="http://peterkellner.net/FilesForWebDownload/Building-a-Simple-Azure-Blob-Tree-Viewer_F37A/AzureBlobTreeViewer.zip" target="_self">AzureBlobTreeViewer.zip</a></div></p></div>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/11/12/azure-storage-treeviewer-directory-browser/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>If You Use BackgroundWorker in .Net, Make Sure You Wrap Your Worker Method In Try (or risk missing the exception thrown)</title>
		<link>http://peterkellner.net/2010/11/09/using-backgroundworker-exception-not-thrown/</link>
		<comments>http://peterkellner.net/2010/11/09/using-backgroundworker-exception-not-thrown/#comments</comments>
		<pubDate>Tue, 09 Nov 2010 21:57:42 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Exceptions]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/11/09/if-you-use-backgroundworker-in-net-make-sure-you-wrap-your-worker-method-in-try-or-risk-missing-the-exception-thrown/</guid>
		<description><![CDATA[So, just a short post in case someone runs into the same problem I had today that cost me about 2 hours using Visual Studio 2010.&#160; Basically, if you are using the BackgroundWorker in a windows app (with visual studio) and find that the method is not finishing and seemingly not throwing exceptions, maybe it [...]]]></description>
			<content:encoded><![CDATA[<p>So, just a short post in case someone runs into the same problem I had today that cost me about 2 hours using <a href="http://www.microsoft.com/visualstudio/en-us/visual-studio-2010-launch?WT.mc_id=SEARCH&amp;WT.srch=1">Visual Studio 2010</a>.&#160; Basically, if you are using the <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.backgroundworker.aspx">BackgroundWorker</a> in a windows app (with visual studio) and find that the method is not finishing and seemingly not throwing exceptions, maybe it actually is and you are missing it.</p>  <p>That is, if you have code like 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">private</span> <span style="color: #0000ff">void</span> backgroundWorker1_DoWork(<span style="color: #0000ff">object</span> sender, DoWorkEventArgs e)<br />{<br />    backgroundWorker1.ReportProgress(0,<span style="color: #006080">&quot;starting...&quot;</span>);<br />    <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; 10;i++ )<br />    {<br />        <span style="color: #0000ff">if</span> (i &gt; 5)<br />        {<br />            <span style="color: #0000ff">throw</span> <span style="color: #0000ff">new</span> ApplicationException(<span style="color: #006080">&quot;any error here&quot;</span>);<br />        }<br />        backgroundWorker1.ReportProgress(i, <span style="color: #006080">&quot;working...&quot;</span>);<br />    }<br />}</pre>

  <br /></div>

<p>and you run it in the debugger, it will not stop by default when the exception is thrown (like it would in single threaded code).</p>

<p>So, I recommend doing the following and setting a break point to see the error.</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/If-You-Use-BackgroundThrea_BFD6/image.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://peterkellner.net/FilesForWebDownload/If-You-Use-BackgroundThrea_BFD6/image_thumb.png" width="497" height="251" /></a></p>

<p>&#160;</p>

<p>Another thing you can do is bump up where debug’s stops uisng the dialog:</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/If-You-Use-BackgroundThrea_BFD6/image_3.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://peterkellner.net/FilesForWebDownload/If-You-Use-BackgroundThrea_BFD6/image_thumb_3.png" width="512" height="336" /></a></p>

<p>goes to:</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/If-You-Use-BackgroundThrea_BFD6/image_4.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://peterkellner.net/FilesForWebDownload/If-You-Use-BackgroundThrea_BFD6/image_thumb_4.png" width="462" height="244" /></a></p>

<p>&#160;</p>

<p>Then, you will get an error you expect.</p>

<p>&#160;</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/If-You-Use-BackgroundThrea_BFD6/image_5.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://peterkellner.net/FilesForWebDownload/If-You-Use-BackgroundThrea_BFD6/image_thumb_5.png" width="416" height="229" /></a></p>

<p>HTH’s!</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/11/09/using-backgroundworker-exception-not-thrown/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>A Refactoring I Should Have Been Doing for Years but Forgot, AppSettings</title>
		<link>http://peterkellner.net/2010/10/18/refactoring-for-c-appsettings-configurationmanager/</link>
		<comments>http://peterkellner.net/2010/10/18/refactoring-for-c-appsettings-configurationmanager/#comments</comments>
		<pubDate>Mon, 18 Oct 2010 17:30:25 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/10/18/refactoring-for-c-appsettings-configurationmanager/</guid>
		<description><![CDATA[So, It’s always annoyed me that when I want to check an AppSettings variable from app.config or web.config I’ve had to go through code like the following:     &#60;appSettings&#62;  &#60;add key=&#34;AccountStorageURL&#34; value=&#34;http://testit.blob.core.windows.net/&#34;  /&#62;


&#160;


  


  public string GetStorageURLOld(string username, string password){   var accountStorageURL = string.Empty;   [...]]]></description>
			<content:encoded><![CDATA[<p>So, It’s always annoyed me that when I want to check an AppSettings variable from app.config or web.config I’ve had to go through code like the following:</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">&lt;</span><span style="color: #800000">appSettings</span><span style="color: #0000ff">&gt;</span><br />  <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">key</span><span style="color: #0000ff">=&quot;AccountStorageURL&quot;</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">=&quot;http://testit.blob.core.windows.net/&quot;</span>  <span style="color: #0000ff">/&gt;</span></pre>
</div>

<div>&#160;</div>

<div>
  <br /></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">public</span> <span style="color: #0000ff">string</span> GetStorageURLOld(<span style="color: #0000ff">string</span> username, <span style="color: #0000ff">string</span> password)<br />{<br />   var accountStorageURL = <span style="color: #0000ff">string</span>.Empty;<br />   <span style="color: #0000ff">if</span> (ConfigurationManager.AppSettings[<span style="color: #006080">&quot;AccountStorageURL&quot;</span>] != <span style="color: #0000ff">null</span>)<br />   {<br />       accountStorageURL = ConfigurationManager.AppSettings.Get(<span style="color: #006080">&quot;AccountStorageURL&quot;</span>);<br />   }<br />   <span style="color: #0000ff">return</span> accountStorageURL;<br />}</pre>

  <br /></div>

<p>I should have realized a long time ago that this code is equivalent to:</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">string</span> GetStorageURL(<span style="color: #0000ff">string</span> username, <span style="color: #0000ff">string</span> password)<br />{<br />  <span style="color: #0000ff">return</span> ConfigurationManager.AppSettings[<span style="color: #006080">&quot;AccountStorageURL&quot;</span>] ?? <span style="color: #0000ff">string</span>.Empty;<br />}</pre>

  <br /></div>

<div>Just thought I’d share in case someone else had forgot this also.</div>

<div>&#160;</div>

<div>HTH’s.
  <br /></div>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/10/18/refactoring-for-c-appsettings-configurationmanager/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How To Override ToString() in a Simple C# Class</title>
		<link>http://peterkellner.net/2010/07/26/override-tostring-csharp-simple/</link>
		<comments>http://peterkellner.net/2010/07/26/override-tostring-csharp-simple/#comments</comments>
		<pubDate>Mon, 26 Jul 2010 18:25:50 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/07/26/override-tostring-csharp-simple/</guid>
		<description><![CDATA[So, you have a simple class that has a bunch public properties and you want to be able to use ToString() on it to show some data?&#160; It’s easy.&#160; All you have to do is override the ToString() class inside your C# code.  So, here is an example class that does that.   [...]]]></description>
			<content:encoded><![CDATA[<p>So, you have a simple class that has a bunch public properties and you want to be able to use <a href="http://msdn.microsoft.com/en-us/library/system.object.tostring.aspx">ToString()</a> on it to show some data?&#160; It’s easy.&#160; All you have to do is override the ToString() class inside your <a href="http://msdn.microsoft.com/en-us/vcsharp/default.aspx">C#</a> code.</p>  <p>So, here is an example class that does that.</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> DbProgressReport<br />{<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> ScopeNameProgress { get; set; }<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> SourceOrDestination { get; set; }<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> TableName { get; set; }<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> TotalRecords { get; set; }<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> ChangesApplied { get; set; }<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> ChangesFailed { get; set; }<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> ChangesPending { get; set; }<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Deletes { get; set; }<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Inserts { get; set; }<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> TotalChanges { get; set; }<br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> Updates { get; set; }<br />    <span style="color: #0000ff">public</span> DateTime LastChangeDate { get; set; }<br /><br />    <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">string</span> ToString()<br />    {<br />        <span style="color: #0000ff">string</span> readableDbProgressReport =<br />            <span style="color: #0000ff">string</span>.Format(<br />                <span style="color: #006080">&quot;{0} ScopeName {1},Table {2},TotalRecords {3}&quot;</span><br />                SourceOrDestination, ScopeNameProgress, TableName);<br /><br />        <span style="color: #0000ff">return</span> readableDbProgressReport;<br />    }<br />    ...</pre>

  <br /></div>

<p>Now, all you have to do when accessing this class is use the ToString property.</p>

<p>That is:</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">DbProgressReport dbProgressReport = <br />  <span style="color: #0000ff">new</span> DbProgressReport(<span style="color: #006080">&quot;source&quot;</span>,<span style="color: #006080">&quot;scopename&quot;</span>,<span style="color: #006080">&quot;tablename&quot;</span>);<br /><span style="color: #0000ff">string</span> str = dbProgressReport.ToString();<br /></pre>

  <br /></div>

<p>and the output will be:</p>

<p>source ScopeName scopename,&#160; Table tablename</p>

<p>Simple as that!</p>

<p>Hope this helps.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/07/26/override-tostring-csharp-simple/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Handy Refactoring with CodeRush (InLine Temp)</title>
		<link>http://peterkellner.net/2010/07/02/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 = <br />  <span style="color: #0000ff">new</span> SessionAttendeeODS();<br />listSessionAttendees = <br />  sessionAttendeeOds.GetByUsername(Context.User.Identity.Name);</pre>
</div>

<div>&#160;</div>
<span id="more-1335"></span>

<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 = <br />                <span style="color: #0000ff">new</span> SessionAttendeeODS().GetByUsername(Context.User.Identity.Name);</pre>

  <br /></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>ReSharper 5.0 Adds New &#8220;Add Parameter&#8221; Refactoring</title>
		<link>http://peterkellner.net/2010/04/26/resharper-refactor-add-parameter/</link>
		<comments>http://peterkellner.net/2010/04/26/resharper-refactor-add-parameter/#comments</comments>
		<pubDate>Mon, 26 Apr 2010 18:38:40 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.Net 2.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Refactor]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/04/26/resharper-refactor-add-parameter/</guid>
		<description><![CDATA[In this post, I’ll show a simple example of how when you add a parameter to C# method, ReSharper gives you a simple prompting to ask if you want to add a parameter to your method, or create an overloaded method that gives you the flexibility to maintain the old method signature and have the [...]]]></description>
			<content:encoded><![CDATA[<p>In this post, I’ll show a simple example of how when you add a parameter to C# method, <a href="http://www.jetbrains.com/resharper/whatsnew/">ReSharper</a> gives you a simple prompting to ask if you want to add a parameter to your method, or create an overloaded method that gives you the flexibility to maintain the old method signature and have the new method.</p> <span id="more-1281"></span>  <p>In my original code, I had the following code that called the method GetLoadResultsFromInterval.</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">List<span style="color: #0000ff">&lt;</span><span style="color: #800000">LoadResult</span><span style="color: #0000ff">&gt;</span> loadResultsForInterval =<br />    GetLoadResultsForInterval(rec.TimePoint,<br />                              rec.IntervalLengthInMs);</pre>

  <br /></div>

<p>Then, I simply added one more parameter (loadFtpArrivalTimeDictionary) and ReSharper prompted me with the following:</p>

<p>&#160;</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/ReSharpe.0AddsNewAddParameterRefactoring_A3A1/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/ReSharpe.0AddsNewAddParameterRefactoring_A3A1/image_thumb.png" width="627" height="203" /></a> </p>

<p>If I choose the first “Add Parameter, I get what I would expect, which is just the method with an extra parameter.</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">summary</span><span style="color: #0000ff">&gt;</span><br />/// <span style="color: #0000ff">&lt;</span><span style="color: #800000">param</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;timePoint&quot;</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">param</span><span style="color: #0000ff">&gt;</span><br />/// <span style="color: #0000ff">&lt;</span><span style="color: #800000">param</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;intervalLengthInMs&quot;</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">param</span><span style="color: #0000ff">&gt;</span><br />/// <span style="color: #0000ff">&lt;</span><span style="color: #800000">param</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;loadFtpArrivalTimeDictionary&quot;</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">param</span><span style="color: #0000ff">&gt;</span><br />/// <span style="color: #0000ff">&lt;</span><span style="color: #800000">returns</span><span style="color: #0000ff">&gt;&lt;/</span><span style="color: #800000">returns</span><span style="color: #0000ff">&gt;</span><br />private static List<span style="color: #0000ff">&lt;</span><span style="color: #800000">LoadResult</span><span style="color: #0000ff">&gt;</span> <br />   GetLoadResultsForInterval(DateTime timePoint, <br />   int intervalLengthInMs, <br />   Dictionary<span style="color: #0000ff">&lt;</span><span style="color: #800000">string</span>, <span style="color: #ff0000">DateTime</span><span style="color: #0000ff">&gt;</span> loadFtpArrivalTimeDictionary)<br />{<br />   throw new NotImplementedException();<br />}</pre>

  <br /></div>

<p>It even adds the param xml definition automatically.</p>

<p>However, I’m not quite as pleased with the second refactoring, that is, “Create Overload”.&#160; It does almost everything I would expect, but does not implement the method, which I think it could.&#160; Or, at least it could give it a try.&#160; What it does implement is the following:</p>

<p><a href="http://peterkellner.net/FilesForWebDownload/ReSharpe.0AddsNewAddParameterRefactoring_A3A1/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/ReSharpe.0AddsNewAddParameterRefactoring_A3A1/image_thumb_3.png" width="652" height="141" /></a> </p>

<p>I’m actually including a picture of it rather than the actual code because it created an error.&#160; It should have said DateTime instead of loadFtpArrivalTimeDictionary.&#160; Two other things of interest.</p>

<p>First, it did not add xml comments (which is probably because of the syntax error), and second, I would have expected it to have called the overloaded method as follows instead of what it did.</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">private static List<span style="color: #0000ff">&lt;</span><span style="color: #800000">LoadResult</span><span style="color: #0000ff">&gt;</span> GetLoadResultsForInterval(DateTime timePoint,<br />    int intervalLengthInMs, <br />    Dictionary<span style="color: #0000ff">&lt;</span><span style="color: #800000">string</span>, <span style="color: #ff0000">DateTime</span><span style="color: #0000ff">&gt;</span> loadFtpArrivalTimeDictionary)<br />{<br />    // - put additional code here<br />    return GetLoadResultsForInterval(timePoint, intervalLengthInMs);<br />}</pre>
</div>

<div>&#160;</div>

<div>(before anyone points out this is the opposite of how you would normally overload a method call, I do know that.&#160; That is, typically, you would have a method with 2 incoming parameters that internally would call a method with 3 internal parameters, and you would simply default the third parameter.&#160; In my case, I’m going the other way, which is unusual, but not unheard of.)</div>

<div>
  <br /></div>

<p>I began this post to brag about yet another awesome ReSharper refactoring because I really just wanted to add a parameter to an existing method call and it worked perfect.&#160; It was not until I decided to show the other refactoring for this blog post did I run into the problem.&#160; </p>

<p>I’m going to post this to the refactor dev’s and see if they can let me know where either I’ve gone astray, or there is a problem in their code.&#160; At any rate, I’m running this on vs2008 and have not tried it on vs2010.</p>

<p>Hope this helps!</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/04/26/resharper-refactor-add-parameter/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>OData Query Option top Forces Data To Be Sorted By Primary Key</title>
		<link>http://peterkellner.net/2010/03/31/odata-wcfdataservice-top-orderby-default-sort/</link>
		<comments>http://peterkellner.net/2010/03/31/odata-wcfdataservice-top-orderby-default-sort/#comments</comments>
		<pubDate>Wed, 31 Mar 2010 18:24:31 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.Net 2.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[OData]]></category>
		<category><![CDATA[WCF Data Services]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/03/31/odata-wcfdataservice-top-orderby-default-sort/</guid>
		<description><![CDATA[This post show a simple WCF Data Service (Formerly known as ADO.NET Data Services) project that retrieves data using the Reflection Provider for accessing data.  It goes on to show that using $top forces a server side sort of the data by the data's primary key before sending the data out to the client program.]]></description>
			<content:encoded><![CDATA[<p>I’ve recently started using <a href="http://msdn.microsoft.com/en-us/data/aa937697.aspx">Microsoft’s WCF Data Services</a> which supports <a href="http://www.odata.org/developers">OData Services</a>.&#160; What this means is that we can access resources by simply specifying a <a href="http://en.wikipedia.org/wiki/Uniform_Resource_Identifier">URI</a>.&#160; This concept greatly simplified building an ORM layer on a web site, as well as creating the linkage between the server side data and the client side application, which in my case is usually a browser.</p>  <p>So, the issue this blog addresses is that if you form a URI with the parameter $top={anything}, your data will automatically be sorted.&#160; The documentation for OData on top basically says that, but it could be clearer.&#160; It says the following:</p>  <blockquote>   <p><a href="http://www.odata.org/developers/protocols/uri-conventions#TopSystemQueryOption">“If the data service URI contains a $top query option, but does not contain a $orderby option, then the Entries in the set needs to first be fully ordered by the data service.”</a></p> </blockquote>  <p>What actually happens is when you use the orderby clause, the data will be sorted 100% of the time for you, whether you do it or not.</p> <span id="more-1165"></span>  <p>I put a small example together that shows that.&#160; I’ll briefly step through the parts of the code attached to this post that show that happening.&#160; First, the results:</p>  <p><a href="http://peterkellner.net/FilesForWebDownload/ODataQueryOptiontopForcesDataToBeSortedB_9CBC/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/ODataQueryOptiontopForcesDataToBeSortedB_9CBC/image_thumb.png" width="411" height="202" /></a></p>  <p>Here is the actual Visual Studio 2010 RC project you can run yourself:</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:3b91b815-24f9-4c84-aa5e-cf26654049f6" class="wlWriterSmartContent">   <div>Solution: <a href="http://peterkellner.net/FilesForWebDownload/ODataQueryOptiontopForcesDataToBeSortedB_9CBC/WCFServicesWithODataExample.zip" target="_self">WCFServicesWithODataExample.zip</a></div> </div>  <p>Notice that when $top=5 is called, the data returned is sorted by the primary key.</p>  <p>Now, here are the details of the project that shows this happening.</p>  <p>Below is the complete code for the WcfDataService.svc.&#160; Notice that the data is created in the SampleDataSource constructor and is created in non-key order.</p>  <div class="csharpcode">   <pre class="alt"><span class="kwrd">using</span> System;</pre>

  <pre><span class="kwrd">using</span> System.Collections.Generic;</pre>

  <pre class="alt"><span class="kwrd">using</span> System.Data.Services;</pre>

  <pre><span class="kwrd">using</span> System.Data.Services.Common;</pre>

  <pre class="alt"><span class="kwrd">using</span> System.Linq;</pre>

  <pre><span class="kwrd">using</span> System.ServiceModel.Web;</pre>

  <pre class="alt"><span class="kwrd">using</span> System.Web;</pre>

  <pre>&#160;</pre>

  <pre class="alt"><span class="kwrd">namespace</span> WebApp</pre>

  <pre>{</pre>

  <pre class="alt">    [EntityPropertyMappingAttribute(</pre>

  <pre>        <span class="str">&quot;Title&quot;</span>, SyndicationItemProperty.Title,</pre>

  <pre class="alt">        SyndicationTextContentKind.Plaintext, <span class="kwrd">true</span>)]</pre>

  <pre>    [DataServiceKey(<span class="str">&quot;Id&quot;</span>)]</pre>

  <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">class</span> SampleInfo</pre>

  <pre>    {</pre>

  <pre class="alt">        <span class="rem">// The DataServiceKey attribute is necessary unless you name </span></pre>

  <pre>        <span class="rem">// this ID</span></pre>

  <pre class="alt">        <span class="kwrd">public</span> <span class="kwrd">int</span> Id { get; set; } </pre>

  <pre>        <span class="kwrd">public</span> <span class="kwrd">string</span> Title { get; set; }</pre>

  <pre class="alt">    }</pre>

  <pre>&#160;</pre>

  <pre class="alt">    <span class="kwrd">public</span> <span class="kwrd">class</span> SampleDataSource</pre>

  <pre>    {</pre>

  <pre class="alt">        <span class="kwrd">private</span> <span class="kwrd">readonly</span> List&lt;SampleInfo&gt; _sampleInfoList;</pre>

  <pre>&#160;</pre>

  <pre class="alt">        <span class="kwrd">public</span> SampleDataSource()</pre>

  <pre>        {</pre>

  <pre class="alt">            _sampleInfoList =</pre>

  <pre>                <span class="kwrd">new</span> List&lt;SampleInfo&gt;()</pre>

  <pre class="alt">                    {</pre>

  <pre>                        <span class="kwrd">new</span> SampleInfo {Id = 3, Title = <span class="str">&quot;3&quot;</span>},</pre>

  <pre class="alt">                        <span class="kwrd">new</span> SampleInfo {Id = 4, Title = <span class="str">&quot;4&quot;</span>},</pre>

  <pre>                        <span class="kwrd">new</span> SampleInfo {Id = 1, Title = <span class="str">&quot;1&quot;</span>},</pre>

  <pre class="alt">                        <span class="kwrd">new</span> SampleInfo {Id = 5, Title = <span class="str">&quot;5&quot;</span>},</pre>

  <pre>                        <span class="kwrd">new</span> SampleInfo {Id = 2, Title = <span class="str">&quot;2&quot;</span>}</pre>

  <pre class="alt">                    };</pre>

  <pre>        }</pre>

  <pre class="alt">&#160;</pre>

  <pre>        <span class="kwrd">public</span> IQueryable&lt;SampleInfo&gt; SampleInfoData</pre>

  <pre class="alt">        {</pre>

  <pre>            get { <span class="kwrd">return</span> _sampleInfoList.AsQueryable(); }</pre>

  <pre class="alt">        }</pre>

  <pre>    }</pre>

  <pre class="alt">&#160;</pre>

  <pre>    <span class="kwrd">public</span> <span class="kwrd">class</span> WcfDataService : DataService&lt;SampleDataSource&gt;</pre>

  <pre class="alt">    {</pre>

  <pre>        <span class="rem">// This method is called only once to initialize service-wide policies.</span></pre>

  <pre class="alt">        <span class="kwrd">public</span> <span class="kwrd">static</span> <span class="kwrd">void</span> InitializeService(DataServiceConfiguration config)</pre>

  <pre>        {</pre>

  <pre class="alt">            config.SetEntitySetAccessRule(<span class="str">&quot;*&quot;</span>, EntitySetRights.AllRead);</pre>

  <pre>            config.SetServiceOperationAccessRule(<span class="str">&quot;*&quot;</span>, ServiceOperationRights.All);</pre>

  <pre class="alt">            config.DataServiceBehavior.MaxProtocolVersion = </pre>

  <pre>                DataServiceProtocolVersion.V2;</pre>

  <pre class="alt">        }</pre>

  <pre>    }</pre>

  <pre class="alt">}</pre>
</div>

<div class="csharpcode">&#160;</div>

<div class="csharpcode">I’ve created a very simple web page that includes some simple JavaScript to display the results of calling this WCF DataService with and without $top.&#160; The page code is below.</div>

<div class="csharpcode">&#160;</div>

<div class="csharpcode">
  <pre class="alt"><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></pre>

  <pre>    <span class="kwrd">&lt;</span><span class="html">title</span><span class="kwrd">&gt;</span>Demonstration of OData Sort Based on Using $top<span class="kwrd">&lt;/</span><span class="html">title</span><span class="kwrd">&gt;</span></pre>

  <pre class="alt">&#160;</pre>

  <pre>     <span class="kwrd">&lt;</span><span class="html">script</span> <span class="attr">src</span><span class="kwrd">=&quot;http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js&quot;</span>    </pre>

  <pre class="alt">        <span class="attr">type</span><span class="kwrd">=&quot;text/javascript&quot;</span><span class="kwrd">&gt;&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span>  </pre>

  <pre>&#160;</pre>

  <pre class="alt">&#160;</pre>

  <pre>    &lt;script type=<span class="str">&quot;text/javascript&quot;</span> &gt;</pre>

  <pre class="alt">        <span class="rem">// Display the data with and without $top with OData</span></pre>

  <pre>        $(document).ready(<span class="kwrd">function</span> () {</pre>

  <pre class="alt">            loadData(<span class="str">&quot;/WcfDataService.svc/SampleInfoData&quot;</span>,<span class="str">&quot;#resultTableNoODataParms&quot;</span>);</pre>

  <pre>            loadData(<span class="str">&quot;/WcfDataService.svc/SampleInfoData?$top=5&quot;</span>, <span class="str">&quot;#resultTableWithODataParms&quot;</span>);</pre>

  <pre class="alt">        });</pre>

  <pre>&#160;</pre>

  <pre class="alt">        <span class="rem">// Called after DOM is ready to call service and display table data</span></pre>

  <pre>        <span class="kwrd">function</span> loadData(urlString,divName) {</pre>

  <pre class="alt">            <span class="kwrd">var</span> url = urlString; <span class="rem">// &quot;/WcfDataService.svc/SampleInfoData&quot;;</span></pre>

  <pre>            $.ajax({</pre>

  <pre class="alt">                type: <span class="str">&quot;GET&quot;</span>,</pre>

  <pre>                url: url,</pre>

  <pre class="alt">                contentType: <span class="str">&quot;application/json; charset=utf-8&quot;</span>,</pre>

  <pre>                dataType: <span class="str">&quot;json&quot;</span>,</pre>

  <pre class="alt">                success: <span class="kwrd">function</span> (msg) {</pre>

  <pre>                    loadTable(msg.d,divName);</pre>

  <pre class="alt">                }</pre>

  <pre>            });</pre>

  <pre class="alt">        }</pre>

  <pre>&#160;</pre>

  <pre class="alt">        <span class="rem">// display data at a given ID</span></pre>

  <pre>        <span class="kwrd">function</span> loadTable(results,divName) {</pre>

  <pre class="alt">            <span class="kwrd">var</span> table = <span class="str">'&lt;table border=1&gt;&lt;tbody&gt;&lt;tr&gt;'</span>;</pre>

  <pre>            <span class="kwrd">for</span> (<span class="kwrd">var</span> post <span class="kwrd">in</span> results) {</pre>

  <pre class="alt">                <span class="kwrd">var</span> row = <span class="str">''</span>;</pre>

  <pre>                row += <span class="str">'&lt;td&gt;'</span> + results[post].Id + <span class="str">'&lt;/td&gt;'</span>;</pre>

  <pre class="alt">                table += row;</pre>

  <pre>            }</pre>

  <pre class="alt">            table += <span class="str">'&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;'</span>;</pre>

  <pre>            $(divName).html(table);</pre>

  <pre class="alt">        }</pre>

  <pre>    <span class="kwrd">&lt;/</span><span class="html">script</span><span class="kwrd">&gt;</span></pre>

  <pre class="alt">&#160;</pre>

  <pre><span class="kwrd">&lt;/</span><span class="html">head</span><span class="kwrd">&gt;</span></pre>

  <pre class="alt"><span class="kwrd">&lt;</span><span class="html">body</span><span class="kwrd">&gt;</span></pre>

  <pre>    <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></pre>

  <pre class="alt">    <span class="kwrd">&lt;</span><span class="html">div</span><span class="kwrd">&gt;</span></pre>

  <pre>    <span class="kwrd">&lt;</span><span class="html">h2</span><span class="kwrd">&gt;</span>/WcfDataService.svc/SampleInfoData<span class="kwrd">&lt;/</span><span class="html">h2</span><span class="kwrd">&gt;</span></pre>

  <pre class="alt">    <span class="kwrd">&lt;</span><span class="html">div</span> <span class="attr">id</span><span class="kwrd">=&quot;resultTableNoODataParms&quot;</span> <span class="kwrd">/&gt;</span></pre>

  <pre>    <span class="kwrd">&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span></pre>

  <pre class="alt">    <span class="kwrd">&lt;</span><span class="html">hr</span> <span class="kwrd">/&gt;</span></pre>

  <pre>     <span class="kwrd">&lt;</span><span class="html">div</span><span class="kwrd">&gt;</span></pre>

  <pre class="alt">    <span class="kwrd">&lt;</span><span class="html">h2</span><span class="kwrd">&gt;</span>/WcfDataService.svc/SampleInfoData?$top=5<span class="kwrd">&lt;/</span><span class="html">h2</span><span class="kwrd">&gt;</span></pre>

  <pre>    <span class="kwrd">&lt;</span><span class="html">div</span> <span class="attr">id</span><span class="kwrd">=&quot;resultTableWithODataParms&quot;</span> <span class="kwrd">/&gt;</span></pre>

  <pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">div</span><span class="kwrd">&gt;</span></pre>

  <pre>&#160;</pre>

  <pre class="alt">    <span class="kwrd">&lt;/</span><span class="html">form</span><span class="kwrd">&gt;</span></pre>

  <pre><span class="kwrd">&lt;/</span><span class="html">body</span><span class="kwrd">&gt;</span></pre>

  <pre class="alt"><span class="kwrd">&lt;/</span><span class="html">html</span><span class="kwrd">&gt;</span></pre>
</div>
<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><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>As you can see, adding the $top=5 causes the data to be sorted.</p>

<p>Hope this helps!</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/03/31/odata-wcfdataservice-top-orderby-default-sort/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Small LINQ Trick for Converting List&lt;String&gt; to String Text List</title>
		<link>http://peterkellner.net/2010/02/24/linq-trick-foreach-stringbuilder/</link>
		<comments>http://peterkellner.net/2010/02/24/linq-trick-foreach-stringbuilder/#comments</comments>
		<pubDate>Wed, 24 Feb 2010 21:58:28 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[LINQ]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/02/24/linq-trick-foreach-stringbuilder/</guid>
		<description><![CDATA[Here is a shorthand way for converting a list of strings defined as follows:   List&#60;string&#62; strings = new List&#60;string&#62;
 
	{
		&#34;a&#34;,&#34;b&#34;,&#34;c&#34;

	};



.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 { [...]]]></description>
			<content:encoded><![CDATA[<p>Here is a shorthand way for converting a list of strings defined as follows:</p>  <pre class="csharpcode"> List&lt;<span class="kwrd">string</span>&gt; strings = <span class="kwrd">new</span> List&lt;<span class="kwrd">string</span>&gt;
 
	{
		<span class="str">&quot;a&quot;</span>,<span class="str">&quot;b&quot;</span>,<span class="str">&quot;c&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>

<p>To something that looks like:</p>
<span id="more-525"></span>

<p>“a:b:c:”</p>

<p>Here is the trick using the foreach syntax:</p>

<pre class="csharpcode"> var stringBuilder = <span class="kwrd">new</span> StringBuilder();
 strings.ForEach(a=&gt;stringBuilder.AppendFormat(<span class="str">&quot;{0}:&quot;</span>,a));</pre>

<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>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/02/24/linq-trick-foreach-stringbuilder/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Authentication and Authorization Using RIA Services (Article 7 of 7)</title>
		<link>http://peterkellner.net/2010/01/25/authentication-and-authorization-using-ria-services-article-7-of-7/</link>
		<comments>http://peterkellner.net/2010/01/25/authentication-and-authorization-using-ria-services-article-7-of-7/#comments</comments>
		<pubDate>Mon, 25 Jan 2010 23:25:12 +0000</pubDate>
		<dc:creator>Peter Kellner</dc:creator>
				<category><![CDATA[.NET 4.0]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Entity Framework]]></category>
		<category><![CDATA[RIA Services]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[VS2010]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>
		<category><![CDATA[RIA Service Presentation]]></category>
		<category><![CDATA[RIA Services Bay.Net Presentation]]></category>

		<guid isPermaLink="false">http://peterkellner.net/2010/01/25/authentication-and-authorization-using-ria-services-article-7-of-7/</guid>
		<description><![CDATA[               &#160;       Title Of Each Article       Video Included With Each Post                 Part 1 [...]]]></description>
			<content:encoded><![CDATA[<br /><table width="90%">   <tbody>     <tr>       <td width="70">&nbsp;</td>       <td>Title Of Each Article</td>       <td width="150" style="width: 100px;">Video Included With Each Post</td>     </tr>     <tr>       <td width="70">Part 1</td>       <td><a href="/2010/01/20/riaservices-silverlight-4-tutorial-svcc-part1of7-introduction/">Introduction To RIA Services In Silverlight (This Article)</a></td>       <td width="150" style="width: 100px;">7 Minutes</td>     </tr>     <tr>       <td width="70">Part 2</td>       <td><a href="/2010/01/25/basic-ria-services-and-datagrid-with-vs-2010-tooling-article-2-of-7/">Basic RIA Services And DataGrid With&nbsp; VS 2010 Tooling</a></td>       <td width="150" style="width: 100px;">14 Minutes</td>     </tr>     <tr>       <td width="70">Part 3</td>       <td><a href="/2010/01/23/adding-a-datagrid-with-connect-the-dots-databinding-in-vs-2010-article-3-of-7/">Adding A DataGrid With Connect The Dots DataBinding in VS 2010</a></td>       <td width="150" style="width: 100px;">13 Minutes</td>     </tr>     <tr>       <td width="70">Part 4</td>       <td><a href="/2010/01/25/adding-a-navigation-page-to-a-silverlight-business-application-template-article-4-of-7/">Adding a Navigation Page to a Silverlight Business Application Template</a></td>       <td width="150" style="width: 100px;">11 Minutes</td>     </tr>     <tr>       <td width="70">Part 5</td>       <td><a href="/2010/01/25/adding-speakers-page-template-with-converters-in-visual-studio-2010-beta2-article-5-of-7/">Adding Speakers Page Template With Converters In Visual Studio 2010 Beta2</a></td>       <td width="150" style="width: 100px;">11 Minutes</td>     </tr>     <tr>       <td width="70">Part 6</td>       <td><a href="/2010/01/25/adding-a-sessions-page-that-includes-a-query-parameter-in-silverlight-vs2010-article-6-of-7/">Adding A Sessions Page That Includes a Query Parameter In Silverlight VS2010 Beta2</a></td>       <td width="150" style="width: 100px;">10 Minutes</td>     </tr>     <tr>       <td width="70">Part 7</td>       <td><a href="/2010/01/25/authentication-and-authorization-using-ria-services-article-7-of-7/">Basic Authentication and Authorization In RIA Services</a></td>       <td width="150" style="width: 100px;">5 Minutes</td>     </tr> </tbody></table><br />  <br /><img src="http://video.peterkellner.net/video/RIAServicesBayNet20090120/P7_AuthenticationAndAuthorization_Thumb.jpg" alt="media" /><br />
   <p>This article is very short.&#160; In the actual presentation, there was not much time to talk about this so a brief overview was done.&#160; Basically, it’s all standard WCF stuff.&#160; The idea is that the Silverlight Business Template adds logging in and supports Authentication just like an asp.net application does.&#160; It uses the DomainDataSource to do the bridging between the silverlight clientside app, and the web application.&#160; Authorization is built into the login module also so that you can assign attributes to your domainservice classes to restrict access.</p> <span id="more-484"></span> <p>That is, if you only want people who are logged in able to access one of your methods, you simply tag it as follows with the [RequiresAuthentication] attribute.</p>  <p><a href="http://peterkellner.net/FilesForWebDownload/AuthenticationandAuthorizationUsingRIASe_E1CB/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/AuthenticationandAuthorizationUsingRIASe_E1CB/image_thumb.png" width="694" height="222" /></a></p>  <p>If you only want a certain logged in user role to be able to access a certain method, you tag it as follows</p>  <p><a href="http://peterkellner.net/FilesForWebDownload/AuthenticationandAuthorizationUsingRIASe_E1CB/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/AuthenticationandAuthorizationUsingRIASe_E1CB/image_thumb_3.png" width="710" height="151" /></a></p>  <p>This of course solve the server side only.&#160; On the client side, you need to call methods that are exposed through the domain data.&#160; <a title="http://www.silverlightshow.net/items/Silverlight-3-with-Ria-Authentication-service.aspx" href="http://www.silverlightshow.net/items/Silverlight-3-with-Ria-Authentication-service.aspx">http://www.silverlightshow.net/items/Silverlight-3-with-Ria-Authentication-service.aspx</a> discusses much of this on the client side.</p>]]></content:encoded>
			<wfw:commentRss>http://peterkellner.net/2010/01/25/authentication-and-authorization-using-ria-services-article-7-of-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	<enclosure url="http://video.peterkellner.net/video/RIAServicesBayNet20090120/P7_AuthenticationAndAuthorization.flv" length="1" type="video/x-flv"/>
	</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 3/13 queries in 0.159 seconds using disk

Served from: peterkellner.net @ 2012-02-08 19:53:10 -->
