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

Incompatible when used with .NET 5 in SingleFile option #32

Closed ngoquoctoandev closed 2 years ago

ngoquoctoandev commented 3 years ago

I have a sample WPF project running on .NET 5. Everything works fine for both debug and release environments. However, if I publish the project with target runtime as win-x64 or win-x86 with "Produce Single File" option, the following error occurs.

image

image

This error is logged with Serilog

MatthewKing commented 3 years ago

Hello. This is due to https://github.com/PowerShell/MMI/issues/45 - can you target win10-x64 instead? Or one of the other more specific runtimes.

ngoquoctoandev commented 3 years ago

There doesn't seem to be a suitable alternative. And no specific operating system appears in the target runtime list. Of course, if I choose Portable, there is no "Single File" option.

image

DaniusLabs commented 3 years ago

same issue when targeting win-x64. Does not even need to be single file.

ngoquoctoandev commented 3 years ago

same issue when targeting win-x64. Does not even need to be single file.

Confirmed.

mj2015 commented 3 years ago

Just to say I too have fallen over this. I don't understand the technicalities, but it looks like a long chain of dependency that would be good to cut out the middle man and go directly to the WMI interface. I'll resort to storing a GUID in a file the \Windows directory until then...

MatthewKing commented 3 years ago

This is obviously catching a lot of people out.

For anyone seeing this issue and not wanting to change their runtime identifier, I would recommend just using v5.2.0 for now - it still uses WMI and thus doesn't have any of the issues with the MMI package.

Moving forwards, I think the best option will be to provide both a WMI and a MMI package, so that people can choose which set of tradeoffs they want to make. I'll do this in the next release.

mj2015 commented 3 years ago

Ahah! It does indeed work fine with 5.2. Thank you for correcting me.

MatthewKing commented 2 years ago

I'm working on a v6 release that will break out the various platform-specific stuff into its own packages. It will allow you to choose between using WMI and MMI on Windows.

For the Windows stuff, you'll have a choice of:

I'm going to go back to recommending the WMI stuff as a default, as the runtime issues with MMI are catching too many users out.

Simple example using the new syntax:

var deviceId = new DeviceIdBuilder()
    .AddMachineName()
    .AddUserName()
    .OnWindows(windows => windows
        .AddProcessorId()
        .AddSystemSerialDriveNumber())
    .OnLinux(linux => linux
        .AddCpuInfo()
        .AddSystemDriveSerialNumber())
    .ToString();

v6 is currently available as an alpha. Alternatively, users who want to stay on v5 can use v5.2.0 for WMI or v5.3.0 for MMI.

ngoquoctoandev commented 2 years ago

This error is not really fixed even though I am using the latest version of DeviceId 6.2.0

MatthewKing commented 2 years ago

It actually is fixed but I can help you out. Are you using WMI or MMI?

MatthewKing commented 2 years ago

The following minimal reproduction confirms that it does work as described above:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <PublishSingleFile>true</PublishSingleFile>
    <RuntimeIdentifier>win10-x64</RuntimeIdentifier>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="DeviceId.Windows.Mmi" Version="6.2.0" />
  </ItemGroup>

</Project>
using System;
using DeviceId;

var deviceId = new DeviceIdBuilder()
    .AddMachineName()
    .AddUserName()
    .OnWindows(windows => windows
        .AddProcessorId()
        .AddSystemDriveSerialNumber())
    .ToString();

Console.WriteLine(deviceId);
ngoquoctoandev commented 2 years ago

DeviceId.Windows.Mmi This error occurs when I publish the application as SingleFile

ngoquoctoandev commented 2 years ago

My application runs on two operating systems Windows 7/10

MatthewKing commented 2 years ago

Please refer to the discussion I mentioned earlier: https://github.com/PowerShell/MMI/issues/45 Or use DeviceId.Windows.Wmi

MatthewKing commented 2 years ago

Here is a simple reproduction that confirms you can publish single file on win-x64 using DeviceId.Windows.Wmi:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <PublishSingleFile>true</PublishSingleFile>
    <IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="DeviceId.Windows.Wmi" Version="6.2.0" />
  </ItemGroup>

</Project>
using System;
using DeviceId;

var deviceId = new DeviceIdBuilder()
    .AddMachineName()
    .AddUserName()
    .OnWindows(windows => windows
        .AddProcessorId()
        .AddSystemDriveSerialNumber())
    .ToString();

Console.WriteLine(deviceId);
ngoquoctoandev commented 2 years ago

Why can't be compatible with MMI

MatthewKing commented 2 years ago

I've linked to the discussion the MMI repo twice now. Please go read it. Here it is again: https://github.com/PowerShell/MMI/issues/45 You need to include your specific runtime identifiers (example: win7-x64, win10-x64) rather than just using win-x64. This is just how MMI works. It has nothing to do with the DeviceId library and it's nothing that I can change.