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.

4
  • .net standart only relized, just wait couple of months Commented Jun 1, 2017 at 6:19
  • 3
    Enable Code Analysis seems to still be projected for a future release. Commented Jun 1, 2017 at 6:30
  • And Code Contracts seems to have withered and died Commented Jun 1, 2017 at 7:48
  • That's strange! Code contracts were useful, so the only alternative then is for applications to weave the custom contract code into the assembly after the build then ... Commented Jun 1, 2017 at 17:15

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

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.