Global suppression for entire solution (C#)

8k Views Asked by At

Does any one of you know a way I'd be able to suppress e.g. CA2000 for an entire solution?

I'm thinking of something like a GlobalSuppressions class with the following line of code:

[assembly: SuppressMessage("Microsoft.Reliability", "CA2000:DisposeObjectsBeforeLosingScope")]

Thanks for your help in advance.

3

There are 3 best solutions below

0
Fredrik C On

The best way of doing this is by creating a Directory.Build.props file which will be automatically included in all projects in sub-folders to where it is located. Then add the following to the file:

<?xml version="1.0" encoding="utf-8" ?>
<Project>
  <!-- This file will be automatically included in all projects because of its name -->
  <PropertyGroup>
    <NoWarn>CA2000</NoWarn>
  </PropertyGroup>
</Project>
0
Vijay Nirmal On

Global Suppressions can be configured in .editorconfig file.

  1. Create a file called .editorconfig in the root folder of your solution
  2. Open in a text editor
  3. Add root = true in first line
  4. Below that line add the rules to disable
    [*.{cs,vb}]
    dotnet_diagnostic.<Rule_Code>.severity=silent
    

Here is an example .editorconfig file

root = true

[*.{cs,vb}]
dotnet_diagnostic.CA2000.severity=silent

[*.{cs,vb}]
dotnet_diagnostic.MA0004.severity=silent    # Even works with 3rd part rules/analyser 
0
mBardos On

In case anyone stumbles on this, it is possible to create a global suppressions file (Eg: GlobalSuppressions.cs), add it "as link" to each project you need to. It should contains lines like:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Reliability", "CA2007:Consider calling ConfigureAwait on the awaited task", Justification = "ASP.NET Core doesn't use thread context to store request context.", Scope = "module")]

For more details see: https://learn.microsoft.com/en-us/visualstudio/code-quality/in-source-suppression-overview?view=vs-2022#suppress-violations-using-a-global-suppression-file