Visual Studio Code Analysis on .Net Framework Projects

Viewed 426

I am attempting to enable code analysis via Nuget for an existing .Net Framework 4.72 Web Application. To be frank, the documentation has me confused as to which packages to install.

I have installed Microsoft.CodeAnalysis.NetAnalyzers via nuget.

The following is in my csproj file:

  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    ...
    <EnableNETAnalyzers>true</EnableNETAnalyzers>
    <AnalysisMode>AllEnabledByDefault</AnalysisMode>

This code is loaded with problems, but I only the following two items are listed:

warning CA2237: Add [Serializable] to ParseException as this type implements ISerializable
warning CA1001: Type 'Repository' owns disposable field(s) '_context' but is not disposable

Are there other packages to load ?

2 Answers

If you're seeing CA*** diagnostics, then it's working.

You can turn on more diagnostics in your projects via the "Dependencies" tree (expand the analyzers node).

Also, you have to add this PackageReference and associated properties to each project in your solution. You might like to do this in a Directory.Build.props file.

Why do you think you should be seeing more diagnostics?

The answer is in the References\Analyzers node of the .Net Framework project. I can verify the presence of Microsoft.CodeAnalysis.NetAnalyzers. The rules now default to minimum, which means there are only 2 issues in my app.

Right click on Analyzers|Open Active Ruleset... and the rules can be customized.

It did not help this legacy project is a little wonky. I had to make a new project and pull in the Analyzer peices.

Related