Enabling Microsoft's Code Analysis on .NET Core Projects

后端 未结 2 827
天命终不由人
天命终不由人 2020-12-29 03:17

Our team uses the Code Analysis feature with a custom ruleset to cause our build to fail if we forget to do things like null checks on method arguments.

However, now

相关标签:
2条回答
  • 2020-12-29 03:31

    Normally, the only thing you need to do is install the Microsoft.CodeAnalysis.FxCopAnalyzers nuget on your project.

    But as mentioned correctly, this does not work, especially for .Net Core (currently in vs2017).

    In order to work for aspnet core projects, as well, and resolve the error:

    "Could not identify platform for ..."

    Manually modify the project's csproj file and make sure to not insert the RunCodeAnalysis tag. Make the PropertyGroup like that:

    <PropertyGroup>
      <TargetFramework>netcoreapp2.2</TargetFramework>
      <CodeAnalysisRuleSet>..\MyStylecop.ruleset</CodeAnalysisRuleSet>
    </PropertyGroup>
    

    Also, if you need to put a given ruleset file, make sure to place it to the correct path, as seen above ..\MyStylecop.ruleset. MyStylecop.ruleset is the file with the rules (actually the ones suppresed I think - so it is inverse logic).

    For example my ruleset file is:

    <?xml version="1.0" encoding="utf-8"?>
    <RuleSet Name="Default stylecop settings" Description="This rule set contains all rules (as warnings), with a few specific supressions." ToolsVersion="15.0">
      <IncludeAll Action="Warning" />
      <Rules AnalyzerId="Microsoft.Analyzers.ManagedCodeAnalysis" RuleNamespace="Microsoft.Rules.Managed">
        <Rule Id="CA1004" Action="None" />
        <Rule Id="CA1006" Action="None" />
        <Rule Id="CA1020" Action="None" />
        <Rule Id="CA1025" Action="None" />
        <Rule Id="CA1032" Action="None" />
        <Rule Id="CA1054" Action="None" />
        <Rule Id="CA1055" Action="None" />
        <Rule Id="CA1056" Action="None" />
        <Rule Id="CA1062" Action="None" />
        <Rule Id="CA1300" Action="None" />
        <Rule Id="CA1303" Action="None" />
        <Rule Id="CA1704" Action="Warning" />
        <Rule Id="CA1709" Action="None" />
        <Rule Id="CA2007" Action="None" />
        <Rule Id="CA2225" Action="None" />
        <Rule Id="CA2227" Action="None" />
        <Rule Id="CA2233" Action="None" />
        <Rule Id="CA2234" Action="None" />
        <Rule Id="CA2237" Action="None" />
        <Rule Id="CS1591" Action="None" />
        <Rule Id="CA1715" Action="None" />
      </Rules>
      <Rules AnalyzerId="StyleCop.Analyzers" RuleNamespace="StyleCop.Analyzers">
        <Rule Id="SA1101" Action="None" />
        <Rule Id="SA1116" Action="None" />
        <Rule Id="SA1117" Action="None" />
        <Rule Id="SA1118" Action="None" />
        <Rule Id="SA1208" Action="None" />
        <Rule Id="SA1600" Action="None" />
        <Rule Id="SA1601" Action="None" />
        <Rule Id="SA1602" Action="None" />
        <Rule Id="SA1623" Action="None" />
        <Rule Id="SA1633" Action="None" />
        <Rule Id="SA1634" Action="None" />
        <Rule Id="SA1637" Action="None" />
        <Rule Id="SA1640" Action="None" />
        <Rule Id="SA1652" Action="None" />
        <Rule Id="SA0001" Action="None" />
        <Rule Id="SA1314" Action="None" />
      </Rules>
    </RuleSet>
    
    0 讨论(0)
  • 2020-12-29 03:34

    Update

    Apparently the right way to do this is to install the Microsoft.CodeAnalysis.FxCopAnalyzers NuGet package. This works great, even on ASP.NET Core projects, and doesn't require the <RunCodeAnalysis> flag at all.

    Original Answer

    I realized that there's another tag in the csproj file which actually enables code analysis. The <PropertyGroup> tag in my .csproj file now looks like this:

      <PropertyGroup>
        <TargetFramework>netstandard1.4</TargetFramework>
        <CodeAnalysisRuleSet>..\MyCompanyCodeAnalysisRules.ruleset</CodeAnalysisRuleSet>
        <RunCodeAnalysis>true</RunCodeAnalysis>
      </PropertyGroup>
    

    And it works great, at least on normal projects. An ASP.NET Core project is producing the following errors:

    CA0055 : Could not identify platform for 'C:\Source\...\bin\Debug\netcoreapp1.1\....dll'.
    CA0052 : No targets were selected.
    
    0 讨论(0)
提交回复
热议问题