Microsoft.CodeAnalysis.BannedApiAnalyzers
Tags: csharp, dotnetcore, productivity, codequality
Published: 2023-03-19 19:15:10

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](https://www.nuget.org/packages/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](https://www.nuget.org/packages/Microsoft.CodeAnalysis.BannedApiAnalyzers) 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.

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

  <ItemGroup>
      <AdditionalFiles Include="BannedSymbols.txt" />
  </ItemGroup>
```

## 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
```

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

```
T:System.Data.SqlClient.SqlConnection; Use EF Core instead
```
On the same line, prevent a local method from being used.

```
M:Library1.Classes.DataOperations.ReadData; use EF Core instead
```

### Example 2
Disallow using **Newtonsoft.Json.JsonConvert**, use **System.Text.Json.JsonSerializer** instead.

```
T:Newtonsoft.Json.JsonConvert;Use System.Text.Json.JsonSerializer instead
```

### Example 3
Disallow **System.DateTime.Now** in favor of Use **System.DateTime.UtcNow**

```
P:System.DateTime.Now;Use System.DateTime.UtcNow instead
```

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

Library1 class project Person class

```csharp
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; }
}
```

Library2 class project Person class

```csharp
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; }
}
```

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.
```
## Finding broken rules.

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


![Error List window in Visual Studio](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ri92zf1b49ecu55splor.png)

What the code appears like with broken rules


![Broken rules in the code editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7xs8kh3rfkn6ftvnmj6.png)

## Rules cheat sheet

See the following [page](https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/BannedApiAnalyzers.Help.md)

## 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](https://github.com/karenpayneoregon/banned-api-analyzers).