5 easy steps to run code analysis on your .NET Framework project with SonarQube

Quang Nguyen
4 min readAug 1, 2021

--

Photo by Caspar Camille Rubin on Unsplash

There are two ways to get a SonarQube server instance up and running on your local machine — via a zip file and use Docker. I’ll go with the second option in this post — Build a SonarQube server with Docker and Docker Compose.

Prerequisites

Install the SonarQube server locally

Step 1: Create a new docker compose file

Create a yaml file with the content below. Give it a name (Ex: sonarqube.yml) and then save it.

sonarqube.yml

Step 2: Run the docker containers using the compose tool

Open a terminal console and executes this command.

docker compose -f /path/to/sonarqube.yml up
Uses the compose tool to create multiple docker containers

Note: Basically, after you run the command above, all jobs are done automatically, and you will have two containers running successfully. But if you run into the error “max virtual memory areas vm.max_map_count […] is too low, increase to at least 262144”, then your sonar server couldn’t be started. Let’s do an ad-hoc step like this:

The “max virtual memory areas vm.max_map_count […] is too low” error
  • Step 2.1: Stop the sonarqube and the postgres container.
  • Step 2.2: Open a terminal console and run these commands:
wsl -d docker-desktop
sysctl -w vm.max_map_count=262144
Adjust the max_map_count config to the new value
  • Step 2.3: Up the containers again by following the command at step 3.
Two containers are up and running.

Step 3: Create a new sonar project

  • Step 3.1: Open the browser, enter the address is http://localhost:9000, and login to the SonarQube server with the default user/password (admin/admin).
Login page

After that, change the default password with your password is required. Note: It is run in the first login only.

  • Step 3.2: Create a new project by following these substeps: Menu > Projects > Add a project > manual > enter the Project key and Display name > Submit the form with the ‘Set Up’ button.
Create a new project.
  • Step 3.3: Create a authenticate token
Enter a name and submit the form by click on the Generate button.
SonaQube server provides a token used to identify you when an analysis is performed.

The token is important because it is used to identify you when an analysis is performed.

Almost done! Now you can follow the instruction of SonarQube to start run an analysis on your project.

Run analysis instruction.

Step 4: Install the SonarScanner for .NET and Java Runtime

  • Step 4.1: Download the scanner for .Net Framework 4.6+
  • Step 4.2: Extract the zip file and set the environment variable.
  • Step 4.3: Add the MsBuild to the environment variable.
Edit environment variable.
  • Step 4.4: Install Java Runtime and create the JAVA_HOME environment.

Step 5: Start run code analysis on the project

Execute the following commands at the root of your solution.

SonarScanner.MSBuild.exe begin /k:"age-calcuator" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="06a146c139773c9f8ba1b28aa12a22ed03447f6e"MsBuild.exe /t:RebuildSonarScanner.MSBuild.exe end /d:sonar.login="06a146c139773c9f8ba1b28aa12a22ed03447f6e"

Note: 06a146c139773c9f8ba1b28aa12a22ed03447f6e is the token we created in step 3.3

And tada! your code analysis report is intent at the SonarQube server — http://localhost:9000

Code analysis report.

Conclusions

SonarQube is a static code quality analysis and also a Static Application Security Testing (SAST) tool. It could help developers writing more secure code with SonarQube detecting Vulnerabilities and Security Hotspots, explaining them, and giving appropriate next steps. It was built to integrate with the CI pipeline to provides feedback during code review. But if you don’t have a CI environment on your local machine, you are still able to use the power of SonarQube to makes your code clean and safer with some simple steps.

References

--

--

Quang Nguyen

Software Engineer (C#/.NET, JavaScript, Microservices, K8s, Azure)