So now that .NET Core and .NET Standard 2.0 have been released some of you may be migrating applications or even creating new ones with it. As you progress you are starting to worry about the quality of your code so what you want is to at least check your code against design and style guidelines don’t you?

This post will show you how easy is to make .NET Core, Code Analysis and StyleCop play along together.

Here you go:

1. Create a folder for your new project#


Open a command prompt an run:

1mkdir codeanalysis

2. Create the project#


1cd codeanalysis
2dotnet new console

3. Add the references for CodeAnalysis and StyleCop#


1dotnet add package Microsoft.CodeAnalysis.FxCopAnalyzers
2dotnet add package StyleCop.Analyzers
3dotnet restore

4. Add CodeAnalysisRuleSet property to the project#


Open the file codeanalysis.csproj and add the following property inside a Property Group:

1<CodeAnalysisRuleSet>ca.ruleset</CodeAnalysisRuleSet>

or add a new Property Group:

1<PropertyGroup>
2    <CodeAnalysisRuleSet>ca.ruleset</CodeAnalysisRuleSet>
3</PropertyGroup>

5. Create the ca.ruleset file#


Create a ca.ruleset file and replace it’s content with the following xml:

 1<?xml version="1.0" encoding="utf-8"?>
 2<RuleSet Name="Custom Rulset" Description="Custom Rulset" ToolsVersion="14.0">
 3    <Rules AnalyzerId="AsyncUsageAnalyzers" RuleNamespace="AsyncUsageAnalyzers">
 4        <Rule Id="UseConfigureAwait" Action="Warning" />
 5    </Rules>
 6    <Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
 7        <Rule Id="CA1001" Action="Warning" />
 8        <Rule Id="CA1009" Action="Warning" />
 9        <Rule Id="CA1016" Action="Warning" />
10        <Rule Id="CA1033" Action="Warning" />
11        <Rule Id="CA1049" Action="Warning" />
12        <Rule Id="CA1060" Action="Warning" />
13        <Rule Id="CA1061" Action="Warning" />
14        <Rule Id="CA1063" Action="Warning" />
15        <Rule Id="CA1065" Action="Warning" />
16        <Rule Id="CA1301" Action="Warning" />
17        <Rule Id="CA1400" Action="Warning" />
18        <Rule Id="CA1401" Action="Warning" />
19        <Rule Id="CA1403" Action="Warning" />
20        <Rule Id="CA1404" Action="Warning" />
21        <Rule Id="CA1405" Action="Warning" />
22        <Rule Id="CA1410" Action="Warning" />
23        <Rule Id="CA1415" Action="Warning" />
24        <Rule Id="CA1821" Action="Warning" />
25        <Rule Id="CA1900" Action="Warning" />
26        <Rule Id="CA1901" Action="Warning" />
27        <Rule Id="CA2002" Action="Warning" />
28        <Rule Id="CA2100" Action="Warning" />
29        <Rule Id="CA2101" Action="Warning" />
30        <Rule Id="CA2108" Action="Warning" />
31        <Rule Id="CA2111" Action="Warning" />
32        <Rule Id="CA2112" Action="Warning" />
33        <Rule Id="CA2114" Action="Warning" />
34        <Rule Id="CA2116" Action="Warning" />
35        <Rule Id="CA2117" Action="Warning" />
36        <Rule Id="CA2122" Action="Warning" />
37        <Rule Id="CA2123" Action="Warning" />
38        <Rule Id="CA2124" Action="Warning" />
39        <Rule Id="CA2126" Action="Warning" />
40        <Rule Id="CA2131" Action="Warning" />
41        <Rule Id="CA2132" Action="Warning" />
42        <Rule Id="CA2133" Action="Warning" />
43        <Rule Id="CA2134" Action="Warning" />
44        <Rule Id="CA2137" Action="Warning" />
45        <Rule Id="CA2138" Action="Warning" />
46        <Rule Id="CA2140" Action="Warning" />
47        <Rule Id="CA2141" Action="Warning" />
48        <Rule Id="CA2146" Action="Warning" />
49        <Rule Id="CA2147" Action="Warning" />
50        <Rule Id="CA2149" Action="Warning" />
51        <Rule Id="CA2200" Action="Warning" />
52        <Rule Id="CA2202" Action="Warning" />
53        <Rule Id="CA2207" Action="Warning" />
54        <Rule Id="CA2212" Action="Warning" />
55        <Rule Id="CA2213" Action="Warning" />
56        <Rule Id="CA2214" Action="Warning" />
57        <Rule Id="CA2216" Action="Warning" />
58        <Rule Id="CA2220" Action="Warning" />
59        <Rule Id="CA2229" Action="Warning" />
60        <Rule Id="CA2231" Action="Warning" />
61        <Rule Id="CA2232" Action="Warning" />
62        <Rule Id="CA2235" Action="Warning" />
63        <Rule Id="CA2236" Action="Warning" />
64        <Rule Id="CA2237" Action="Warning" />
65        <Rule Id="CA2238" Action="Warning" />
66        <Rule Id="CA2240" Action="Warning" />
67        <Rule Id="CA2241" Action="Warning" />
68        <Rule Id="CA2242" Action="Warning" />
69        <Rule Id="CA1012" Action="Warning" />
70    </Rules>
71    <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
72        <Rule Id="SA1305" Action="Warning" />
73        <Rule Id="SA1412" Action="Warning" />
74        <Rule Id="SA1600" Action="None" />
75        <Rule Id="SA1609" Action="Warning" />
76    </Rules>
77</RuleSet>

Of course you can choose to ignore rules (Action=“None”) and get warnings (Action=“Warning”) or errors (Action=“Error”) in the case your code is not compliant.

6. Build the application and check the warnings#


Run the following command:

1dotnet build

You can get the code here.

Hope it helps!