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

Error: Could not load file or assembly Microsoft.Management.Infrastructure #36

Closed fairking closed 2 years ago

fairking commented 2 years ago

DeviceId: 5.3.0 Running the app on windows 10 I have the following error:

 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.GetValues(String className, String propertyName)
   at DeviceId.Components.WmiDeviceIdComponent.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()

My code looks like this:

var systemId = new DeviceIdBuilder()
                .AddProcessorId()
                .AddMotherboardSerialNumber()
                .AddSystemUUID()
                .AddOSInstallationID()
                .UseFormatter(new HashDeviceIdFormatter(() => MD5.Create(), new Base64UrlByteArrayEncoder()))
                .ToString();

Is it a bug or am I doing something wrong? Appreciate any help.

MatthewKing commented 2 years ago

This is an issue with the Microsoft.Management.Infrastructure package.

It only ships with the following runtimes: image

If you're using a different runtime it won't work.

Your options are:

fairking commented 2 years ago

I don't understand. I would rather write my own code then. Without any dependency. Just based on System.Management. This library is not compatible with windows as I understand.

MatthewKing commented 2 years ago

It is compatible with Windows.

DeviceId.Windows.Wmi uses System.Management. This is the one that the readme recommends that you use. DeviceId.Windows.Mmi uses MMI, but it's not recommended (for the reasons you've seen above) unless System.Management is not available for your use case. If you're able to use System.Management then just use DeviceId.Windows.Wmi.

fairking commented 2 years ago

Ok, I switched to v6.0 and the following example is working on Windows (runtime win-x64) 👍

            string systemId = new DeviceIdBuilder()
                .AddMacAddress()
                .AddOsVersion()
                .OnWindows(windows => windows
                    .AddProcessorId()
                    .AddMotherboardSerialNumber()
                    .AddSystemUuid())
                .OnLinux(linux => linux
                    .AddCpuInfo()
                    .AddMotherboardSerialNumber()
                    .AddProductUuid())
                .UseFormatter(new HashDeviceIdFormatter(() => MD5.Create(), new Base64UrlByteArrayEncoder()))
                .ToString();

Thanks a lot for your help. 🥇

MatthewKing commented 2 years ago

Glad you got it going!