Friday, August 19, 2016

Static Code Analysis in ASP.NET Core

To ensure conformance of coding styles in team, I wanted to enable the Static Code Analysis on our ASP.NET Core project. I spent couple of hours identifying the current version of StyleCop and additional few hours to configure it on my solution. This blog outlines the configuration of my project to perform StyleCop static code analysis and also to override some of the default rules.

For the .NET Compiler Platform ("Roslyn") StyleCop provides the static code analysis. Here is the GitHub location for this tool.

As per the documentation mentioned on the GitHub site, StyleCop can be installed using NuGet Package Manager.

Alternatively you can manually add the dependency in project.json
    "StyleCop.Analyzers": "1.0.0"

It is recommended that the package be marked as build type as shown below:
    "StyleCop.Analyzers": {
      "version": "1.0.0",
      "type": "build"

Once StyleCop is installed, you can notice the warnings after building the project

To effectively utilize StyleCop, it needs to be configured as below:

  • One of the default rule, SA1633, requires that you need to specify the copyright information. Here is the copyright text I am using on top of each class file
// <copyright file="ChartFactory.cs" company="Nootus">
//  Copyright (c) Nootus. All rights reserved.
// </copyright>
// <description>
//  This is factory class which is used to create and format data for displaying charts
// </description>
  • For this copyright to properly work, we need to define a config file
  • Add “stylecop.json” to the project or solution
  • Specify the proper copyrightText. Here is my stylecop.json
  "$schema": "",
  "settings": {
    "documentationRules": {
      "companyName": "Nootus",
      "copyrightText": "Copyright (c) Nootus. All rights reserved."
  • The compiler needs to be told about this config file. For that add the following to the project.json
  "buildOptions": {
    "additionalArguments": [ "/additionalfile:stylecop.json" ]
  • Note that you need to specify the relative path of stylecop.json

The above configuration should enable default rules on the project.

In addition to running the default rules, you can also specify particular rules or override any of the rules. For my requirement, I want to disable the xmlDoc option (rule SA1652). Below are the configuration steps need to disable this rule.
  • Create a rule file where you can specify the override. I am calling this file as stylecop.ruleset. Here is my rule file where I am specifying not to check the SA1652 rule.
<?xml version="1.0" encoding="utf-8"?>
<RuleSet Name="Override Rules" Description="Override Rules for the solution." ToolsVersion="14.0">
  <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
    <Rule Id="SA1652" Action="None" />
  •  This rule file needs to be specified in the build option of the compiler. Modify the project.json to include the rulefile as shown below
"buildOptions": {
  "additionalArguments": [ "/ruleset:stylecop.ruleset", "/additionalfile:stylecop.json"]
With the above configuration our project will be ready for the Static Code Analysis by StyleCop. Next step is to get conformance of coding styles in team. Happy coding :)


  1. thanks for shared wonderful information of giving best information.its more useful and more helpful. great doing keep sharing
    Digital Marketing Course in Chennai

    1. I have read your blog its very attractive and impressive. I like it your blog.

      Dot Net Training in Chennai Dot Net Training in Chennai .Net Online Training .Net Online Training Dot Net Training in Chennai

      Dot Net Online Training .Net Online Training ASP.NET Online Training ASP.NET Online Training

  2. nice blog too informative. looking and reading your points its so impressive. doing more blog like this. i really appreciated doing like this.
    Digital Marketing Course in Chennai

  3. Nice Blog. Thank you for Sharing. Enterprise Resource Planning Software Solutions in Chennai. For more details contact us
    erp in Chennai | erp providers chennai

  4. I have read your blog its very Interesting. Thanks for sharing. erp software providers in chennai | erp software development company chennai. We provide best quality cost effective ERP software applications and end to end enterprise solutions to all the industry verticals with the affordable cost.

  5. I guess if you want to get the best result you will use the ebst programmes and I am pleased to have found your site.

  6. Your website content nice nice and interesting to observe.
    jobbörse Neunkirchen

  7. Thanks For Your valuable posting, it was very informative
    office 2011 mac download deutsch

  8. Good post and I like it very much. By the way, anybody try this app development company for iOS and Android? I find it is so professional to help me boost app ranking and increase app downloads.