So I have a C# class library project that I only intend to use on Windows. It contains some classes that use the System.Drawing.Image class which is only available on Windows. After upgrading to Visual Studio 2022 and setting the target framework to .NET 6.0 I'm seeing a bunch of warnings that say:
CA1416"This call site is reachable on all platforms. 'SomeClass.SomeMethod' is only supported on: 'windows'.
See screenshot below for some examples:
In some sense, it's cool that VS2022 has scanned the library and found all the platform specific code that I'm using in the library. But I'd like to tell VS that I only plan to use the library on windows and it can mute all those warnings.
First I checked the Target framework option in the properties of the project but didn't seen any windows specific targets.
Then I decided to edit the project's .csproj directly and changed the Target framework from
<TargetFramework>net6.0</TargetFramework>
to
<TargetFramework>net6.0-windows</TargetFramework>
But, sadly, even after a recompile, that didn't make the warnings go away either. So then I did some reading on the CA1416 warnings and sure enough it says in the Microsoft Docs that the target framework moniker is ignored for assessing this warning, however, VS does add an attribute to the project based on the TFM that influences this warning, but it only does so if the project is configured to generate the AssemblyInfo.cs file on the fly. But, alas, my project's AssemblyInfo.cs is maintained as a actual file rather then having it auto generated at build time.
So at this point, I'm ready to punt the ball and just disable CA1416 warnings for my project. So in the project's .proj file I added CA1416 for both the release and debug builds like so:
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<NoWarn>1701;1702;CA1416;</NoWarn>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<NoWarn>1701;1702;CA1416;</NoWarn>
</PropertyGroup>
One would think that would be the end of those pesky warnings. (sigh) As it turns out, after rebuilding the project the warnings still show up. Got any suggestions? I'm all ears.



Update:
Targeting Windows worked fine until one of our developers tried to start the solution on his Apple computer using
Visual Studio 2022 for Mac Preview 1.https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1416
Reading .NET 6 Breaking changes Microsoft has a section about
System.Drawing.Common.https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/6.0/system-drawing-common-windows-only
Their recommendations are the following:
Even though
Microsoft.Maui.Graphicsis in preview and is considered an experimental library I tried to use it given that Microsoft has the library as a recommended action library.It seemed really promising at first but then I encountered a bug in their
IImage Downsizemethod.https://github.com/dotnet/Microsoft.Maui.Graphics/issues/247
Until that is fixed my temporary solution is using Target framework .NET 6, Target OS (none) and then use
Exclude specific warnings as errorsgiven that we have enabledTreat warnings as errors.I have also created a
runtimeconfig.template.jsonin our web project root with the following values:Original:
You can suppress the warning with
dotnet_diagnostic.CA1416.severity = nonebut imao if you only intend to use it on Windows you should set Target OS toWindowsfor the project which will fix the warning.https://learn.microsoft.com/en-us/dotnet/core/compatibility/code-analysis/5.0/ca1416-platform-compatibility-analyzer
Source:
https://stackoverflow.com/a/70272543/3850405