• Americaneagle.com
  • Development

Enforce C# Coding Standards during Web Application Development with StyleCop.MSBuild

Alex Hawley | April 13, 2016 Comments
Standards with Style
It’s no secret that transitioning site development to a different team is rarely done without a headache, usually for all the parties involved. However, these transitions are sometimes unavoidable since initial development and post development are often handled by completely separate teams. Tight deadlines can often make this transition even more difficult when commented code is scarce and coding standards are ignored. Educating programmers on coding standards and best practices can help alleviate the confusion that happens during a transition. Clean, consistent, and commented code is going to have a bunch better chance of transitioning than code that is confusing, inconsistent, and not uniform. Still, even with educated programmers, standards can be ignored. One option to combat this is to utilize StyleCop for forcing developers into a set of coding standard rules. If their code doesn’t meet the StyleCop standards then they are not able to compile the solution. Setting up StyleCop is easy and can be done prior to initial project development:

  1. Install the StyleCop Visual Studio Extension
    Every developer that will be working on the application with StyleCop should install the Visual StyleCop Extension:

    Web Design Tools 1

  2. Add the StyleCop.MSBuild NuGet package to the C# Project
    After developers have installed the Visual StyleCop extension StyleCop.MSBuild needs to be added to the C# project for enforcing the rules. StyleCop.MSBuild can be added easily through NuGet:

    Web Design Tools 3

  3. Enforce StyleCop warnings as errors 
    Once the NuGet Package is installed, StyleCop will begin to throw warnings for any code rules that are not passed. Warnings are OK, but we want these to be errors so the solution doesn’t compile.  With errors being thrown, developers will have no option but to fix all their styling before they can successfully build.

    Web Design Tools 5

  4. -Edit the .csproj file in Visual Studio

    -In the first <ProjectGroup> section, add:


    -Save the file and reload the project

  5. Adjust StyleCop settings as needed. Now that the warnings are set to be treated as errors, the solution can no longer build until they are resolved. For example, property headings are required by default in StyleCop. If one is missing, an error will be received like the following:

Web Design Tools 6

Web Design Tools 7

When I add the header, the error is resolved and the build succeeds. Error info also provides the specific StyleCop rule that is being enforced. Rules can be toggled by right-clicking on the project and selecting “StyleCop Settings”

                              Web Design Tools 8
            Web Design Tools 9

StyleCop provides a slew of different options that can be configured for enforcing. Don’t want to enforce property summaries and file headers? Easily turn them off. Want to make sure everyone is spelling correctly in their summaries and ends their comments with a period? There are options for those as well.

Introducing programming standards like this will likely slow down development as developers begin to familiarize themselves with the new rules and limits.  However, it will save a lot of time when transitioning to different teams or back to the client. StyleCop will not fix all issues that occur during a project transition, but it is sure to help with many.

1 review

Write a review
  1. Adam Ralph | Apr 15, 2016

    Good article.

    I just wanted to point out one thing - no developer needs to install the StyleCop Visual Studio extension for this to work. All you need is StyleCop.MSBuild installed in your projects, and the analysis will run on every build on every machine, whether that machine has StyleCop installed or not.

    Write a review


    • Ahmed Okour
    • Paul-Ross-Blogger-Bio-Pic
    • Rachel-B-Web
    • Courtney-V4
    • scottstiles
    • stu-3
    • Joseph Gustafson
    • Vince Scarlata
    • Tim Ahlenius
    • Staff Blogs
    • Shawn Griffin
    • Americaneagle Partner
    • Nick Goodrum
    • Missy Hildebrand
    • Mike Avello
    • Corte Swearingen
    • Adrian Krzeszkiewicz
    • Emily Stark