Microsoft.CodeAnalysis.BannedApiAnalyzers

Karen Payne - Mar 19 '23 - - Dev Community

Typically a profession team of developers will have rules for coding along with code reviews to ensure code is writen to the rules and guidelines of the team.

Even with code reviews rules can still be broken which brings us to a Roslyn Analyzer Microsoft.CodeAnalysis.BannedApiAnalyzers MCABAA NuGet package which provides a way to perform a pre-check on a code review and/or while writing code.

How to use MCABAA

  • Add the package to a project
  • Create a text file named BannedSymbols.txt in the root folder of the project.

Inspect the project file by double clicking the project file in Solution Explorer and note how the banned file was placed in.

  <ItemGroup>
      <None Remove="BannedSymbols.txt" />
  </ItemGroup>

  <ItemGroup>
      <AdditionalFiles Include="BannedSymbols.txt" />
  </ItemGroup>
Enter fullscreen mode Exit fullscreen mode

Adding rules

Example 1

Suppose for working with SQL-Server database with a data provider the package Microsoft.Data.SqlClient should not be used but instead Entity Framework Core. Add the following rule (as the first call is usually creating an instance of SqlConnection.

T:Microsoft.Data.SqlClient.SqlConnection; Use EF Core instead
Enter fullscreen mode Exit fullscreen mode

Another rule is needed in the event a developer attempts using System.Data.SqlClient

T:System.Data.SqlClient.SqlConnection; Use EF Core instead
Enter fullscreen mode Exit fullscreen mode

On the same line, prevent a local method from being used.

M:Library1.Classes.DataOperations.ReadData; use EF Core instead
Enter fullscreen mode Exit fullscreen mode

Example 2

Disallow using Newtonsoft.Json.JsonConvert, use System.Text.Json.JsonSerializer instead.

T:Newtonsoft.Json.JsonConvert;Use System.Text.Json.JsonSerializer instead
Enter fullscreen mode Exit fullscreen mode

Example 3

Disallow System.DateTime.Now in favor of Use System.DateTime.UtcNow

P:System.DateTime.Now;Use System.DateTime.UtcNow instead
Enter fullscreen mode Exit fullscreen mode

Example 4

The team has two versions of a class, in this case Person in two separate class projects.

Library1 class project Person class

public class Person
{
    public int Id { get; set; }
    public string Type { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime? BirthDate { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Library2 class project Person class

public class Person
{
    public int Id { get; set; }
    public string Type { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateOnly? BirthDate { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Library1 is for pre-existing project so it need to stay but for new projects, use Library2. We add the following.

T:Library1.Models.Person;Use Library2.Models.Person which uses DateOnly rather than DateTime.
Enter fullscreen mode Exit fullscreen mode

Finding broken rules.

Build the project(s) and check the Error List in Visual Studio.

Error List window in Visual Studio

What the code appears like with broken rules

Broken rules in the code editor

Rules cheat sheet

See the following page

Caveats

  • The file BannedSymbols.txt can be placed in a folder outside the project but may not always work, its hit and miss.
  • Rules can not be applied to language extension methods.

Source code

Rather than taking time to setup a project, clone the following GitHub repository.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .