MatthewKing / DeviceId

A simple library providing functionality to generate a 'device ID' that can be used to uniquely identify a computer.
MIT License
729 stars 118 forks source link

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

Closed profix898 closed 3 years ago

profix898 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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:

Regards

MatthewKing commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

@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 commented 3 years ago

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: https://github.com/PowerShell/MMI/issues/45

MatthewKing commented 3 years ago

Thanks Daniel. Not sure what to do about this one (at least until https://github.com/PowerShell/MMI/issues/45 is resolved)...