6

Are Static Code Analysis and Code Contracts not supported for .NET Standard ?

VS 2017 and .NET Standard 1.6 or .NET core class libraries do not seem to have options to run code analysis.

ameya
  • 1,448
  • 1
  • 15
  • 31

1 Answers1

2

You can make Code Contracts work for .NET standard projects (I have); however there is no VS 2017 IDE support for enabling Code Contracts in any project, let alone a netstandard project.

The Code Contracts rewriter (ccrewrite) currently crashes and burns if you run it on a project with portable PDBs. In my opinion, netstandard projects should have portable PDBs (it's the only PDB format that works cross-platform).

For me, this is the deal-breaker with respect to using Code Contracts on netstandard libraries long-term. However, we have a few internal netstandard libraries that use legacy/Windows PDBs with Code Contracts for the time being. We're using legacy/Windows-only PDBs with our netstandard libraries only because it was too much immediate effort to tear out all our Code Contracts code while preserving the integrity of the projects.

In my answer to another question about VS 2017 support for Code Contracts, I provide information on how to manually enable Code Contracts for VS 2017 builds. This will work for netstandard projects, if you also change the PDB type. This can be done using the project properties UI, or by adding something like the following to your csproj or imported msbuild file:

  <!-- For netstandard and netcoreapp, DebugType must be full or pdbonly for ccrewrite to work -->
  <PropertyGroup Condition=" '$(Configuration)' != 'Release' ">
    <DebugType>full</DebugType>
  </PropertyGroup>

  <PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
    <CodeContractsRuntimeCheckingLevel>ReleaseRequires</CodeContractsRuntimeCheckingLevel>
    <DebugType>pdbonly</DebugType>
  </PropertyGroup>
crimbo
  • 10,308
  • 8
  • 51
  • 55