2

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 ?

1
  • If you install the Microsoft.CodeAnalysis.NetAnalyzers NuGet package, you should not add the EnableNETAnalyzers property to either your project file or a Directory.Build.props file. When the NuGet package is installed and the EnableNETAnalyzers property is set to true, a build warning is generated. Please see this article for more detail: learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/… Commented Jan 7, 2022 at 16:28

2 Answers 2

1

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?

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.