Yubico / Yubico.NET.SDK

A YubiKey SDK for .NET developers
Apache License 2.0
96 stars 48 forks source link

Referencing YubiKeyDeviceListener.Instance throws System.TypeInitializationException on all versions after 1.2 #24

Closed mjfara closed 1 year ago

mjfara commented 1 year ago

Any reference to YubiKeyDeviceListener.Instance throws an exception. Version 1.2 is the last working version. .NET Framework 4.8

YubiKeyDeviceListener.Instance.Arrived += YubiKey_Arrived;

System.TypeInitializationException: 'The type initializer for 'PerTypeValues`1' threw an exception.'

FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
GregDomzalski commented 1 year ago

Hi @mjfara,

I tried to create a project to reproduce this, but I was not successful.

Here's what I did:

The app was able to run and I saw the expected arrive and remove event messages.

Would you be able to package up a minimal repro app for me to investigate further?

I also found this StackOverflow thread that seems like it might be relevant? https://stackoverflow.com/questions/62764744/could-not-load-file-or-assembly-system-runtime-compilerservices-unsafe

mjfara commented 1 year ago

When trying to create a minimal repro, I now get the following error when trying to add the 1.4.2 package to an empty class library:

Could not install package 'Yubico.NativeShims 1.3.1'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.8', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Version 1.2 installs without issue.

GregDomzalski commented 1 year ago

Yes, 1.2 is when we added device event support and refactored a lot of our interop code into a custom library. It's unfortunately cause headaches for our .NET Framework 4.x based users. We have yet to discover a good fix for this.

This error is because your new project is using packages.config which is not supported. If the project already has NuGet references, there should be a right-click option on the project or the project references item in solution explorer to "migrate to PackageReferences" or something like that.

Otherwise you can unload the project file and open it in the editor and add:

  <ItemGroup>
    <PackageReference Include="Yubico.YubiKey">
      <Version>1.4.2</Version>
    </PackageReference>
  </ItemGroup>

somewhere before the final import of the project targets (usually the second to last line in the file).

mjfara commented 1 year ago

Thanks for the info. I migrated to PackageReferences but still get the same error, here is a repro:

YubiTest.zip

GregDomzalski commented 1 year ago

Yes I did. I was able to compile and run the application. Unfortunately I still was not able to reproduce this.

I suspect that this may be a machine-specific issue. Are you able to try this out on a second machine?

The SO link I provided may be on to something: perhaps the version of this library registered in the GAC is different from what your current runtime requires. I'm much less familiar with .NET Framework and the GAC - is there a way you can confirm or force the installation of the library version is asking for?

mjfara commented 1 year ago

I'm also beginning to think it's machine specific issue. I'll have to circle back to this issue at a later time and will simply avoid using the listener events for now. Thanks for looking into it.

GregDomzalski commented 1 year ago

Hi @mjfara - any progress on your side? Were you able to get past this issue?

Let me know if I can close this issue.

mjfara commented 1 year ago

Unfortunately had to manually bundle NativeShims.dll in the installer in order to get this to work properly. Can confirm there are no problems on .NET6, but .NET Framework has provided some headaches.