dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.35k stars 4.74k forks source link

System.Management 9.0.0 throws an exception #109860

Open GitUser200607 opened 5 hours ago

GitUser200607 commented 5 hours ago
            var query = new WqlEventQuery(string.Format(
            "SELECT * FROM RegistryValueChangeEvent WHERE Hive = 'HKEY_LOCAL_MACHINE' AND KeyPath = '{0}' AND (ValueName = '{1}' OR ValueName = '{2}')",
            keyPath.Replace("\\", "\\\\"), valueName1, valueName2));

The exception is:

System.PlatformNotSupportedException: 'System.Management currently is only supported for Windows desktop applications.'

But version 8.0.0 does not throw the exception.

dotnet-policy-service[bot] commented 5 hours ago

Tagging subscribers to this area: @dotnet/area-system-management See info in area-owners.md if you want to be subscribed.

ViktorHofer commented 4 hours ago

What's the OS that you are running on? Please share a small repro so that we can diagnose this further.

GitUser200607 commented 3 hours ago

SystemManagementErrorExample.zip

The problem with <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework> in file SystemManagementErrorExample.csproj if I set <TargetFramework>net8.0-windows8.0</TargetFramework> it runs fine, but for MY project I need windows10.0.19041.

OS: Windows 10 Pro Version 22H2 OS build 19045.5131

ViktorHofer commented 2 hours ago

Using the 9.0.0 packages with a net6.0 TFM isn't a supported scenario. See the errors below that I get when I build:

PS C:\Users\vihofer\Downloads\SystemManagementErrorExample\SystemManagementErrorExample> dotnet run
C:\Users\vihofer\.nuget\packages\system.codedom\9.0.0\buildTransitive\netcoreapp2.0\System.CodeDom.targets(4,5): warning : System.CodeDom 9.0.0 doesn't support net6.0-windows10.0.19041.0 and has not been tested with it. Consider upgrading your TargetFramework to net8.0 or later. You may also set <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk.
C:\Users\vihofer\.nuget\packages\system.management\9.0.0\buildTransitive\netcoreapp2.0\System.Management.targets(4,5): warning : System.Management 9.0.0 doesn't support net6.0-windows10.0.19041.0 and has not been tested with it. Consider upgrading your TargetFramework to net8.0 or later. You may also set <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk.
C:\Users\vihofer\.nuget\packages\system.codedom\9.0.0\buildTransitive\netcoreapp2.0\System.CodeDom.targets(4,5): warning : System.CodeDom 9.0.0 doesn't support net6.0-windows10.0.19041.0 and has not been tested with it. Consider upgrading your TargetFramework to net8.0 or later. You may also set <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk.
C:\Users\vihofer\.nuget\packages\system.management\9.0.0\buildTransitive\netcoreapp2.0\System.Management.targets(4,5): warning : System.Management 9.0.0 doesn't support net6.0-windows10.0.19041.0 and has not been tested with it. Consider upgrading your TargetFramework to net8.0 or later. You may also set <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk.
C:\Users\vihofer\.nuget\packages\system.codedom\9.0.0\buildTransitive\netcoreapp2.0\System.CodeDom.targets(4,5): warning : System.CodeDom 9.0.0 doesn't support net6.0-windows10.0.19041.0 and has not been tested with it. Consider upgrading your TargetFramework to net8.0 or later. You may also set <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk.
C:\Users\vihofer\.nuget\packages\system.management\9.0.0\buildTransitive\netcoreapp2.0\System.Management.targets(4,5): warning : System.Management 9.0.0 doesn't support net6.0-windows10.0.19041.0 and has not been tested with it. Consider upgrading your TargetFramework to net8.0 or later. You may also set <SuppressTfmSupportBuildWarnings>true</SuppressTfmSupportBuildWarnings> in the project file to ignore this warning and attempt to run in this unsupported configuration at your own risk.

When I downgrade the package version to 8.0.0 (which supports net6.0), the warnings go away and the app runs successfully. The technical explanation is that when targeting net6.0, the netstandard2.0 System.Management assembly is used instead which doesn't work with net6.0.

GitUser200607 commented 24 minutes ago

So what should I do to evade this error? Downgrade the package?

vcsjones commented 15 minutes ago

So what should I do to evade this error?

Ideally you would move to a supported version of .NET, since .NET 6 recently went out of support, either .NET 9 or .NET 8.