Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for .NET 5 (and later) #31

Closed
profix898 opened this issue Jun 23, 2021 · 8 comments
Closed

Add support for .NET 5 (and later) #31

profix898 opened this issue Jun 23, 2021 · 8 comments

Comments

@profix898
Copy link

DeviceId targets .NETStandard 2.0, so in principle it should work on net5.0. Unfortunately, the project uses the obsolete platform abstraction package, which is not available for net5.0 (except for an old preview version). For that reason, it would be great to have a net5.0 target. Once #25 is merged, the net5.0 version would be identical to the netStandard version, except for the deprecated dependency.

See https://docs.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/5.0/platformabstractions-package-removed for details.

@MatthewKing
Copy link
Owner

Thanks for filing this issue and providing the info. I've been super busy with life recently so I haven't had the time to give DeviceId as much love as it deserves - particularly when chasing the moving target that is netcore/net5.0/crossplat/etc.

I've pushed some code which I believe will address this issue. It'll remove the dependency on the PlatformAbstractions package, and replace WMI with MMI.

Would you mind having a look and letting me know if this would solve your issue?

Thanks

@profix898
Copy link
Author

I've been super busy with life recently

I know the feeling ... still DeviceId is a pretty useful library :)

Your latest changes are looking good. I built the project and used it in a net5.0 console app. It seems the net5.0 issue got resolved. Thanks a lot!

Please allow me to include some comments for a follow up:

  • Increment version number (it's still on the already released version)
  • For the NuSpec, it is important to include 'Microsoft.Management.Infrastructure' Version='2.0.0' as a dependency. It pulls in the native/runtime dependencies, so it must to be included in the referencing project.
  • One comment which is unrelated to the issue here, but showed up during testing: The RegistryValueDeviceIdComponent works on -windows targets only. The compiler warning reads:
    RegistryValueDeviceIdComponent.cs(46,29): warning CA1416: This call site is reachable on all platforms. 'Registry.GetValue(string, string?, object?)' is only supported on: 'windows'. [.\DeviceId\src\DeviceId\DeviceId.csproj]
    I guess it would be good to wrap that call with 'RuntimeInformation.IsOSPlatform(OSPlatform.Windows)' and return String.Empty (or something) on other platforms.

Regards

@MatthewKing
Copy link
Owner

Thanks for the review, I really appreciate it.

  1. I've incremented the version to 5.3.0.
  2. It's included in the netstandard2.0 and net5.0 targets - net3.5 and net4.0 are still using System.Management.
  3. I've added an extra guard here but the error message is still going to show up until I can use SupportedOSPlatformGuardAttribute.

I'll be releasing v5.3.0 later today. Thanks again for the feedback and assistance.

@DanielMcAssey
Copy link

Unfortunately when using this on .NET 5.0, I am running into the following issue:

System.IO.FileNotFoundException: Could not load file or assembly 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. The system cannot find the file specified.
File name: 'Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'
   at DeviceId.Internal.Wmi.GetSystemDriveSerialNumber()
   at DeviceId.Components.SystemDriveSerialNumberDeviceIdComponent.GetValue()
   at DeviceId.Formatters.HashDeviceIdFormatter.<>c.<GetDeviceId>b__3_1(IDeviceIdComponent x)
   at System.Linq.Enumerable.SelectIPartitionIterator`2.PreallocatingToArray(Int32 count)
   at System.Linq.Enumerable.SelectIPartitionIterator`2.ToArray()
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at DeviceId.Formatters.HashDeviceIdFormatter.GetDeviceId(IEnumerable`1 components)
   at DeviceId.DeviceIdBuilder.ToString()

After publishing

@MatthewKing
Copy link
Owner

Hi Daniel.

I can't reproduce this issue on my PC - everything seems to work fine with .NET 5 on v5.3.0 of DeviceId.

Are you able to put together a small example project that demonstrates that issue?

What platform/runtime are you using?
You don't have a build script that is removing the runtimes directory do you?

Cheers

@profix898
Copy link
Author

@DanielMcAssey:
Looks like the MMI 2.0 package has different native/runtime dependencies for each Windows version (see https://www.nuget.org/packages/Microsoft.Management.Infrastructure.Runtime.Win/2.0.0). So instead of 'win-x64' as your target runtime for publishing, you need to be more specific, such as 'win10-x64' (or 'win8-x64'). Then the correct version will be included when publishing the project. Not sure that's on purpose or simply an oversight on MS's part.

@DanielMcAssey
Copy link

DanielMcAssey commented Jun 27, 2021

Hi there,
Here you go: https://github.com/DanielMcAssey/device-id-repro
If you debug it works fine, if you do a self-contained publish it breaks, I have an example broken self-contained publish.

EDIT: A @profix898 I will give that a go. The only issue is, I think we would need to distribute multiple versions of the same app

For reference: PowerShell/MMI#45

@MatthewKing
Copy link
Owner

Thanks Daniel. Not sure what to do about this one (at least until PowerShell/MMI#45 is resolved)...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants