MatthewKing / DeviceId

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

JsonException when generating Id on Alpine Linux #63

Open InFarAday opened 1 year ago

InFarAday commented 1 year ago

Hello, I am facing an issue generating a device id that happens only on Alpine Linux. The specific context is a docker container using the official dotnet/sdk:7.0-alpine image. On Windows and on my Ubuntu container everything runs fine.

The code used to generate the machine ID is this one:

new DeviceIdBuilder()
        .AddMacAddress(true)
        .OnWindows(windows => windows
            .AddProcessorId()
            .AddMotherboardSerialNumber()
            .AddSystemDriveSerialNumber())
        .OnLinux(linux => linux
            .AddCpuInfo()
            .AddMotherboardSerialNumber()
            .AddSystemDriveSerialNumber())
        .ToString()

And the error I'm getting is this one:

System.TypeInitializationException : The type initializer for 'XXX.Common.Utilities.Security.Device' threw an exception.
---- System.Text.Json.JsonException : The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. Path: $ | LineNumber: 0 | BytePositionInLine: 0.
-------- System.Text.Json.JsonReaderException : The input does not contain any JSON tokens. Expected the input to start with a valid JSON token, when isFinalBlock is true. LineNumber: 0 | BytePositionInLine: 0.
  Stack Trace:
     at XXX.Common.Utilities.Security.Device.get_Id() in /builds/XXX/common/Utilities/Security/Device.cs:line 17
   at XXX.Common.Test.Utilities.Security.DeviceTests.Id() in /builds/XXX/common/Test/Utilities/Security/Device.cs:line 20
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)
----- Inner Stack Trace -----
   at System.Text.Json.ThrowHelper.ReThrowWithPath(ReadStack& state, JsonReaderException ex)
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 utf8Json, JsonTypeInfo jsonTypeInfo, Nullable`1 actualByteCount)
   at System.Text.Json.JsonSerializer.ReadFromSpan[TValue](ReadOnlySpan`1 json, JsonTypeInfo jsonTypeInfo)
   at DeviceId.Linux.Components.LinuxRootDriveSerialNumberDeviceIdComponent.GetValue()
   at DeviceId.Formatters.HashDeviceIdFormatter.<>c.<GetDeviceId>b__4_1(KeyValuePair`2 x)
   at System.Linq.Enumerable.SelectIPartitionIterator`2.PreallocatingToArray(Int32 count)
   at System.Linq.Enumerable.SelectIPartitionIterator`2.ToArray()
   at DeviceId.Formatters.HashDeviceIdFormatter.GetDeviceId(IDictionary`2 components)
   at DeviceId.DeviceIdBuilder.ToString()
   at XXX.Common.Utilities.Security.Device..cctor() in /builds/XXX/common/Utilities/Security/Device.cs:line 17
----- Inner Stack Trace -----
   at System.Text.Json.ThrowHelper.ThrowJsonReaderException(Utf8JsonReader& json, ExceptionResource resource, Byte nextByte, ReadOnlySpan`1 bytes)
   at System.Text.Json.Utf8JsonReader.Read()
   at System.Text.Json.Serialization.JsonConverter`1.ReadCore(Utf8JsonReader& reader, JsonSerializerOptions options, ReadStack& state)

I would appreciate any feedback even if it's that alpine linux is not supported. Thanks.

MatthewKing commented 1 year ago

The Linux functionality is driven by community contribution, as there are too many different flavours of Linux for me to deal with.

If this doesn't currently work then I will need a PR from one of the Linux community members to resolve it.