New users as well as users of the recent versions of the C# plugins should consult this page.

 

 

 

 

 

 

 

Below:

Requirements

Your .NET solution must be compiled prior to the SonarQube analysis. The SonarQube analysis must be run in the same directory where the solution was compiled.

By default, the C# plugin triggers the execution of some external tools. To deactivate some of them, you can set the below properties:

sonar.fxcop.mode=skip
sonar.gendarme.mode=skip
sonar.gallio.mode=skip
sonar.ndeps.mode=skip
sonar.stylecop.mode=skip

Running an Analysis with the SonarQube Runner

  1. Compile your solution. SonarQube's primary goal is to analyze source code, so everything has been made to work seamlessly after the compilation of a solution. In other words, if you have a build process that moves the assemblies and packages your application, it is definitely best to run SonarQube before (or in a separate process).

  2. Create a sonar-project.properties file and place it in the same folder as the solution file (.sln):

    sonar.projectKey=com.mycompany:myCSharpApplication
    sonar.projectVersion=1.0
    sonar.projectName=My CSHARP Application
    
    sonar.language=cs
    
    # To prevent any issues while analyzing multiple solutions containing projects with similar keys
    # Will be set by default to safe starting at version 2.2: http://jira.codehaus.org/browse/SONARDOTNT-339
    sonar.dotnet.key.generation.strategy=safe
     
    # This property is set because it is required by the SonarQube Runner.
    # But it is not taken into account because the location of the source
    # code is retrieved from the .sln and .csproj files.
    sonar.sources=.
    
    



  3. Run the following command from the directory containing the sonar-project.properties file (= the directory containing the solution):

    sonar-runner

A sample project is available on GitHub that can be browsed or downloaded: /projects/languages/csharp.

Path Patterns

Always use "/" instead of "\" (even if it looks weird on a Windows box).

When noted, Ant style wildcard patterns can be used. * means any file or directory. ** means any directories and subdirectories:

# Relative path that refers to any dll files prefixed by Foo in the solution
**/Foo*.dll

# Absolute path that refers to any dll files in any sub-directories of the lib directory of the drive T
T:/lib/**/*.dll

 

In some advanced use cases (which should not occur too often if you follow our best practices), the following placeholders can be used in path patterns:

ExpressionDescription
$(SolutionDir)Root directory of the solution, this is the directory where the ".sln" file is located.
$(ProjectName)Name of the currently analyzed project of the solution.
$(AssemblyName)Name of the assembly generated by the currently analyzed solution. Works only for non-ASP projects.
$(AssemblyVersion)Version of the assembly generated by the currently analyzed solution.
$(OutputType)Type of the assembly. Can be "dll" or "exe".
$(RootNamespace)Root namespace of the currently analyzed project.

For example:

$(SolutionDir)/../lib/**.*dll
$(SolutionDir)/BUILD/**/$(AssemblyName).$(OutputType)

If you start using absolute paths or placeholders in path patterns, this means that your configuration becomes more complex and you are likely to face issues sooner or later. Everything has been done to make sure you need to write as few configuration lines as possible.

Advanced Usage

Setting the .NET SDK to use

To set the default version of the .NET SDK to be used, log in as a System administrator and go to Settings > Configuration > General Settings > .NET and set the sonar.dotnet.version property. Note that you can override this default value in your analysis configuration file.

The same parametrization is available for the Silverlight Framework: sonar.silverlight.version property.

Setting exclusions

By default, generated code is excluded from the analysis (Reference.cs or *.designer.cs files): sonar.dotnet.excludeGeneratedCode is set to true.

To exclude projects, use the sonar.skippedModules property. It is a comma-separated list of project names. The names that must be used are the identifiers in the solution file: the first string right after the equals sign on a project definition.
Known limitation: this property does not currently work while the "sonar.dotnet.key.generation.strategy" is set to "safe". See SONARDOTNT-10.

# To exclude "FooProject":

# ...
# Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "FooProject", "FooProject", "{59BECB8B-A7E3-4823-9CE6-584D0D1755EE}"
# ...

