Using Sonar for your .Net project

Please note: Sonar .net plugins have been rewritten and version 1.0 has been released. Information on this blog relates to version 0.6 of the plugin. More information on the updated plugin can be found here

In my previous post, I covered how to install sonar, followed by how to setup a Java maven project to use sonar. You can also use sonar for your .net project – it’s not quite as straightforward as using it for Java, as you need to have various tools installed on the machine as prerequisites.

Firstly you need to install some plugins on the Sonar server. If your sonar server is running. You’ll need to stop it and then download the sonar .net plugins from the sonar .net plugin site and copy the jar files into you sonar extensions plugin folder.

The machine that will actually run the maven sonar analysis, will need to have a few prerequisites installed.

(1) Install Gallio onto your machine – With Gallio you can run different types of test frameworks, NUnit, MSTest, MBunit etc.. all under the one tool.

(2) Install FXCop 10, unfortunately this a little bit tricky, as only Visual Studio Premium comes shipped with FXCop 10, however, if you download and install the Windows SDK vers 7.1 (warning though, it’s a big download/install), you can install FXCop from the bin folder of the Windows SDK, follow instructions here

(3) Install PartCover 4 and install onto the machine (this allows you to get code coverage stats).

(4) Install source monitor onto your machine

The way sonar-net plugin works, is that it’s a maven plugin, so to build your Visual Studio solution, you actually have to use maven and create a pom.xml for your visual studio solution. So basically you will run maven and it will build your visual studio project for you and generate the sonar metrics for you.

Note if you are using 64 bit OS..

If you are using a 64 bit OS, currently you have to patch PartCover.exe and Gallio.Echo.exe to be 32 bits, otherwise you will not get any code coverage stats. You can patch the binaries using the tool CoreFlags.exe (this is provided in the windows sdk tool).

So open up a command prompt (but you have to open using “Run as Administrator” otherwise you’ll get a message unable to open file if you don’t).

path=C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin;%PATH%
cd "C:\Program Files (x86)\PartCover\PartCover .NET 4.0"
corFlags.exe PartCover.exe /32BIT+ /Force

cd "C:\Program Files (x86)\Gallio\bin"
corFlags.exe Gallio.Echo.exe /32BIT+ /Force

32 bit OS users can ignore the above step.

Here is the DistanceConverter-Net project to use, (again like the Java version, nothing much in it but enough to do a sonar analsysis). Note, this sample project is using NUnit for the unit tests, you will need to have NUnit installed on the machine to run sonar analysis. Unzip that to a directory run the command

mvn clean install -Psonar-dotnet sonar:sonar

If you’re running for the first time, expect it to take a while for the all maven dependencies to be downloaded. If all goes well, your sonar dashboard should be updated

8 Replies to “Using Sonar for your .Net project”

  1. I am in need of runnnig statci code analysis using fxCop for C#..NET projects and display its results in Sonar ? I am new to sonar . Can I run FxCop from Sonar on .NET code. How would I do this and get the resutls displayed in sonar ?

    1. You can run sonar analysis on your C# .net projects and the results are uploaded onto the sonar database for reporting. Sonar analysis can run FXCop, StyleCop, Copy Paste Detection and Code coverage. Sonar plugins have been rewritten and it’s now easier to run (no need to install maven, you can use the sonar-runner instead) please follow the instructions on the sonar c# plugins page

      If you’re new to Sonar, first I suggest you make sure you have installed sonar correctly and you can view the sonar web page correctly. Then I’d attempt to run a sonar analysis on C# project and check the results get published onto your sonar site. The second screenshot on this blog shows some of the messages from FXCop and StyleCop being reported on the Sonar site.

  2. Hi,
    I have a xml test report with test cases passed,failed,skipped,problem and marginal options.Can I use SONAR as a dashboard tool to display how many test cases fall under each category and difference between any two successive builds.

    1. hi venkata kishore,

      i need to help from you how to use sonar please help me if you dont mind give me your contact number i call back to you………..waiting for your reply..

      Thanks&Regards,
      choudary

  3. HI
    Am having problem in runnnng silverlight proejct for Sonar. it could not able to find mscorlib.dll. I already given path for silverlight like
    C:/Program Files (x86)/Reference Assemblies/Microsoft/Framework/Silverlight/v4.0

    but still am having the issue. please help me out.

Leave a Reply

Your email address will not be published. Required fields are marked *