dotnet runtime_exception ai_generated true

System.PlatformNotSupportedException: Operation is not supported on this platform.

ID: dotnet/platform-not-supported

Also available as: JSON · Markdown
75%Fix Rate
80%Confidence
45Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
8 active

Root Cause

PlatformNotSupportedException is thrown when calling APIs that are not available on the current OS or runtime. Common when using Windows-specific APIs (Registry, Windows Forms, WMI) on Linux containers, or when using APIs trimmed by the .NET linker. Fix by using cross-platform alternatives or conditional compilation.

generic

Workarounds

  1. 80% success Use cross-platform alternative APIs
    Replace Windows-specific APIs with cross-platform alternatives: use System.IO instead of Registry for config storage, use System.Diagnostics.Process instead of WMI for process management, use SkiaSharp instead of System.Drawing for image processing. Check the .NET Platform Compatibility Analyzer (CA1416) warnings
  2. 78% success Use conditional compilation or runtime platform checks
    Use OperatingSystem.IsWindows()/IsLinux() for runtime checks: if (OperatingSystem.IsWindows()) { /* Windows-only code */ } else { /* cross-platform fallback */ }. For compile-time: #if WINDOWS ... #else ... #endif with <DefineConstants>WINDOWS</DefineConstants> in .csproj. Enable the platform compatibility analyzer by adding <EnableNETAnalyzers>true</EnableNETAnalyzers>

Dead Ends

Common approaches that don't work:

  1. Installing Windows compatibility packages on Linux to get Windows-only APIs 70% fail

    The Microsoft.Windows.Compatibility pack provides some APIs on Linux but many still throw PlatformNotSupportedException at runtime because they depend on Windows OS services (Registry, WMI, COM) that simply do not exist on Linux

  2. Using reflection to bypass platform checks and call internal methods 85% fail

    Platform checks exist because the underlying OS capability is absent; bypassing the check just moves the failure to a lower level with a more cryptic error (DllNotFoundException, EntryPointNotFoundException) or causes undefined behavior

Error Chain

Leads to:
Preceded by:
Frequently confused with: