<?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>Tallan&#039;s Technology Blog &#187; .NET Framework</title>
	<atom:link href="http://blog.tallan.com/tag/net-framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.tallan.com</link>
	<description>Tallan&#039;s Top Technologists Share Their Thoughts on Today&#039;s Technology Challenges</description>
	<lastBuildDate>Mon, 06 Feb 2012 02:15:46 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Automatically log errors with Elmah in Script0Service-WebService calls</title>
		<link>http://blog.tallan.com/2012/02/03/automatically-log-errors-with-elmah-in-script0service-webservice-calls/</link>
		<comments>http://blog.tallan.com/2012/02/03/automatically-log-errors-with-elmah-in-script0service-webservice-calls/#comments</comments>
		<pubDate>Fri, 03 Feb 2012 17:23:33 +0000</pubDate>
		<dc:creator>Karl Schwirz</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[.net]]></category>

		<guid isPermaLink="false">http://blog.tallan.com/2012/02/03/automatically-log-errors-with-elmah-in-script0service-webservice-calls/</guid>
		<description><![CDATA[If you’ve used Elmah logging in the past then you know it’s a very useful and easy to set up tool in your web applications.&#160; However, when we encounter errors in web services we sometimes do not get the desired logging into the xml file or database, depending on how you have Elmah configured.&#160; This [...]]]></description>
			<content:encoded><![CDATA[<p>If you’ve used Elmah logging in the past then you know it’s a very useful and easy to set up tool in your web applications.&#160; However, when we encounter errors in web services we sometimes do not get the desired logging into the xml file or database, depending on how you have Elmah configured.&#160; This is because when a error occurs in a script service enabled web service, the errors get serialized into JSON and returned to the ajax caller which is then take by the javascript to handle the exception.&#160; The application is left unaware of the exception occurrence. </p>
<p>There is a way however, to intercept the message before it is returned to the front-end and log the error automatically the way we are used to with Elmah.</p>
<p>First we set up an ErrorHandlerFilter class which will override the default error stream message ( {“Message”:”There was an error processing the request.”, “StackTrace”:””, “ExceptionType”:””})} with a more meaningful error message with details.</p>
<div>
<div>
<pre><span style="color: #606060">   1:</span> <span style="color: #0000ff">using</span> System;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   2:</span> <span style="color: #0000ff">using</span> System.Collections.Generic;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   3:</span> <span style="color: #0000ff">using</span> System.IO;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   4:</span> <span style="color: #0000ff">using</span> System.Linq;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   5:</span> <span style="color: #0000ff">using</span> System.Text;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   6:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   7:</span> <span style="color: #0000ff">namespace</span> Namespace.ElmahExtension</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   8:</span> {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   9:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ErrorHandlerFilter : Stream</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  10:</span>     {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  11:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  12:</span>         <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> Stream _responseFilter;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  13:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  14:</span>         <span style="color: #0000ff">public</span> List&lt;<span style="color: #0000ff">byte</span>&gt; OriginalBytesWritten { get; <span style="color: #0000ff">private</span> set; }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  15:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  16:</span>         <span style="color: #0000ff">private</span> <span style="color: #0000ff">const</span> <span style="color: #0000ff">string</span> Content =</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  17:</span>           <span style="color: #006080">&quot;{\&quot;Message\&quot;:\&quot;There was an error processing the request.\&quot;&quot;</span> +</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  18:</span>           <span style="color: #006080">&quot;,\&quot;StackTrace\&quot;:\&quot;\&quot;,\&quot;ExceptionType\&quot;:\&quot;\&quot;}&quot;</span>;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  19:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  20:</span>         <span style="color: #0000ff">public</span> ErrorHandlerFilter(Stream responseFilter)</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  21:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  22:</span>             _responseFilter = responseFilter;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  23:</span>             OriginalBytesWritten = <span style="color: #0000ff">new</span> List&lt;<span style="color: #0000ff">byte</span>&gt;();</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  24:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  25:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  26:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Flush()</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  27:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  28:</span>             <span style="color: #0000ff">byte</span>[] bytes = Encoding.UTF8.GetBytes(Content);</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  29:</span>             _responseFilter.Write(bytes, 0, bytes.Length);</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  30:</span>             _responseFilter.Flush();</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  31:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  32:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  33:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">long</span> Seek(<span style="color: #0000ff">long</span> offset, SeekOrigin origin)</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  34:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  35:</span>             <span style="color: #0000ff">return</span> _responseFilter.Seek(offset, origin);</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  36:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  37:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  38:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> SetLength(<span style="color: #0000ff">long</span> <span style="color: #0000ff">value</span>)</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  39:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  40:</span>             _responseFilter.SetLength(<span style="color: #0000ff">value</span>);</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  41:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  42:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  43:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">int</span> Read(<span style="color: #0000ff">byte</span>[] buffer, <span style="color: #0000ff">int</span> offset, <span style="color: #0000ff">int</span> count)</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  44:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  45:</span>             <span style="color: #0000ff">return</span> _responseFilter.Read(buffer, offset, count);</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  46:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  47:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  48:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">void</span> Write(<span style="color: #0000ff">byte</span>[] buffer, <span style="color: #0000ff">int</span> offset, <span style="color: #0000ff">int</span> count)</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  49:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  50:</span>             <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = offset; i &lt; offset + count; i++)</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  51:</span>             {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  52:</span>                 OriginalBytesWritten.Add(buffer[i]);</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  53:</span>             }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  54:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  55:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  56:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> CanRead</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  57:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  58:</span>             get { <span style="color: #0000ff">return</span> _responseFilter.CanRead; }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  59:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  60:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  61:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> CanSeek</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  62:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  63:</span>             get { <span style="color: #0000ff">return</span> _responseFilter.CanSeek; }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  64:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  65:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  66:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> CanWrite</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  67:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  68:</span>             get { <span style="color: #0000ff">return</span> _responseFilter.CanWrite; }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  69:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  70:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  71:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">long</span> Length</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  72:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  73:</span>             get { <span style="color: #0000ff">return</span> _responseFilter.Length; }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  74:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  75:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  76:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">long</span> Position</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  77:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  78:</span>             get { <span style="color: #0000ff">return</span> _responseFilter.Position; }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  79:</span>             set { _responseFilter.Position = <span style="color: #0000ff">value</span>; }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  80:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  81:</span>     }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  82:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Now, we’re going to need to set up a class that implements I<em>HttpModule.&#160; </em>which we’ll use to intercept the message being passed after the <em>Request </em>and <em>EndRequest</em> handlers execute.</p>
<div>
<div>
<pre><span style="color: #606060">   1:</span> <span style="color: #0000ff">using</span> System;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   2:</span> <span style="color: #0000ff">using</span> System.Diagnostics;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   3:</span> <span style="color: #0000ff">using</span> System.IO;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   4:</span> <span style="color: #0000ff">using</span> System.Text;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   5:</span> <span style="color: #0000ff">using</span> System.Web;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   6:</span> <span style="color: #0000ff">using</span> System.Web.Services.Protocols;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   7:</span> <span style="color: #0000ff">using</span> Elmah;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   8:</span>  </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   9:</span> <span style="color: #0000ff">namespace</span> Namespace.ElmahExtension</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  10:</span> {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  11:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ElmahExtension : IHttpModule</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  12:</span>     {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  13:</span>         <span style="color: #0000ff">private</span> HttpApplication _application;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  14:</span>  </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  15:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Dispose()</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  16:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  17:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  18:</span>  </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  19:</span>         <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Init(HttpApplication context)</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  20:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  21:</span>             _application = context;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  22:</span>             _application.PostRequestHandlerExecute += OnPostRequestHandlerExecute;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  23:</span>             _application.EndRequest += OnEndRequest;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  24:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  25:</span>  </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  26:</span>         <span style="color: #0000ff">private</span> <span style="color: #0000ff">void</span> OnPostRequestHandlerExecute(<span style="color: #0000ff">object</span> sender, EventArgs e)</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  27:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  28:</span>             var context = sender <span style="color: #0000ff">as</span> HttpApplication;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  29:</span>             <span style="color: #0000ff">if</span>(context != <span style="color: #0000ff">null</span> &amp;&amp; (context.Request.Path.Contains(<span style="color: #006080">&quot;.asmx&quot;</span>) &amp;&amp; (context.Response.StatusCode &gt;= 400 &amp;&amp; context.Response.StatusCode &lt; 600)))</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  30:</span>             {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  31:</span>                 context.Response.Filter = <span style="color: #0000ff">new</span> ErrorHandlerFilter(context.Response.Filter);</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  32:</span>             }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  33:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  34:</span>  </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  35:</span>         <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> OnEndRequest(<span style="color: #0000ff">object</span> sender, EventArgs e) </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  36:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  37:</span>             var context = (HttpApplication)sender;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  38:</span>             var errorHandlerFilter = context.Response.Filter <span style="color: #0000ff">as</span> ErrorHandlerFilter;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  39:</span>  </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  40:</span>             <span style="color: #0000ff">if</span> (errorHandlerFilter == <span style="color: #0000ff">null</span>)</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  41:</span>             {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  42:</span>                 <span style="color: #0000ff">return</span>;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  43:</span>             }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  44:</span>  </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  45:</span>             var originalContent = Encoding.UTF8.GetString(errorHandlerFilter.OriginalBytesWritten.ToArray());</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  46:</span>             ErrorLog.GetDefault(context.Context).Log(<span style="color: #0000ff">new</span> Error(<span style="color: #0000ff">new</span> Exception(originalContent)));</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  47:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  48:</span>     }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  49:</span>  </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  50:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>Notice in the <em>OnPostRequestHandlerExecute </em>function we’re intercepting any request that comes from a Webservice (.asmx page) and has a status code of greater than 400 (All error code types).&#160; At his point we want to set the ErrorHandelerFilter to the current Response.Filter value.</p>
<p>Now in the OnEndRequest function we want to get the current context and set the error handler based on the filter which should now be persisting the values we set in the previous function.&#160; Once we have the error values all we have to do is call the Elmah manual logging API and log the exception. </p>
<p>The final piece you have to do is set up the webConfig to recognize and use your I<em>HttpModule </em>code. n In your config file navigate down to System.Web &gt;&gt; HttpModules and fill in the attribute accordingly.</p>
<div>
<div>
<pre><span style="color: #606060">   1:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">system.web</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   2:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">httpModules</span><span style="color: #0000ff">&gt;</span>    </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   3:</span>       <span style="color: #0000ff">&lt;</span><span style="color: #800000">add</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">=&quot;ElmahExtension&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;Namespace.ElmahExtension.ElmahExtension&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   4:</span>     <span style="color: #0000ff">&lt;/</span><span style="color: #800000">httpModules</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   5:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">system.web</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></div>
</div>
<p>Once you compile and run, any errors encountered in your web services should now be logged by Elmah just like any others.</p>
<p>&#160;</p>
<p>Thanks to Reddy Kadasani, Mike Gerety and Dylan Barrett for their contributions to this solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tallan.com/2012/02/03/automatically-log-errors-with-elmah-in-script0service-webservice-calls/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Calling an ASP.Net web service using jQuery and JSON.</title>
		<link>http://blog.tallan.com/2012/01/29/calling-an-asp-net-web-service-using-jquery-and-json/</link>
		<comments>http://blog.tallan.com/2012/01/29/calling-an-asp-net-web-service-using-jquery-and-json/#comments</comments>
		<pubDate>Sun, 29 Jan 2012 19:09:27 +0000</pubDate>
		<dc:creator>Karl Schwirz</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Enterprise .NET]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://blog.tallan.com/2012/01/29/calling-an-asp-net-web-service-using-jquery-and-json/</guid>
		<description><![CDATA[Let’s say you have a webpage where you need to call a service but cannot perform a post back.&#160; Recently I was on a client engagement where we needed to improve page performance by dynamically loading a navigation tree with a potential for several thousand links.&#160; We implemented a solution that would load each branch [...]]]></description>
			<content:encoded><![CDATA[<p>Let’s say you have a webpage where you need to call a service but cannot perform a post back.&#160; Recently I was on a client engagement where we needed to improve page performance by dynamically loading a navigation tree with a potential for several thousand links.&#160; We implemented a solution that would load each branch as the user clicked on the expanding icon by calling a web service via ajax and passing the required links back to the page and render them client side.&#160; This saved immensely on the load time for the page and improved the user experience </p>
<p>This kind of solution can be applied to several situations such as complex data processing, dynamic loading, or combining them all into a seamless form submission process that would clear the form on post backs. </p>
<p>We can accomplish this process simply by grabbing an event, such as a button click, and collecting all the required information using jQuery.&#160; Next using a JSON call, we can pass all the parameters to the web service and execute our code from there.</p>
<p>Here, I’ll start with a simple .aspx page with jquery, a textbox, and link that will be used to call the web service.</p>
<div>
<div>
<pre><span style="color: #606060">   1:</span> <span style="background-color: #ffff00">&lt;%@ Master Language=&quot;C#&quot; MasterPageFile=&quot;~/Site.Master&quot; AutoEventWireup=&quot;true&quot; CodeBehind=&quot;ThisPage.cs&quot; Inherits=&quot;ThisSolution.ThisProject.ThisFolder&quot; %&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   2:</span>  </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   3:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:Content</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;Content1&quot;</span> <span style="color: #ff0000">ContentPlaceHolderID</span><span style="color: #0000ff">=&quot;HeadContent&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   4:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">script</span> <span style="color: #ff0000">src</span><span style="color: #0000ff">=&quot;/Scripts/jquery-1.4.1.min.js&quot;</span> <span style="color: #ff0000">type</span><span style="color: #0000ff">=&quot;text/javascript&quot;</span><span style="color: #0000ff">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   5:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">asp:Content</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   6:</span>  </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   7:</span> <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:Content</span> <span style="color: #ff0000">ID</span><span style="color: #0000ff">=&quot;Content2&quot;</span> <span style="color: #ff0000">ContentPlaceHolderID</span><span style="color: #0000ff">=&quot;MainContent&quot;</span> <span style="color: #ff0000">runat</span><span style="color: #0000ff">=&quot;server&quot;</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   8:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:TextBox</span> <font color="#ff0000">ID</font>=”<font color="#0000ff">textBox</font>” <span style="color: #ff0000">TextMode</span><span style="color: #0000ff">=&quot;MultiLine&quot;</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:TextBox</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></p>
<pre>

<span style="color: #606060">   9:</span>     <span style="color: #0000ff">&lt;</span><span style="color: #800000">a</span> <span style="color: #ff0000">href</span><span style="color: #0000ff">=&quot;#&quot;</span> <span style="color: #ff0000">class</span><span style="color: #0000ff">=&quot;buttonClass&quot;</span><span style="color: #0000ff">&gt;</span> Save <span style="color: #0000ff">&lt;/</span><span style="color: #800000">a</span><span style="color: #0000ff">&gt;</span>

<span style="color: #0000ff">  <font color="#a5a5a5">10</font>:     <span style="color: #0000ff">&lt;</span><span style="color: #800000">asp:Label</span> <font color="#ff0000">ID</font>=”<font color="#0000ff">successLabel</font>” <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></span>
</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  11:</span> <span style="color: #0000ff">&lt;/</span><span style="color: #800000">asp:Content</span><span style="color: #0000ff">&gt;</span></pre>
<p><!--CRLF--></div>
</div>
<p>Next, you will need to establish your web service and create an .asmx file.&#160; There are a couple of important things to note here. Line 5 will be by default commented out.&#160; So in order for us to make our JSON call we’re going to need this <em>uncommented</em>.&#160; Also, notice I’m returning a string.&#160; For the purposes of this demo I am going to return a string to the UI and display a message, but you can return any kind of object that can be serialized down to a primitive data type.</p>
<div>
<div>
<pre><span style="color: #606060">   1:</span> [WebService(Namespace = <span style="color: #006080">&quot;http://tempuri.org/&quot;</span>)]</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   2:</span> [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   3:</span> [System.ComponentModel.ToolboxItem(<span style="color: #0000ff">false</span>)]</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   4:</span> <span style="color: #008000">// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. </span></pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   5:</span> [System.Web.Script.Services.ScriptService]</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   6:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> wService : System.Web.Services.WebService</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   7:</span> {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   8:</span>     [WebMethod()]</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   9:</span>     [ScriptMethod(UseHttpGet = <span style="color: #0000ff">false</span>, ResponseFormat = ResponseFormat.Json)]</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  10:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> UpdateComment(<span style="color: #0000ff">string</span> textBoxText)</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  11:</span>     {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  12:</span>         var success = Service.CallToDb(textBoxText);</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  13:</span>  </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  14:</span>         <span style="color: #0000ff">if</span>(success)</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  15:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  16:</span>             <span style="color: #0000ff">return</span> <span style="color: #006080">&quot;Call to Database using text was successful&quot;</span>;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  17:</span>         }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  18:</span>         <span style="color: #0000ff">else</span> </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  19:</span>         {</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  20:</span>             <span style="color: #0000ff">return</span> <span style="color: #006080">&quot;Call to Database using text was not successful&quot;</span>;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  21:</span>         }    </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  22:</span>     }           </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  23:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>I use a jQuery selector to pick up on the mouse click of the link and gather our data into an javascript object.&#160; Note that when passing parameters through this object you have to name them the same way as you do in the parameter declaration of the web service.</p>
<p>When setting up the call to the service you’re going to want to remember to call the “JSON.stringify” function on your data object so that it serializes properly and that your contentType as well as dataType are configured properly as well.</p>
<div>
<div>
<pre><span style="color: #606060">   1:</span> &lt;script type=<span style="color: #006080">'text/javascript&gt;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   2:</span>     $('</span>.buttonClass<span style="color: #006080">').live('</span>click<span style="color: #006080">', function(){</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   3:</span>         var text = $('</span>#textBox<span style="color: #006080">').text();</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   4:</span>         var data = new Object();</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   5:</span>         data.textBoxText = text;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   6:</span>  </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   7:</span>         $.ajax({</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   8:</span>             type: &quot;POST&quot;,</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">   9:</span>             url: &quot;/Ajax/WbServcie.asmx/UpdateComment&quot;,</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  10:</span>             data: JSON.stringify(data),</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  11:</span>             contentType: &quot;application/json; charset=utf-8&quot;,</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  12:</span>             dataType: &quot;json&quot;,</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  13:</span>             success: function(msg){</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  14:</span>                 $('</span>#successLabel<span style="color: #006080">').val(msg);</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  15:</span>             },</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  16:</span>             error: function(msg){</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  17:</span>                 alert('</span>Something, somewhere, has gone terribly wrong');</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  18:</span>                 <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  19:</span>             }</pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  20:</span>         });    </pre>
<p><!--CRLF--></p>
<pre><span style="color: #606060">  21:</span> &lt;/script&gt;</pre>
<p><!--CRLF--></div>
</div>
<p>Once you get a response from the service, you can set any of your data that is returned.&#160; In this case, we’re returning a string indicating the call to the database was successful and will set a label on the page to show that.</p>
<p>And that’s it.&#160; With just a few simple configurations we have a quick call back to the server without having to run an expensive post back. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tallan.com/2012/01/29/calling-an-asp-net-web-service-using-jquery-and-json/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing SharePoint Default ASP.NET compiler to use ASP.NET 3.5</title>
		<link>http://blog.tallan.com/2011/12/31/changing-sharepoint-default-asp-net-compiler-to-use-asp-net-3-5/</link>
		<comments>http://blog.tallan.com/2011/12/31/changing-sharepoint-default-asp-net-compiler-to-use-asp-net-3-5/#comments</comments>
		<pubDate>Sat, 31 Dec 2011 14:08:00 +0000</pubDate>
		<dc:creator>Michael Gerety</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[SharePoint]]></category>
		<category><![CDATA[SharePoint 2010]]></category>
		<category><![CDATA[SharePoint Foundation]]></category>
		<category><![CDATA[SharePoint Server]]></category>
		<category><![CDATA[development]]></category>

		<guid isPermaLink="false">http://blog.tallan.com/2011/12/31/changing-sharepoint-default-asp-net-compiler-to-use-asp-net-3-5/</guid>
		<description><![CDATA[Overview
While SharePoint 2010 supports .NET 3.5, it uses the .NET 2.0 compiler by default for ASP.NET pages.&#160; If you write any UserControls or consume any DLLs in your hosted ASPX pages that utilizes .NET 3.5 features such as the var keyword, extension methods, and LINQ, you’ll get compilation error messages when attempting to access those [...]]]></description>
			<content:encoded><![CDATA[<h2>Overview</h2>
<p>While SharePoint 2010 supports .NET 3.5, it uses the .NET 2.0 compiler by default for ASP.NET pages.&#160; If you write any UserControls or consume any DLLs in your hosted ASPX pages that utilizes .NET 3.5 features such as the var keyword, extension methods, and LINQ, you’ll get compilation error messages when attempting to access those pages.</p>
<p>To resolve this issue, you can edit the web.config in your site collection’s virtual directory (Usually under \inetpub\wwwroot\wss\virtualdirectories\&lt;site&gt;).&#160; Add the following snippet under the configuration element:</p>
<p>&#160;</p>
<pre><span class="kwrd">&lt;</span><span class="html">system.codedom</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;</span><span class="html">compilers</span><span class="kwrd">&gt;</span>
    <span class="kwrd">&lt;</span><span class="html">compiler</span> <span class="attr">language</span><span class="kwrd">=&quot;c#;cs;csharp&quot;</span> <span class="attr">extension</span><span class="kwrd">=&quot;.cs&quot;</span> <span class="attr">warningLevel</span><span class="kwrd">=&quot;4&quot;</span>
              <span class="attr">type</span><span class="kwrd">=&quot;Microsoft.CSharp.CSharpCodeProvider, System, Version=2.0.0.0,
                    Culture=neutral, PublicKeyToken=b77a5c561934e089&quot;</span><span class="kwrd">&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">providerOption</span> <span class="attr">name</span><span class="kwrd">=&quot;CompilerVersion&quot;</span> <span class="attr">value</span><span class="kwrd">=&quot;v3.5&quot;</span> <span class="kwrd">/&gt;</span>
      <span class="kwrd">&lt;</span><span class="html">providerOption</span> <span class="attr">name</span><span class="kwrd">=&quot;WarnAsError&quot;</span> <span class="attr">value</span><span class="kwrd">=&quot;false&quot;</span> <span class="kwrd">/&gt;</span>
    <span class="kwrd">&lt;/</span><span class="html">compiler</span><span class="kwrd">&gt;</span>
  <span class="kwrd">&lt;/</span><span class="html">compilers</span><span class="kwrd">&gt;</span>
<span class="kwrd">&lt;/</span><span class="html">system.codedom</span><span class="kwrd">&gt;</span> </pre>
<p>
.csharpcode, .csharpcode pre<br />
{<br />
	font-size: small;<br />
	color: black;<br />
	font-family: consolas, &#8220;Courier New&#8221;, courier, monospace;<br />
	background-color: #ffffff;<br />
	/*white-space: pre;*/<br />
}<br />
.csharpcode pre { margin: 0em; }<br />
.csharpcode .rem { color: #008000; }<br />
.csharpcode .kwrd { color: #0000ff; }<br />
.csharpcode .str { color: #006080; }<br />
.csharpcode .op { color: #0000c0; }<br />
.csharpcode .preproc { color: #cc6633; }<br />
.csharpcode .asp { background-color: #ffff00; }<br />
.csharpcode .html { color: #800000; }<br />
.csharpcode .attr { color: #ff0000; }<br />
.csharpcode .alt<br />
{<br />
	background-color: #f4f4f4;<br />
	width: 100%;<br />
	margin: 0em;<br />
}<br />
.csharpcode .lnum { color: #606060; }</p>
<p>Also, look under the Assemblies section and make sure you have the following entries:</p>
<div>
<div class="csharpcode">
<pre><span class="lnum">   1:</span> <span class="kwrd">&lt;</span><span class="html">add</span> <span class="attr">assembly</span><span class="kwrd">=&quot;System.Web.Extensions, Version=3.5.0.0,Culture=neutral, PublicKeyToken=31BF3856AD364E35&quot;</span> <span class="kwrd">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span class="lnum">   2:</span> <span class="kwrd">&lt;</span><span class="html">add</span> <span class="attr">assembly</span><span class="kwrd">=&quot;System.Xml.Linq, Version=3.5.0.0,Culture=neutral, PublicKeyToken=B77A5C561934E089&quot;</span> <span class="kwrd">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span class="lnum">   3:</span> <span class="kwrd">&lt;</span><span class="html">add</span> <span class="attr">assembly</span><span class="kwrd">=&quot;Microsoft.SharePoint, Version=14.0.0.0,Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;</span> <span class="kwrd">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span class="lnum">   4:</span> <span class="kwrd">&lt;</span><span class="html">add</span> <span class="attr">assembly</span><span class="kwrd">=&quot;System.Web.Extensions, Version=3.5.0.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35&quot;</span> <span class="kwrd">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span class="lnum">   5:</span> <span class="kwrd">&lt;</span><span class="html">add</span> <span class="attr">assembly</span><span class="kwrd">=&quot;Microsoft.Web.CommandUI, Version=14.0.0.0,Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;</span> <span class="kwrd">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span class="lnum">   6:</span> <span class="kwrd">&lt;</span><span class="html">add</span> <span class="attr">assembly</span><span class="kwrd">=&quot;Microsoft.SharePoint.Search, Version=14.0.0.0,Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;</span> <span class="kwrd">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span class="lnum">   7:</span> <span class="kwrd">&lt;</span><span class="html">add</span> <span class="attr">assembly</span><span class="kwrd">=&quot;Microsoft.Office.Access.Server.UI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;</span> <span class="kwrd">/&gt;</span></pre>
<p><!--CRLF--></p>
<pre><span class="lnum">   8:</span> <span class="kwrd">&lt;</span><span class="html">add</span> <span class="attr">assembly</span><span class="kwrd">=&quot;Microsoft.SharePoint.Publishing, Version=14.0.0.0,Culture=neutral, PublicKeyToken=71e9bce111e9429c&quot;</span> <span class="kwrd">/&gt;</span></pre>
<p><!--CRLF--></div>
</div>
<p>&#160;</p>
<p><em>Note: You can also add other .NET 3.5 assemblies here as needed.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tallan.com/2011/12/31/changing-sharepoint-default-asp-net-compiler-to-use-asp-net-3-5/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Three .NET Best Practices to Keep in Mind</title>
		<link>http://blog.tallan.com/2011/06/29/three-net-best-practices-to-keep-in-mind/</link>
		<comments>http://blog.tallan.com/2011/06/29/three-net-best-practices-to-keep-in-mind/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 19:37:54 +0000</pubDate>
		<dc:creator>Michael Gerety</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Best Practices]]></category>

		<guid isPermaLink="false">http://blog.tallan.com/?p=1173</guid>
		<description><![CDATA[Practice #1: Catch Exceptions at the Highest Possible Level (Tier)
In general, exceptions should be bubbled up to the highest possible level to be caught and processed.  For example, in our project, a search goes through the following logical tiers:
UI -&#62; Services -&#62; Data/SharePoint/Refinement -&#62; Web Services
There are quite a few instances where we’re catching exceptions [...]]]></description>
			<content:encoded><![CDATA[<h1>Practice #1: Catch Exceptions at the Highest Possible Level (Tier)</h1>
<p>In general, exceptions should be bubbled up to the highest possible level to be caught and processed.  For example, in our project, a search goes through the following logical tiers:</p>
<p>UI -&gt; Services -&gt; Data/SharePoint/Refinement -&gt; Web Services</p>
<p>There are quite a few instances where we’re catching exceptions in the Data or Services layer, logging them using common logging components or configurations, and then swallowing the exception one way or another.</p>
<p>This is a bad practice for few reasons:</p>
<ul>
<li>This behavior is not transparent to developers that may be using the DLL but do not have access to the source code.</li>
<li> Logging from lower tier components is generally a bad idea, as it either requires a static logging location embedded in the dll or creates a dependency on the UI to provide pre-determined logging mechanism (i.e. EntLib).   (This can get especially ugly with EntLib, and EntLib Logging and ExceptionHandling blocks generally use named “policies” for handling and logging exceptions, and referencing them from a middle-tier component requires any UI consuming it to implement the exact same logging configuration.</li>
<li>Applications consuming the DLL do not know about or are not given the ability to choose how to react to exceptions in the code.</li>
<li>Swallowing exceptions dilutes the effectiveness of unit tests testing these functions, as tests can’t explicitly test for scenarios that might cause specific exceptions,</li>
<li>Swallowing exceptions can make debugging issues a nightmare (if not impossible) for developers that might consume this middle-tier component and not have access to the source code.</li>
<li>Catch blocks have an inherent performance cost.  Catching exceptions unnecessarily results in unnecessary performance degredataion.</li>
</ul>
<p>There are a few legitimate cases where this best practice might not be followed:</p>
<p><strong>1. </strong><strong>The Exception contains potentially sensitive information</strong></p>
<p>If the exception or exception message contains potentially sensitive information such as user credentials, server names, or any other sensitive information, it likely makes sense to catch the exception, create an exception that accurately describes the issue without the sensitive information, then throw that exception.  <em>Note however, there are very few scenarios where this should not occur at the UI layer.</em></p>
<p><em> </em></p>
<p>2. <strong>The exception is too vague or needs more metadata to be meaningful to the code consuming it.</strong></p>
<p>If the exception by itself is too vague or requires rebadging or augmented metadata to be useful to the code consuming it, it generally makes sense to wrap the exception by creating an exception that allows the original exception to be set as an “InnerException”, and augments the original exception with more information.  This new exception should then be thrown.</p>
<p><strong>3. </strong><strong>The exception that was caught should not affect the execution of the code</strong></p>
<p>If an exception occurs that should not affect the execution of the code, it may be swallowed or ignored in limited cases.  However, exceptions really should not be being thrown unless there is a severe enough issue to potentially interrupt program execution, so I’d suggest reviewing the code throwing the exception you are swallowing in this case.</p>
<p>4. <strong>The Exception Occurs At A Physical Tier Boundary</strong></p>
<p>Your multi-tier applications may not only contain multiple logical tiers, but physical tiers as well.  (for example, your web application may rely on a web service layer on a separate physical server).  While your web service layer may be “middle-tier” as far as the overall application is concerned, exceptions should be caught, processed, and logged at the boundary to that physical tier.  Logging on the machine where the services resides aides in issue resolution, and it gives an opportunity to review exceptions for sensitive information before sending them over the network, where they may be intercepted.</p>
<h1>Practice #2: You Should (Almost Never) Catch System.Exception</h1>
<p>You should almost never write the following:</p>
<div class="wlWriterEditableSmartContent" style="margin: 0px;float: none;padding: 0px">
<div style="border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt">
<div style="background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px">Code Snippet</div>
<div style="background: #ddd;overflow: auto">
<ol style="background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px">
<li><span style="color: #0000ff">try</span></li>
<li>{</li>
<li> <span style="color: #008000">//Some code here.</span></li>
<li>}</li>
<li><span style="color: #0000ff">catch</span> (System.<span style="color: #2b91af">Exception</span> ex)</li>
<li>{</li>
<li> <span style="color: #008000">//Do something with this exception</span></li>
<li>}</li>
</ol>
</div>
</div>
</div>
<p>This code says that you don’t know what type of exception to expect, so catch them all.</p>
<p>Any exception that the code is not expecting is, by definition, an unexpected or unhandled exception.  <em>You probably want your application to crash (or gracefully exit) with an “Unhandled Exception” message if an unexpected exception occurs! </em>How can you be certain of the stability of the currently running instance of your code if an exception occurred somewhere that you didn’t expect?</p>
<p>Also, catching specific exceptions helps greatly in diagnosing issues, and displaying useful and meaningful error messages to your users.  I think we all know how frustrating a generic error message such as “An error occurred.” can be.</p>
<p>If you think an exception might occur but you don’t know the type, either determine the type and catch it or don’t implement a handler at all!   If you need to throw an exception and there’s not an exception type available that is specific to the error that occurred, create a new one.</p>
<p><strong><em> </em></strong></p>
<p><strong> </strong></p>
<h1>Practice #3: Always Null-Check Objects Returned from a Function Call</h1>
<p>Review the 5 lines of code below.</p>
<div class="wlWriterEditableSmartContent" style="margin: 0px;float: none;padding: 0px">
<div style="border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt">
<div style="background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px">Code Snippet</div>
<div style="background: #ddd;overflow: auto">
<ol style="background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px">
<li><span style="color: #0000ff">var</span> result = SomeFunctionThatReturnsAnObject();</li>
<li></li>
<li> <span style="color: #0000ff">if</span> (!<span style="color: #2b91af">String</span>.IsNullOrEmpty(result.SomeValue))</li>
<li> {</li>
<li> <span style="color: #008000">//Do Something</span></li>
<li> }</li>
</ol>
</div>
</div>
</div>
<p>What’s wrong with this picture?</p>
<p>If you said <strong><em>Null Reference Exception</em></strong> you’re right.  <strong><em>Never</em></strong>,<strong><em> ever</em></strong> access any member of any object that has been returned from a function without checking for null.  You should <strong><em>never</em></strong> assume that a function will return an instantiated object.</p>
<p><strong><em>Never. </em></strong></p>
<p><em> </em></p>
<p><strong><em>Even if you wrote the function you’re calling.</em> </strong></p>
<p>We all work on projects with multiple developers, and at any given time another developer (or even you) may alter the code and end up returning a null object without realizing that there is some expectation that an instantiated object be returned by some callers.</p>
<p>Taking the extra time to type 15-20 extra characters can mean the difference between stable software and buggy, error-prone code.</p>
<div class="wlWriterEditableSmartContent" style="margin: 0px;float: none;padding: 0px">
<div style="border: #000080 1px solid;color: #000;font-family: 'Courier New', Courier, Monospace;font-size: 10pt">
<div style="background: #000080;color: #fff;font-family: Verdana, Tahoma, Arial, sans-serif;font-weight: bold;padding: 2px 5px">Code Snippet</div>
<div style="background: #ddd;overflow: auto">
<ol style="background: #ffffff;margin: 0 0 0 2em;padding: 0 0 0 5px">
<li><span style="color: #0000ff">var</span> result = SomeFunctionThatReturnsAnObject();</li>
<li></li>
<li> <span style="color: #0000ff">if</span> (result != <span style="color: #0000ff">null</span> &amp;&amp; !<span style="color: #2b91af">String</span>.IsNullOrEmpty(result.SomeValue))</li>
<li> {</li>
<li> <span style="color: #008000">//Do Something</span></li>
<li> }</li>
</ol>
</div>
</div>
</div>
<h2>Conclusion</h2>
<p>I wrote this as an e-mail to my team after doing some refactoring and realized that it could be beneficial to a wider audience being that violations of these practices are so common.  I’ve made these mistakes (as I’m sure many of us have) numerous times in the past, so writing this up was a good reminder for me to focus on these points when writing code.</p>
<p>Please feel free to post additions, corrections, questions, and concerns in the comments section below!  The dialogue will benefit all involved!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tallan.com/2011/06/29/three-net-best-practices-to-keep-in-mind/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MIX11 in review</title>
		<link>http://blog.tallan.com/2011/05/06/mix11-in-review/</link>
		<comments>http://blog.tallan.com/2011/05/06/mix11-in-review/#comments</comments>
		<pubDate>Fri, 06 May 2011 05:03:34 +0000</pubDate>
		<dc:creator>Ben Dewey</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[NuGet]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://blog.tallan.com/?p=1146</guid>
		<description><![CDATA[I know this post is overdue, but I&#8217;m happy to say that in the time since MIX has elapsed, my wife and I have sold our condo and moved to a new home.  I&#8217;ve also transition to a new contract, and I am excited to get my feet wet my new project.  Despite [...]]]></description>
			<content:encoded><![CDATA[<p>I know this post is overdue, but I&#8217;m happy to say that in the time since <a href="http://live.visitmix.com">MIX</a> has elapsed, my wife and I have sold our condo and moved to a new home.  I&#8217;ve also transition to a new contract, and I am excited to get my feet wet my new project.  Despite the delay, it was a wonderful experience.  This was my first year at MIX.  The content, announcements, and the networking were all memorable.  I only wish I could&#8217;ve attended all the sessions.  Luckily the content is <a href="http://live.visitmix.com">available online</a>, so I did my best to mark key talks that I wanted to catch back home.</p>
<p>This year was filled with a number of announcements at the two keynotes I did my best to update my <a href="http://twitter.com/bendewey">twitter feed</a> with some of the cool ones.</p>
<p>The <strong>first keynote</strong> was focused around recent release of <strong>IE9</strong>, the next generation of web, <strong>ASP.NET MVC3</strong>, and <strong>HTML5</strong>, and the new release of <strong>IE 10 Platform Preview 1</strong> (<a href="http://channel9.msdn.com/events/mix/mix11/key01">watch online</a>):</p>
<ul>
<li>IE9 Released</li>
<li>IE10 Platform Preview1 Released (<a href="http://ie.microsoft.com/testdrive/">download</a>)</li>
<li>HTML5 Labs <a href="http://html5labs.interoperabilitybridges.com/">available</a> to demonstrate HTML5 features and compatibility</li>
<li>New Developer Conference coming to Anaheim, CA from September 13-16 2011 (PDC maybe? maybe not?)</li>
<li>Microsoft is commited to pushing and actively contributing to <a href="http://nuget.org">NuGet</a> (an open source package manager)</li>
<li>Tools update for ASP.NET MVC 3 available for <a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=82cbd599-d29a-43e3-b78b-0f863d22811a">download</a></li>
<li>Entity Framework 4.1 released (<a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=b41c728e-9b4f-4331-a1a8-537d16c6acdf&amp;displaylang=en">download</a>)</li>
<li>Updated Scaffolding support for Entity Framework and MVC</li>
<li>Updated ASP.NET MVC New Project Dialog (<a href="http://www.bendewey.com/blog/?attachment_id=348">screenshot</a>) with support for the Razor View engine, HTML5 markup</li>
<li>MVC 3 new projects now include built in <a href="http://www.modernizr.com/">Modernizr</a> script support and many scripts and references are updatable via <a href="http://nuget.org">NuGet</a> package references.</li>
</ul>
<p>The <strong>second keynote</strong>, which really piqued my interests was all about <strong>Windows Phone 7, Silverlight</strong>, and the <strong>Kinect </strong>(<a href="http://channel9.msdn.com/events/mix/mix11/key02">watch</a>):</p>
<ul>
<li>Windows Phone 7
<ul>
<li>Respectable acknowledgement of the Windows Phone 7 Update state by Joe Belfiore, and announcement of the <a href="http://www.microsoft.com/windowsphone/en-us/features/update-schedule-usa.aspx">Where&#8217;s my Update?</a> page.</li>
<li>New Windows Phone 7 Update codenamed &#8220;Mango&#8221; shipping this Fall</li>
<li>Focus of Mango is on Opportunity (Ecosystem, Countries, Discoverability), Capability (Browser, Phone Integration, Multi-tasking), and Developer Experience and Tools</li>
<li>Opportunity
<ul>
<li>Nokia appeared live and affirmed their commitment to partnering with Microsoft to reach hundreds of millions of customers</li>
<li>Mango will support for 16 Languages, 38 Countries, and 35 Countries can buy apps from a single Marketplace</li>
<li>Marketplace search added to App list on home screen</li>
<li>New Pivot view added to App decriptions in Marketplace</li>
<li>New Related Apps pivot on in Marketplace</li>
<li>Hubs and Search Cards (Movies, Restaurants, etc) have an Extras tab that deep links to relevant content</li>
</ul>
</li>
<li>Capability
<ul>
<li>IE9 and HTML5 will be included in the Mango</li>
<li>HTML5 audio/video tag support added</li>
<li>Background Audio support added to Mango</li>
<li>Background Agents available in Mango, with support for running when users battery/wifi are optimal</li>
<li>Multiple Live Tiles for Apps</li>
<li>Live Tile Animation</li>
<li>Updates to Live Tiles without Push notifications</li>
<li>Sensor support added for Direct Camera support, Compass, and Gyro</li>
<li>Socket Communication added</li>
<li>Performance Optimizations &#8211; Scrolling and Input, Image Decode, Garbage Collection, and Memory Usage</li>
<li>New Motion Sensor API for combining the Compass and Gyro raw data</li>
<li>Angry Birds (May 25), Spotify, and Skype are coming to the Windows Phone</li>
</ul>
</li>
<li>Developer Tools
<ul>
<li>Phone orientation tool for simulating Accelerometer, with options for pre-recorded gestures and actions</li>
<li>Location simulation available via built-in Bing Maps tool, with options for pre-recorded location changes</li>
<li>Phone Performance monitoring and analysis tools available</li>
</ul>
</li>
</ul>
</li>
<li>Silverlight
<ul>
<li>Sliverlight 5 Beta is now available (<a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=41c85cc4-de12-4bdb-a60f-f120266e9780&amp;displaylang=en">download</a>)</li>
<li>Expression Blend Preview for Silverlight 5 is now available (<a href="http://www.microsoft.com/downloads/en/details.aspx?FamilyID=22feb67c-4f65-4ced-97cf-5f8ada296445&amp;displaylang=en">download</a>)</li>
<li>Hardware-Based Video Decode</li>
<li>Added 3D support</li>
<li>Trick Play support &#8211; pitch correction audio</li>
<li>Recieve Commands from a remote control</li>
</ul>
</li>
<li>Kinect
<ul>
<li>Kinect SDK is <a href="http://research.microsoft.com/en-us/um/redmond/projects/kinectsdk/">coming this Fall</a></li>
</ul>
</li>
</ul>
<p>In addition to some wonderful keynotes, I wrote a personal summary of the sessions I attended (all of this content is available <a href="http://live.visitmix.com">online</a>):</p>
<ul>
<li><a href="http://channel9.msdn.com/events/MIX/MIX11/FRM06">Deconstructing Orchard: Build, Customize, Extend, Ship</a>
<p>Bradley Millington explained the new features of the v1.1 release of Orchard CMS.  This release comes with a new Recipes concept to startup development.  Think Visual Studio Project Templates but for Orchard sites, these are fully customizable and open to community contributions, so new templates may come out for many common scenarios.  Bradley also showed how simple it is to extend modules and themes.  Finally we saw how a simplified deployment can round out a solution with minimal barriers to entry.</p>
</li>
<li><a href="http://channel9.msdn.com/events/MIX/MIX11/EXT02">Fonts, Form and Function: A Primer on Digital Typography</a>
<p>Everytime I see Robby Ingebretsen I&#8217;m always so impress about how simple he makes design feel.  Check out his presentation at the <a href="http://www.microsoft.com/design/toolbox/school/">Design Toolbox</a> if you haven&#8217;t yet.  Robby took us on a journey through the font choices for his group&#8217;s new website <a href="http://thinkpixellab.com/">http://thinkpixellab.com/</a>.  He had some musings about fonts having personalities and relating that to a Hollywood cast in a major screenplay.  Of course, we were shown with the every important Grid concept.</p>
</li>
<li><a href="http://channel9.msdn.com/events/MIX/MIX11/UXL01">Inspiring UX &#8211; UX Lightning Series</a>
<p>&#8216;UX Lightning sessions are a full hour session with 4 exceptional speakers each presenting 10 minute topics&#8217;. There was lots of inspiring content about Natural User interfaces, putting a lot of ownership on us as Developers and Designers.   August de los Reyes was truly remarkable in his 21st Century Design Manifesto where he promotes a design process which is focused on Motivation, Needs, Positive Emotion, Learnability, Adaptability, and Revolutionary changes. This is in contrast to a user centric design which puts faith in the users (who often don&#8217;t know what, why, and how they like something), and incremental design evolution.</p>
</li>
<li><a href="http://channel9.msdn.com/events/MIX/MIX11/EXT13">ECMAScript 5: The New Parts</a>
<p>We had the pleasure of listening to the godfather, Doug Crockford, show off the new ECMAScript 5 changes.  Many of the features have been wanted for some time.  Luckily many of them have shims for older browsers.  The addition of strict mode allows the type safety to bring javascript to the next level of software craftsmanship.</p>
</li>
<li><a href="http://channel9.msdn.com/events/MIX/MIX11/DVC18">Multitasking in the Next Version of Windows Phone, Part I</a>
<p>Announced after the second keynote, Darin Miller, reviews the new Background/Live Agents, Reminders, and other background and multi-tasking APIs.  Mango has some great new features, and they are being designed in a way that respects the user, all while retaining their excellent developer experience.  Like optimizing downloads when you are on WiFi and power.</p>
</li>
<li><a href="http://channel9.msdn.com/events/MIX/MIX11/FRM05">Building Data-centric N-tier Applications with jQuery</a>
<p>The WCF RIA Services team has been doing a great job rounding out the REST/jQuery story.  Brad Olenick showed a WCF Service that was consumed by jQuery using plugins for templating, data linking, change tracking, sorting and paging on the server side and on the client side.  I can see this being a great tool coupled with the coming jQuery UI Grid.  I hope the community rallies around this one especially with the <a href="http://blog.jquery.com/2011/04/16/official-plugins-a-change-in-the-roadmap/">recent shift in structure</a> for the offical jQuery templating and dataLinking plugins</p>
</li>
<li><a href="http://channel9.msdn.com/events/MIX/MIX11/MED06">Graphics &amp; 3D with Silverlight 5</a>
<p>One of most exciting announcements of Silverlight 5 is 3D, I&#8217;m really intrigued by it.  As expected they brought in the XNA 3D features which is more aligned with the Windows Phone 3D experience.   Aaron Oneal wowed everyone with some really cool demos including the Virtual walkthrough of the Windows Cafe.  Then he walked us the through the Graphics processing pipeline showed how vertices, colors, triangles, textures, overlays, and lighting can be written straight to the <a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.graphicsdevice.aspx">GraphicsDevice</a>.  Caution, 3D development is not for yhe faint at heart.</p>
</li>
<li><a href="http://channel9.msdn.com/events/MIX/MIX11/FRM14">WCF Web APis: &#8220;There&#8217;s a URI for That&#8221;</a>
<p>Coupled with Brad Olenick work above Glenn Block wrapped up the other side of what&#8217;s included on the <a href="http://wcf.codeplex.com">http://wcf.codeplex.com</a> release.  In addition to consuming json using dynamic objects, they have a fluent API for configuring services and service creation (ie. IoCs). There are also new strongly typed generic request/response wrappers that expose header information.  For more details, see Sam&#8217;s <a href="http://samgentile.com/Web/wcf/the-continuing-evolution-of-wcf-ndash-the-web- uris-evolve/">post</a>.</p>
</li>
<li><a href="http://channel9.msdn.com/events/MIX/MIX11/OPN07">Fun with ASP.NET MVC 3 and MEF</a>
<p>The power of NuGet was very prevalent at Mix this year, and I&#8217;m very excited about the power of using MEF and NuGet combined.  Maarten Balliauw showed us how setting up your MVC application to accept MEF Imports, then by separating components into different projects you can add/remove them and in turn on/off functionality.</p>
</li>
<li><a href="http://channel9.msdn.com/events/MIX/MIX11/FRM09">NuGet In Depth: Empowering Open Source on the .NET Platform</a>
<p>Another great HaHaa show.  Scott and Phil performed a great ping-pong over creating and consuming NuGet packages.  They also showed off the new Symbols feature, &#8220;which is huge&#8221; as Scott would say.  Very entertaining session and inspiring for NuGet</p>
</li>
</ul>
<p>During the breaks, I joined up with some other East Coast developers and speakers and worked on a Coding for Fun Project involving the .NET Micro Framework called the <a href="http://www.curiouscloudcontest.com/">Curious Cloud Contest</a>, we had a nice time with the project and we won a Video Production award in the process.  You can see our team&#8217;s (MADExpo) video <a href="http://www.curiouscloudcontest.com/Team.aspx?tid=qyKNaxv397ylTm6rj841gQ%3d%3d">here</a>.</p>
<p>I also had two wonderful evening events, the first night I had the pleasure of joining the Microsoft NUI team for some good Mexican food and the usually HTML5 vs. Silverlight discussion.  The following night I had the privilege of being invited to join the folks from Telerik at Aureole, we were joined by Walt Ritscher and a number of people from the Windows Phone Team including Jamie Rodriguez and Ben Riga, both of whom did a wonderful job presenting at that morning&#8217;s Keynote event.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tallan.com/2011/05/06/mix11-in-review/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Intro to PLINQ</title>
		<link>http://blog.tallan.com/2010/07/01/plinq/</link>
		<comments>http://blog.tallan.com/2010/07/01/plinq/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 22:13:44 +0000</pubDate>
		<dc:creator>jraymunt</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Enterprise .NET]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Parallel]]></category>
		<category><![CDATA[PLINQ]]></category>

		<guid isPermaLink="false">http://blog.tallan.com/?p=559</guid>
		<description><![CDATA[I recently had the opportunity to attend the Microsoft launch event for the new Visual Studio 2010.  I was impressed with everything 2010 is bringing to the table, but what really caught my eye is the new Parallel FX library.  Parallel FX provides developers with a set of tools which promises faster and [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had the opportunity to attend the Microsoft launch event for the new Visual Studio 2010.  I was impressed with everything 2010 is bringing to the table, but what really caught my eye is the new Parallel FX library.  Parallel FX provides developers with a set of tools which promises faster and less error prone development of multi-threaded applications.  For this post I’m going to focus in on the new parallel LINQ, also known as PLINQ (which reminds me of Plinko every time I hear it).</p>
<p>For those of you who aren’t familiar with LINQ, I’ll try to sum it up in a few sentences.  LINQ stands for language-integrated query.  It provides .Net developers with a new syntax which makes performing set based operations on a collection, or multiple collections easier.  For example, if we have an unordered list of integers which we need to sort, in the past we would have to do something similar to this:</p>
<pre class="brush: csharp;">
public void RunSort()
{
  var integerList = new List&lt;int&gt; { 5, 63, 23, 1, 0, 91 };
  QuickSort(0, integerList.Count, integerList);
}

public void QuickSort(int left, int right, IList&lt;int&gt; integerList)
{
  var pivot = integerList[left];

  while (left &lt; right)
  {
    while ((integerList[right] &gt;= pivot) &amp;&amp; (left &lt; right))
    {
      right--;
    }

    if (left != right)
    {
      integerList[left] = integerList[right];
      left++;
    }

    while ((integerList[left] &lt;= pivot) &amp;&amp; (left &lt; right))
    {
      left++;
    }

    if (left == right) continue;
    integerList[right] = integerList[left];
    right--;
  }

  integerList[left] = pivot;
  pivot = left;

  if (left &lt; pivot)
  {
    QuickSort(left, pivot - 1, integerList);
  }

  if (right &gt; pivot)
  {
    QuickSort(pivot + 1, right, integerList);
  }
}
</pre>
<p>The above example works fine, however as a developer we needed to make the decision of what sorting algorithm to use, and then go ahead and implement the sorting function.  This wastes time and can sometimes be error prone.  LINQ can solve this problem, and many other set based problems like it quickly and efficiently.  Here is an example of the first example re-written to make use of LINQ:</p>
<pre class="brush: csharp;">
public void RunSort()
{
    var integerList = new List { 5, 63, 23, 1, 0, 91 };
    var sortedIntegerList = from integer in integerList
                            orderby integer ascending
                            select integer;
}
</pre>
<p>LINQ takes care of the sorting here for us and provides a nice, easy to read syntax.  When using LINQ, we can worry less about the details of working with data sets, and focus more about the business rules we are implementing.  PLINQ takes LINQ a step further and extends this powerful set based functionality to take advantage of multi-core CPU’s.  Here is an example of the second example converted to use PLINQ:</p>
<pre class="brush: csharp;">
public void RunSort()
{
    var integerList = new List { 5, 63, 23, 1, 0, 91 };
    var sortedIntegerList = from integer in integerList.AsParallel()
                            orderby integer ascending
                            select integer;
}
</pre>
<p>When comparing the above example with the second example, only a couple of things have changed.  One is obvious just by looking at the code.  We are now calling the extension method .AsParallel on the list of integers.  This tells PLINQ that it is OK to go ahead and try to parallelize the LINQ query.  The second is a little more subtle.  Instead of the LINQ query returning an IEnumerable type, it is returning an IParallelEnumerable.  Luckily, IParallelEnumerable derives from IEnumerable, so existing code should work fine with this change.</p>
<p>This is of course a very basic example of how LINQ and PLINQ differ.  But hopefully I’ve sparked some interest.</p>
<p>-Jon</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tallan.com/2010/07/01/plinq/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Content from LINQ Code Camp Presentation (6/13/09)</title>
		<link>http://blog.tallan.com/2009/06/18/content-from-linq-code-camp-presentation-61309/</link>
		<comments>http://blog.tallan.com/2009/06/18/content-from-linq-code-camp-presentation-61309/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 15:28:07 +0000</pubDate>
		<dc:creator>Michael Gerety</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Presentations]]></category>
		<category><![CDATA[Code Camp]]></category>
		<category><![CDATA[CTDOTNET]]></category>
		<category><![CDATA[Hartford]]></category>
		<category><![CDATA[User Groups]]></category>

		<guid isPermaLink="false">http://blogs.tallan.com/dotnetreflections/2009/06/18/content-from-linq-code-camp-presentation-61309/</guid>
		<description><![CDATA[On June 13, 2009 I was privileged to give a presentation at the Hartford Code Camp at New Foundations in Bloomfield, CT.   The event was a great success, with 29 presenters,  35 sessions, and more than 150 people in attendance.  I’d like to say thank you to anyone who was able to attend my session [...]]]></description>
			<content:encoded><![CDATA[<p>On June 13, 2009 I was privileged to give a presentation at the Hartford Code Camp at New Foundations in Bloomfield, CT.   The event was a great success, with 29 presenters,  35 sessions, and more than 150 people in attendance.  I’d like to say thank you to anyone who was able to attend my session on LINQ, my group was great and had a lot of insightful and pointed questions.</p>
<p>As promised, I’m posting the content from the presentation including the PowerPoint files and Demo Code.</p>
<p>If anyone has any questions about the content, please feel free to e-mail me at <a href="mailto:mgerety@tallan.com">mgerety@tallan.com</a></p>
<p><strong>Presentation and Content Files: <a title="LINQ Presentation and Files" href="http://www.gerety.net/LINQCodeCamp/linq.zip" target="_blank">LINQ Presentation And Files</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tallan.com/2009/06/18/content-from-linq-code-camp-presentation-61309/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating a Project Installer for a Windows Service</title>
		<link>http://blog.tallan.com/2009/06/01/creating-a-project-installer-for-a-windows-service/</link>
		<comments>http://blog.tallan.com/2009/06/01/creating-a-project-installer-for-a-windows-service/#comments</comments>
		<pubDate>Mon, 01 Jun 2009 18:00:10 +0000</pubDate>
		<dc:creator>jraymunt</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Windows Services]]></category>

		<guid isPermaLink="false">http://blogs.tallan.com/dotnetreflections/?p=148</guid>
		<description><![CDATA[I recently had to create a Windows Service for a project I&#8217;m currently working on.  Rather than installing/uninstalling using InstallUtil, I decided to create a project installer to do the work for me.  It took a little bit of digging to gather all of the necessary steps, so here is what I did to get [...]]]></description>
			<content:encoded><![CDATA[<p>I recently had to create a Windows Service for a project I&#8217;m currently working on.  Rather than installing/uninstalling using InstallUtil, I decided to create a project installer to do the work for me.  It took a little bit of digging to gather all of the necessary steps, so here is what I did to get up and running.</p>
<ol>
<li>Create your Windows Service project and Windows Service.</li>
<li>Navigate to the designer for your Windows Service. Under the properties for your service you should see (Name) and ServiceName. Fill in both of these properties with your desired names. You will need to reference these later.</li>
<li>In the designer for your Windows Service, right-click and click Add Installer. You should now have a ProjectInstaller.cs file, along with a designer file for the installer.</li>
<li>Navigate to the designer for your new project installer. You should see two components, a Service Process Installer and a Service Installer. Under the Service Installer properties, you will need to add a DisplayName, and provide the ServiceName of the service you will be installing. The ServiceName must match the (Name) property of the service.</li>
<li>Build your Windows Service project. Now in the same solution, add a new Setup Project. This will provide the Wizard interface for the installation process.</li>
<li>After adding the project, you need to add the primary output of your Windows Service to the Setup Project. Do this by right-clicking on the Setup Project -&gt; Add -&gt; Project Output. Select your Windows Service project from the drop down, highlight Primary Output and click OK.</li>
<li>Up to this point the installer will install the correct files, but it doesn&#8217;t know what to do with the files. We can add custom actions to do the service installation work. Right-click on the Setup Project -&gt; View -&gt; Custom Actions. For a Windows Service we need to add actions for Install, Rollback, and Uninstall. Right-click each of these and click Add Action. Double click Application Folder and select the Primary Output from your Windows Service project.</li>
<li>You can now build your Setup Project. The output of this build will be two setup files which are used to install your new Windows Service.</li>
</ol>
<p>-Jon</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tallan.com/2009/06/01/creating-a-project-installer-for-a-windows-service/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WCF Security Part 1</title>
		<link>http://blog.tallan.com/2009/03/11/wcf-security-part-1/</link>
		<comments>http://blog.tallan.com/2009/03/11/wcf-security-part-1/#comments</comments>
		<pubDate>Thu, 12 Mar 2009 01:48:47 +0000</pubDate>
		<dc:creator>jraymunt</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://blogs.tallan.com/dotnetreflections/?p=115</guid>
		<description><![CDATA[A few months ago I was given the task of setting up security for some WCF services we had created. After a couple days of scouring the internet for ideas and solutions, I ended up purchasing a book on WCF. Although there is a lot of information on the web about WCF and what security [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal">A few months ago I was given the task of setting up security for some WCF services we had created.<span> </span>After a couple days of scouring the internet for ideas and solutions, I ended up purchasing a book on WCF.<span> </span>Although there is a lot of information on the web about WCF and what security methods are available to us as developers, the information was always incomplete, and connecting the dots wasn&#8217;t the easiest.<span> </span>The example below shows how we can setup message based WCF security for a service which will be accessed over the internet, although the same code could be used for an intranet application as well.<span> </span>The example will require both user authentication and an x509 certificate.</p>
<p class="MsoNormal">For our WCF service, we&#8217;re using the wsHttpBinding.<span> </span>This binding provides much of the same functionality as a basic web service, with added service features such as reliable messaging, WS-Addressing, and WS-Security.<span> </span>Since this post is mainly on setting up security for a WCF service, I won&#8217;t go into too many details of the other features of wsHttpBinding.</p>
<p class="MsoNormal">After creating the WCF service, open up the web.config file for the WCF Service Application project.<span> </span>After scrolling down for a long time, you should come across a System.serviceModel tag.<span> </span>This tag contains all of the configuration settings for the WCF services contained in this project.<span> </span>Here&#8217;s what you should be looking at now:</p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span>&lt;</span><span>system.serviceModel</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>services</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>service</span><span> </span><span>name</span><span>=</span><span>&#8220;<span style="color: blue">WcfService1.Service1</span>&#8220;<span style="color: blue"> </span><span style="color: red">behaviorConfiguration</span><span style="color: blue">=</span>&#8220;<span style="color: blue">WcfService1.Service1Behavior</span>&#8220;<span style="color: blue">&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;!&#8211;</span><span> Service Endpoints </span><span>&#8211;&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>endpoint</span><span> </span><span>address</span><span>=</span><span>&#8220;&#8221;<span style="color: blue"> </span><span style="color: red">binding</span><span style="color: blue">=</span>&#8220;<span style="color: blue">wsHttpBinding</span>&#8220;<span style="color: blue"> </span><span style="color: red">contract</span><span style="color: blue">=</span>&#8220;<span style="color: blue">WcfService1.IService1</span>&#8220;<span style="color: blue">&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;!&#8211;</span><span> </span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>Upon deployment, the following identity element should be removed or replaced to reflect the </span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>identity under which the deployed service runs.<span> </span>If removed, WCF will infer an appropriate identity </span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>automatically.</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span></span><span>&#8211;&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>identity</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>dns</span><span> </span><span>value</span><span>=</span><span>&#8220;<span style="color: blue">localhost</span>&#8220;<span style="color: blue">/&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;/</span><span>identity</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;/</span><span>endpoint</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>endpoint</span><span> </span><span>address</span><span>=</span><span>&#8220;<span style="color: blue">mex</span>&#8220;<span style="color: blue"> </span><span style="color: red">binding</span><span style="color: blue">=</span>&#8220;<span style="color: blue">mexHttpBinding</span>&#8220;<span style="color: blue"> </span><span style="color: red">contract</span><span style="color: blue">=</span>&#8220;<span style="color: blue">IMetadataExchange</span>&#8220;<span style="color: blue">/&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;/</span><span>service</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;/</span><span>services</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>behaviors</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>serviceBehaviors</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>behavior</span><span> </span><span>name</span><span>=</span><span>&#8220;<span style="color: blue">WcfService1.Service1Behavior</span>&#8220;<span style="color: blue">&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;!&#8211;</span><span> To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment </span><span>&#8211;&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>serviceMetadata</span><span> </span><span>httpGetEnabled</span><span>=</span><span>&#8220;<span style="color: blue">true</span>&#8220;<span style="color: blue">/&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;!&#8211;</span><span> To receive exception details in faults for debugging purposes, set the value below to true.<span> </span>Set to false before deployment to avoid disclosing exception information </span><span>&#8211;&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>serviceDebug</span><span> </span><span>includeExceptionDetailInFaults</span><span>=</span><span>&#8220;<span style="color: blue">false</span>&#8220;<span style="color: blue">/&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;/</span><span>behavior</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;/</span><span>serviceBehaviors</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;/</span><span>behaviors</span><span>&gt;</span></p>
<p class="MsoNormal"><span><span> </span>&lt;/</span><span>system.serviceModel</span><span>&gt;</span></p>
<p class="MsoNormal"><span>By default, a WCF service will use the wsHttpBinding, this is what we want to continue using.<span> </span>We need to make a few modifications to this web.config in order to provide the additional functionality we want.<span> </span>First take a look at the serviceBehaviors section.<span> </span>By default we already have a behavior for our Service1.<span> </span>Here is an example of a modified behavior:</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span>&lt;</span><span>behavior</span><span> </span><span>name</span><span>=</span><span>&#8220;<span style="color: blue">WcfService1.Service1Behavior</span>&#8220;<span style="color: blue">&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>serviceCredentials</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>serviceCertificate</span><span> </span><span>findValue</span><span>=</span><span>&#8220;<span style="color: blue">CN=ServiceCertificate</span>&#8220;<span style="color: blue"> </span><span style="color: red">storeLocation</span><span style="color: blue">=</span>&#8220;<span style="color: blue">LocalMachine</span>&#8220;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span></span><span>storeName</span><span>=</span><span>&#8220;<span style="color: blue">My</span>&#8220;<span style="color: blue"> </span><span style="color: red">x509FindType</span><span style="color: blue">=</span>&#8220;<span style="color: blue">FindBySubjectDistinguishedName</span>&#8220;<span style="color: blue"> /&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>userNameAuthentication</span><span> </span><span>userNamePasswordValidationMode</span><span>=</span><span>&#8220;<span style="color: blue">Windows</span>&#8220;<span style="color: blue">/&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;/</span><span>serviceCredentials</span><span>&gt;</span></p>
<p class="MsoNormal"><span><span> </span>&lt;/</span><span>behavior</span><span>&gt;</span></p>
<p class="MsoNormal"><span>There is a lot of info in this behavior pertaining to security.<span> </span>The first bit describes the x509 certificate we would like to use that is already installed on our machine.<span> </span>We can create our own test x509 certificate for development purposes if need be.<span> </span>We can also describe the user authentication mode, in this case we are just using Windows authentication.</span></p>
<p class="MsoNormal"><span>The next section extends the wsHttpBinding to provide some additional features not turned on be default:</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span>&lt;</span><span>bindings</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>wsHttpBinding</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>binding</span><span> </span><span>name</span><span>=</span><span>&#8220;<span style="color: blue">wsHttpBindingSettings</span>&#8220;<span style="color: blue"> </span><span style="color: red">messageEncoding</span><span style="color: blue">=</span>&#8220;<span style="color: blue">Text</span>&#8220;<span style="color: blue">&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>security</span><span> </span><span>mode</span><span>=</span><span>&#8220;<span style="color: blue">Message</span>&#8220;<span style="color: blue">&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>message</span><span> </span><span>clientCredentialType</span><span>=</span><span>&#8220;<span style="color: blue">UserName</span>&#8220;<span style="color: blue"> /&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;/</span><span>security</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;/</span><span>binding</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;/</span><span>wsHttpBinding</span><span>&gt;</span></p>
<p class="MsoNormal"><span><span> </span>&lt;/</span><span>bindings</span><span>&gt;</span></p>
<p class="MsoNormal"><span>Here we can describe the transport type, as well as the client credential type.<span> </span>This is fairly simple, we are using Message based transport with UserName security.</span></p>
<p class="MsoNormal"><span>Finally we need to tie these sections together with Service1:</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span>&lt;</span><span>service</span><span> </span><span>behaviorConfiguration</span><span>=</span><span>&#8220;<span style="color: blue">WcfService1.Service1Behavior</span>&#8220;<span style="color: blue"> </span><span style="color: red">name</span><span style="color: blue">=</span>&#8220;<span style="color: blue">WcfService1.Service1Behavior</span>&#8220;<span style="color: blue">&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>endpoint</span><span> </span><span>address</span><span>=</span><span>&#8220;&#8221;<span style="color: blue"> </span><span style="color: red">binding</span><span style="color: blue">=</span>&#8220;<span style="color: blue">wsHttpBinding</span>&#8220;<span style="color: blue"> </span><span style="color: red">bindingConfiguration</span><span style="color: blue">=</span>&#8220;<span style="color: blue">wsHttpBindingSettings</span>&#8220;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span></span><span>contract</span><span>=</span><span>&#8220;<span style="color: blue">WcfService1.IService1</span>&#8220;<span style="color: blue"> </span><span style="color: red">listenUriMode</span><span style="color: blue">=</span>&#8220;<span style="color: blue">Explicit</span>&#8220;<span style="color: blue">&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>identity</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>dns</span><span> </span><span>value</span><span>=</span><span>&#8220;<span style="color: blue">ServiceCertficate</span>&#8220;<span style="color: blue"> /&gt;</span></span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;/</span><span>identity</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;/</span><span>endpoint</span><span>&gt;</span></p>
<p class="MsoNormal" style="margin-bottom: 0.0001pt;line-height: normal"><span><span> </span>&lt;</span><span>endpoint</span><span> </span><span>address</span><span>=</span><span>&#8220;<span style="color: blue">mex</span>&#8220;<span style="color: blue"> </span><span style="color: red">binding</span><span style="color: blue">=</span>&#8220;<span style="color: blue">mexHttpBinding</span>&#8220;<span style="color: blue"> </span><span style="color: red">contract</span><span style="color: blue">=</span>&#8220;<span style="color: blue">IMetadataExchange</span>&#8220;<span style="color: blue"> /&gt;</span></span></p>
<p class="MsoNormal"><span><span> </span>&lt;/</span><span>service</span><span>&gt;</span></p>
<p class="MsoNormal"><span>The only changes we made here are in the bindingConfiguration property and the dns value.<span> </span>The bindingConfiguration allows you to bind our binding configuration to this service, and the dns value should match the x509 certificate name.</span></p>
<p class="MsoNormal"><span>I&#8217;ll be posting some more on this topic, including writing your own custom username validation code for WCF, and creating your own test x509 certificate.<span> </span>Hope this helps!</span></p>
<p class="MsoNormal"><span>-Jon</span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.tallan.com/2009/03/11/wcf-security-part-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

