Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Standard/.NET Core Code Analysis and Code Contracts with VS 2017

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.

like image 477
ameya Avatar asked Jun 01 '17 06:06

ameya


People also ask

What is the difference between .NET Standard and .NET Core?

NET Core is an implementation of the . NET Standard that's optimized for building console applications, Web apps and cloud services using ASP.NET Core. Its SDK comes with a powerful tooling that in addition to Visual Studio development supports a full command line-based development workflow.

Is .NET standard compatible with .NET framework?

NET Framework, Mono, Xamarin or Unity has to implement - at minimum - to support that version of the Standard. For library authors targeting . NET Standard provides the same feature set across all supported platforms - if it compiles to . NET Standard it'll very likely run on those frameworks.

What is the difference between .NET framework core and .NET standard class library project types?

NET Standard is an API specification that defines, for a given version, what Base Class Libraries must be implemented. . NET Core is a managed framework that is optimized for building console, cloud, ASP.NET Core, and UWP applications.


1 Answers

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>
like image 135
crimbo Avatar answered Sep 27 '22 19:09

crimbo