<?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/"
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Dave Jansen's Blog &#187; Code Analysis</title>
	<atom:link href="http://dansen.wordpress.com/category/code-analysis/feed/" rel="self" type="application/rss+xml" />
	<link>http://dansen.wordpress.com</link>
	<description>Thoughts... Words... Stories... Experiences...</description>
	<lastBuildDate>Wed, 25 Jun 2008 23:32:53 +0000</lastBuildDate>
	<generator>http://wordpress.com/</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<cloud domain='dansen.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://www.gravatar.com/blavatar/e960aec7a4a664225f78efe5b222056e?s=96&#038;d=http://s.wordpress.com/i/buttonw-com.png</url>
		<title>Dave Jansen's Blog &#187; Code Analysis</title>
		<link>http://dansen.wordpress.com</link>
	</image>
			<item>
		<title>Suppressing Code Analysis Rules</title>
		<link>http://dansen.wordpress.com/2008/04/15/suppressing-code-analysis-rules/</link>
		<comments>http://dansen.wordpress.com/2008/04/15/suppressing-code-analysis-rules/#comments</comments>
		<pubDate>Tue, 15 Apr 2008 15:13:45 +0000</pubDate>
		<dc:creator>dansen</dc:creator>
				<category><![CDATA[Code Analysis]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://dansen.wordpress.com/2008/04/15/suppressing-code-analysis-rules/</guid>
		<description><![CDATA[I&#8217;ve been using Code Analysis (FxCop) for a few months now and every now and then I come across a CA rule that I my solution violates. In 99% of situations, I comply to all the CA rules. In a previous post, I could not satisfy the CA1304 rule. Spelling checks is also another area [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=41&subd=dansen&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>I&#8217;ve been using Code Analysis (FxCop) for a few months now and every now and then I come across a CA rule that I my solution violates. In 99% of situations, I comply to all the CA rules. In a previous <a href="http://dansen.wordpress.com/2008/04/09/code-analysis-ca1304-must-call-faultreasonfaultreasonstring-cultureinfo/">post</a>, I could not satisfy the CA1304 rule. Spelling checks is also another area where CA rules fail all the time.</p>
<p>The process that I use for checking in code to TFS ensures that Code Analysis is run before hand and that the project setting &#8220;Treat Warnings as Errors&#8221; is set to true. This means that the CA rules that I want to ignore fail my project build.</p>
<p>There are several ways to get around this:</p>
<p>1. Don&#8217;t use CA &#8211; <em>not an option for me.<br />
</em>2. Don&#8217;t treat warnings as errors &#8211; <em>not an option for me.<br />
</em>3. Apply a Suppression &#8220;In Source&#8221;- <em>an option.<br />
</em>4. Apply a Suppression in &#8220;Project Suppression File (GlobalSuppression.cs) &#8211; <em>an ideal option</em>.</p>
<p>The last two options are my choice. Suppressing a CA rule places an attribute in code that informs FxCop to ignore the rule and therefore not raise a Warning or an Error.</p>
<h3>Example Rule Failures</h3>
<p>The following CA rules came up as Warnings for my solution. Both Suppress options are available. CA1304 gives two suppression options where as CA2210 (strong name key required) only allows a Project suppression because it is applied only at the assembly level. For a full list of the CA rules shipped with each version of VS/FxCop see this <a href="http://blogs.msdn.com/fxcop/archive/2008/01/07/faq-which-rules-shipped-in-which-version.aspx">post</a>.</p>
<p><a href="http://dansen.files.wordpress.com/2008/04/image4.png"><img style="border-width:0;" src="http://dansen.files.wordpress.com/2008/04/image-thumb4.png?w=523&#038;h=399" border="0" alt="image" width="523" height="399" /></a></p>
<h3>In Source Suppression</h3>
<p>When available, a CA rule can suppressed in the source code where the rule failed. Use the <strong>SuppressMessageAttribute </strong>to disable the CA rule.</p>
<pre>[<span style="color:#2b91af;">SuppressMessage</span>(<span style="color:#a31515;">"Microsoft.Globalization"</span>, <span style="color:#a31515;">"CA1304:SpecifyCultureInfo"</span>,
MessageId = <span style="color:#a31515;">"System.ServiceModel.FaultReason.#ctor(System.String)"</span>)]
<span style="color:blue;">public void </span>ProvideFault(<span style="color:#2b91af;">Exception </span>error,
<span style="color:#2b91af;">MessageVersion </span>version,
<span style="color:blue;">ref </span><span style="color:#2b91af;">Message </span>fault)
{
<span style="color:green;">// Return a 'ServiceFault' Fault Contract
</span><span style="color:#2b91af;">ServiceFault </span>faultDetail = <span style="color:blue;">new </span><span style="color:#2b91af;">ServiceFault</span>(<span style="color:#a31515;">"An error occured"</span>);

<span style="color:green;">// Construct FaultException with Fault Contract and FaultReason
</span><span style="color:#2b91af;">FaultException</span>&lt;<span style="color:#2b91af;">ServiceFault</span>&gt; faultException =
<span style="color:blue;">new </span><span style="color:#2b91af;">FaultException</span>&lt;<span style="color:#2b91af;">ServiceFault</span>&gt;(faultDetail,
<span style="color:blue;">new </span><span style="color:#2b91af;">FaultReason</span>(<span style="color:#a31515;">"FaultReasonText"</span>));
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The benefit of applying the rule override in the source, is that code reviewers can easily see where the attribute has been placed. An additional file is is not required. The disadvantages is that you must apply the the attribute for every rule failure even if they are the same failures across multiple operations in the same code file and multiple code files. It also dirties the code with an messy attributes.</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<h3>Project Suppressions</h3>
<p>When selecting the Project Supressions File option, the <strong>SuppressMessageAttribute</strong> is placed in the projects GlobalSuppressions.cs file &#8211; Visual Studio will create one automatically. The attribute has overrides which allows you to target the exact place of the rule failure such as a class, method, field, namespace etc.</p>
<p>In my example, I have suppressed botth CA2210 and CA1304 rules in the GlobalSuppressions.cs. Visual Studio generates the code.</p>
<pre><span style="color:green;">// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.
//
// To add a suppression to this file, right-click the message in the
// Error List, point to "Suppress Message(s)", and click
// "In Project Suppression File".
// You do not need to add suppressions to this file manually.

</span><span style="color:blue;">using </span>System.Diagnostics.CodeAnalysis;

[<span style="color:blue;">assembly</span>: <span style="color:#2b91af;">SuppressMessage</span>(
<span style="color:#a31515;">"Microsoft.Design"</span>,
<span style="color:#a31515;">"CA2210:AssembliesShouldHaveValidStrongNames"</span>)]

[<span style="color:blue;">assembly</span>:
<span style="color:#2b91af;">SuppressMessage</span>(
<span style="color:#a31515;">"Microsoft.Globalization"</span>, <span style="color:#a31515;">"CA1304:SpecifyCultureInfo"</span>,
MessageId = <span style="color:#a31515;">"System.ServiceModel.FaultReason.#ctor(System.String)"</span>,
Scope = <span style="color:#a31515;">"member"</span>,
Target = <span style="color:#a31515;">"Wcf.Demo.Service.ErrorHandler.#ProvideFault(System.Exception,System.ServiceModel.Channels.MessageVersion,System.ServiceModel.Channels.Message&amp;)"
</span>)]</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Notice the the CA1304 suppression contains Scope and Target properties. This can be changed so that the suppression is scoped to a wider code base such a a namespace.</p>
<p>The benefits of project suppressions is cleaner code, less duplication of the SuppressMessage attribute and easier to remove if failure is fixed. It&#8217;s also easier to code review as all failure overrides are in the one place. The only disadvantage that I can see is that there is an additional code file.</p>
<p>My preference is to always use project suppressions. In the near future , I will discuss some of the processes of how I use Code Analysis.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dansen.wordpress.com/41/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dansen.wordpress.com/41/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dansen.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dansen.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dansen.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dansen.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dansen.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dansen.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dansen.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dansen.wordpress.com/41/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dansen.wordpress.com/41/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dansen.wordpress.com/41/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=41&subd=dansen&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dansen.wordpress.com/2008/04/15/suppressing-code-analysis-rules/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a2613ce2237e8235db55cafe47bd9ce1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Dave</media:title>
		</media:content>

		<media:content url="http://dansen.files.wordpress.com/2008/04/image-thumb4.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>
	</item>
		<item>
		<title>CA1304 &#8211; Must specify CultureInfo when using FaultReason</title>
		<link>http://dansen.wordpress.com/2008/04/09/code-analysis-ca1304-must-specify-cultureinfo-when-using-faultreason/</link>
		<comments>http://dansen.wordpress.com/2008/04/09/code-analysis-ca1304-must-specify-cultureinfo-when-using-faultreason/#comments</comments>
		<pubDate>Wed, 09 Apr 2008 12:36:31 +0000</pubDate>
		<dc:creator>dansen</dc:creator>
				<category><![CDATA[Code Analysis]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://dansen.wordpress.com/2008/04/09/code-analysis-ca1304-must-call-faultreasonfaultreasonstring-cultureinfo/</guid>
		<description><![CDATA[Today I came across the following issue when running Code Analysis over my WCF Error Shielding solution.

Because I have &#8220;Treat Warnings as Errors&#8221; enabled for all projects, Code Analysis fails.
Rory Primrose has recently blogged a very nice Error Handling solution which have implemented in a WCF service. As soon as I implemented the solution and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=24&subd=dansen&ref=&feed=1" />]]></description>
			<content:encoded><![CDATA[<div class='snap_preview'><br /><p>Today I came across the following issue when running Code Analysis over my WCF Error Shielding solution.</p>
<p><a href="http://dansen.files.wordpress.com/2008/04/caerror.jpg"><img style="border-width:0;" src="http://dansen.files.wordpress.com/2008/04/caerror-thumb.jpg?w=822&#038;h=100" border="0" alt="CAError" width="822" height="100" /></a></p>
<p>Because I have &#8220;Treat Warnings as Errors&#8221; enabled for all projects, Code Analysis fails.</p>
<p><a href="http://neovolve.com/archive/2008/04/07/implementing-ierrorhandler.aspx">Rory Primrose</a> has recently blogged a very nice <a href="http://neovolve.com/archive/2008/04/07/implementing-ierrorhandler.aspx">Error Handling</a> solution which have implemented in a WCF service. As soon as I implemented the solution and specified to return a custom Fault Contract, WCF returned an exception indicating that the &#8220;<a href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.faultreason.aspx">FaultReason</a>&#8221; was not provided. So I constructed a new <a href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.faultreason.aspx">FaultReason</a>, and passed it in to the FaultException(Fault, FaultReason).</p>
<blockquote><p>The <span class="selflink">FaultReason</span> class contains a set of <a id="ctl00_rs1_mainContentContainer_ctl22" href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.faultreasontext.aspx"><span style="color:#0066cc;">System.ServiceModel<span class="cs">.</span><span class="vb">.</span><span class="cpp">::</span><span class="nu">.</span>FaultReasonText</span></a> objects, each of which contains a description of the fault in a specific language.</p></blockquote>
<p><strong>Sample</strong></p>
<p><!-- {\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 void\cf0  ProvideFault(\cf4 Exception\cf0  error, \cf4 MessageVersion\cf0  version, \cf1 ref\cf0  \cf4 Message\cf0  fault)\par ??\{\par ??    \cf5 // Return a 'ServiceFault' Fault Contract\par ??\cf0     \cf4 ServiceFault\cf0  faultDetail = \cf1 new\cf0  \cf4 ServiceFault\cf0 (\cf6 "An error occured"\cf0 );\par ??\par ??    \cf5 // Construct FaultException with FaultContract and FaultReason\par ??\cf0     \cf4 FaultException\cf0 &amp;lt;\cf4 ServiceFault\cf0 &amp;gt; faultException = \par ??        \cf1 new\cf0  \cf4 FaultException\cf0 &amp;lt;\cf4 ServiceFault\cf0 &amp;gt;(faultDetail, \cf1 new\cf0  \cf4 FaultReason\cf0 (\cf6 "FaultReasonText"\cf0 ));\par ??\par ??    \cf4 MessageFault\cf0  messageFault = faultException.CreateMessageFault();\par ??\par ??    fault = \cf4 Message\cf0 .CreateMessage(version, messageFault, faultException.Action);\par ??\}} --></p>
<div style="background:white none repeat scroll 0;font-family:Courier New;font-size:8pt;color:black;">
<p style="margin:0;"><span style="color:blue;">public</span> <span style="color:blue;">void</span> ProvideFault(<span style="color:#2b91af;">Exception</span> error, <span style="color:#2b91af;">MessageVersion</span> version, <span style="color:blue;">ref</span> <span style="color:#2b91af;">Message</span> fault)</p>
<p style="margin:0;">{</p>
<p style="margin:0;"><span style="color:green;"> // Return a &#8216;ServiceFault&#8217; Fault Contract</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> ServiceFault</span> faultDetail = <span style="color:blue;">new</span> <span style="color:#2b91af;">ServiceFault</span>(<span style="color:#a31515;">&#8220;An error occured&#8221;</span>);</p>
<p style="margin:0;">
<p style="margin:0;"><span style="color:green;"><br />
// Construct FaultException with FaultContract and FaultReason</span></p>
<p style="margin:0;"><span style="color:#2b91af;"> FaultException</span>&lt;<span style="color:#2b91af;">ServiceFault</span>&gt; faultException =</p>
<p style="margin:0;"><span style="color:blue;"> new</span> <span style="color:#2b91af;">FaultException</span>&lt;<span style="color:#2b91af;">ServiceFault</span>&gt;(faultDetail, <span style="color:blue;">new</span> <span style="color:#2b91af;">FaultReason</span>(<span style="color:#a31515;">&#8220;FaultReasonText&#8221;</span>));</p>
<p style="margin:0;">
<p style="margin:0;"><span style="color:#2b91af;"><br />
MessageFault</span> messageFault = faultException.CreateMessageFault();</p>
<p style="margin:0;">
<p>fault = <span style="color:#2b91af;">Message</span>.CreateMessage(version, messageFault, faultException.Action);</p>
<p style="margin:0;">}</p>
</div>
<p><!--EndFragment--></p>
<p>I compiled the code in Debug which was successful. I then switched on Code Analysis and it raised CA1304 : Microsoft.Globalization. The Code Analysis rule made sense to ensure I was using CultureInfo for the reason text.</p>
<p>However, the FaultReason does not provide any <strong>&#8216;public</strong>&#8216; constructor overrides to accept the CultureInfo. It does however provide an internal FaultReason(string text, CultureInfo cultureInfo) constructor.</p>
<p><a href="http://dansen.files.wordpress.com/2008/04/faultreason.jpg"><img style="border-width:0;" src="http://dansen.files.wordpress.com/2008/04/faultreason-thumb.jpg?w=678&#038;h=307" border="0" alt="FaultReason" width="678" height="307" /></a></p>
<p>After the help of <a href="http://neovolve.com/default.aspx">Rory</a> who confirmed with <a href="http://blogs.msdn.com/fxcop/">David Kean</a> from the <a href="http://blogs.msdn.com/fxcop/">Code Analysis Team</a> that this was a bug, I submitted feedback to <span style="font-size:11pt;font-family:'Calibri','sans-serif';color:#1f497d;"><a href="http://connect.microsoft.com/VisualStudio"><span style="color:#0000ff;">Microsoft Connect</span></a>.</span></p>
<p>You can keep track of this bug here: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=337431&amp;wa=wsignin1.0</p>
<p>In the meantime, I have suppressed this CA rule in my GlobalSupressions.cs for the project.</p>
<img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/dansen.wordpress.com/24/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/dansen.wordpress.com/24/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dansen.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dansen.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/dansen.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/dansen.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/dansen.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/dansen.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/dansen.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/dansen.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/dansen.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/dansen.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dansen.wordpress.com&blog=1210400&post=24&subd=dansen&ref=&feed=1" /></div>]]></content:encoded>
			<wfw:commentRss>http://dansen.wordpress.com/2008/04/09/code-analysis-ca1304-must-specify-cultureinfo-when-using-faultreason/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/a2613ce2237e8235db55cafe47bd9ce1?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Dave</media:title>
		</media:content>

		<media:content url="http://dansen.files.wordpress.com/2008/04/caerror-thumb.jpg" medium="image">
			<media:title type="html">CAError</media:title>
		</media:content>

		<media:content url="http://dansen.files.wordpress.com/2008/04/faultreason-thumb.jpg" medium="image">
			<media:title type="html">FaultReason</media:title>
		</media:content>
	</item>
	</channel>
</rss>