How to fix "System.Management is supported on Windows Desktop Apps only" Exception

2.5k Views Asked by At

The Issue:

Our application is a .NET 5 Windows Desktop application. The licensing capability requires the application to use the System.Management component from NuGet to create a unique device ID from the system processor ID, MAC Address and BIOS serial number.

This all worked great when testing it from Visual Studio debugger. However, when adding System.Management.dll to the application installer (InstallShield installer) I naturally selected the version of System.Management.dll that was in the app's build target folder. It turns out that the version of the DLL that ends up in that folder is a no-op version that throws not supported exceptions saying "System.Management is supported on Windows Desktop Apps only".

2

There are 2 best solutions below

0
On BEST ANSWER

I think perhaps this is because the deps.json file is missing, causing the stub version to be loaded from the application directory, rather than the one from the runtimes directory. So either the desired dependency could be installed to the application root directory, or the existing hierarchy from the build directory could be maintained, but also distributing the dependency configuration.

https://learn.microsoft.com/en-us/dotnet/core/dependency-loading/default-probing

0
On

The Solution:

I found the correct version of System.Management.dll in the build TargetFolder\runtimes\win\lib\netcoreappX.x folder. In my case, since I'm using System.Management version 6.0, it is in the "netcoreapp3.1" folder.

Conclusion

There are many cases where Microsoft doesn't do the right thing. In this case, my application was specifically a .NET 5 Windows Desktop application, so we would all think that msbuild could figure out the correct thing to do. Not so. In such a case, I would have at least hoped that .NET Foundation would have provided information on this. Well, now you all know now, as well!

Cheers