Donate

.NET 5 - CA1416: This call site is reachable on all platforms. is only supported on Windows 7.0 and later

Hi All,

Our software architect just recently upgraded our custom class library that contains common data access classes, custom utilities and functions for our day to day tasks from .NET Framework 4.7x to .NET 5. Since we are only deploying our applications to windows users, he set the TargetFramework value to net5.0-windows in .csproj file.
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup Label="Globals">
    <SccProjectName>SAK</SccProjectName>
    <SccProvider>SAK</SccProvider>
    <SccAuxPath>SAK</SccAuxPath>
    <SccLocalPath>SAK</SccLocalPath>
  </PropertyGroup>

  <PropertyGroup>
    <TargetFramework>net5.0-windows</TargetFramework>
	<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
	<UseWPF>true</UseWPF>
	<PackageId>Utilities.XYZLibNET</PackageId>
	<Version>1.0.0.3</Version>
	<Authors>Development Team</Authors>
	<Description>Library of common classes and functions for net5.0-windows</Description>
	<Copyright>2021</Copyright>
	<PackageReleaseNotes>Initial Release</PackageReleaseNotes>	
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
    <PackageReference Include="System.Security.Principal.Windows" Version="5.0.0" />
  </ItemGroup>

  <ItemGroup>
    <None Update="XYZLib.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>
After compiling the said library, I referenced the dll file immediately to my project that targets the .NET 5.0 framework and called the database class methods in my application. To my surprise, it generates tons of warning messages with regards to CA1416: This call site is reachable on all platforms. "statement" is only supported on Windows 7.0 and later.
.NET Core - CA1416: This call site is reachable on all platforms. is only supported on Windows 7.0 and later
.NET Core - CA1416: This call site is reachable on all platforms. is only supported on Windows 7.0 and later
As per the documentation, .NET source code analysis, CA1416 is a warning about platform compatibility that are enabled by default of which these messages or violations are reported if a platform-specific API is used in the context of a different platform or if the platform isn't verified (platform-neutral). Violations are also reported if an API that's not supported for the target platform of the project is used. After researching for possible solutions, I found two workarounds to remove these warning messages. First is to disable the code analysis of the project (.csproj) referencing the .NET 5 dll by setting the EnableNETAnalyzers to false as mentioned in this document MSBuild reference for .NET SDK projects
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
	<EnableNETAnalyzers>false</EnableNETAnalyzers>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="GGSLibNet">
      <HintPath>..\..\..\Projects\bin\Debug\net5.0-windows\Utilities.dll</HintPath>
    </Reference>
  </ItemGroup>

  <ItemGroup>
    <None Update="XYZSettings.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>
The other solution is to change the TargetFramework of the project referencing the dll from net5.0 to net5.0-windows which is identical with the TargetFramework of our custom project library.
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0-windows</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Configuration.ConfigurationManager" Version="5.0.0" />
    <PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="GGSLibNet">
      <HintPath>..\..\..\Projects\bin\Debug\net5.0-windows\Utilities.dll</HintPath>
    </Reference>
  </ItemGroup>

  <ItemGroup>
    <None Update="XYZSettings.config">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
  </ItemGroup>

</Project>
After that, these warning messages will disappear.
CA1416: This call site is reachable on all platforms. is only supported on Windows 7.0 and later

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

How To Insert Or Add Emojis In Microsoft Teams Status Message

Pass GUID As Parameter To Action Using ASP.NET MVC ContribGrid