sonar.skippedModules=FooProject

To exclude files, see Project Administration.

On some specific components, you can also prevent issues from being raised against a set of coding rules: see the Switch Off Violations plugin.

Detecting issues with external tools

See FxCop, Gendarme and StyleCop.

Unit tests, integration tests and code coverage

See Unit Tests, Integration Tests and Code Coverage.

Architecture check

See NDeps plugin.

FAQ

Analysis succeeds but too few issues are found

Some tools like FxCop or Gendarme require assemblies. Having too few issues most often comes from the following reasons:

I cannot place the analysis configuration file in the directory containing my solution

Before following this workaround, make sure that you've got strong reasons not to put the analysis configuration file into the folder containing the solution. Note that this workaround may lead to further tricky configuration and issues.

# Relative path from the analysis configuration file to the folder containing the solution
sonar.dotnet.visualstudio.solution.file=my_solution_directory/solution_file.sln

SonarQube cannot retrieve the assemblies

The configuration defined to build the solution will be used to locate the assemblies during the SonarQube analysis. Two properties are taken into account:

Let's take the folowing example:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\Debug\</OutputPath>  <!-- SonarQube will try to retrieve the assembly from this folder -->
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\Release\</OutputPath>  <!-- SonarQube will try to retrieve the assembly from this folder -->
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  ...

If you use the default configuration (Debug|AnyCPU), SonarQube will try to retrieve the assembly of this project in <project_directory>/bin/Debug.

If you set the properties to:

sonar.dotnet.buildConfiguration=Release
sonar.dotnet.buildPlatform=AnyCPU

SonarQube will try to retrieve the assembly of this project in <project_directory>/bin/Release.

Depending on when you run SonarQube in your continuous integration process, it is very important to properly set those properties. A proper configuration will allow SonarQube to automatically retrieve the assemblies and smoothly perform the analysis. It is highly recommend that you run SonarQube right after the compilation phase.

For example, if you run SonarQube after the packaging phase that moves all the assemblies into a single directory, you will face over-complicated configuration because you will have to tell SonarQube where to find the assembly for each project. To do so, set the sonar.dotnet.assemblies property. It is a path pattern that will be used to find the assembly of the analyzed project. The path pattern can be absolute or relative to the directory containing the ".csproj" file.

# If, for each project, the assembly is moved to a "bin/package" directory inside the project directory
sonar.dotnet.assemblies=bin/package/$(AssemblyName).$(OutputType)
 
# If all the assemblies are copied into a "package" directory located in the solution directory
sonar.dotnet.assemblies=$(SolutionDir)/package/$(AssemblyName).$(OutputType)

SonarQube C# parser known limitations

How can I integrate SonarQube in my existing TFS Build environment processes?

Most people want to add SonarQube as an additional step in existing processes, which usually brings a lot of troubles because those processes manipulate assemblies - whereas the SonarQube C# plugins rely best on information found in the source files (mostly "csproj" files).

Generally speaking, it's better / easier to set up a separate Continuous Inspection process that only:

  1. Compiles the solution
  2. Runs a SonarQube analysis

in order to keep the SonarQube-related configuration as simple as possible.

You can read this thread to see how this has been achieved by C# plugin users.

Why do I get MSBuild error such as "error MSB4126: The specified solution configuration "Debug|HPD" is invalid"?

You are using a 64-bit windows OS. There is an environment variable "Platform=HPD" that makes msbuild fail. Try to run:

set platform=[enter]
sonar-runner

On Windows 7 I get "The Silverlight 3/4 SDK is not installed" error message

64-bit msbuild cannot be used with Silverlight. Check that you are not using a 64-bit msbuild by taking a look at dotnet.3.5.sdk.directory properties. "Framework64" should not be present in the path.

The analysis fails with an OutOfMemory exception

The Java process performing the analysis might need more memory. Use the "-Xmx" option to specify the amount of memory that can be used. See the SonarQube Runner documentation for more details