IdentityModel breaks with "Could not load file or assembly 'System.Numerics.Vectors, Version=4.1.4.0"

1.1k Views Asked by At

We have a .NET Framework 4.7.1 project

I installed IdentityModel 6.0.0 with the NuGet Package Manager. It installed the following dependencies for me:

Microsoft.Bcl.AsyncInterfaces 6.0.0
System.Buffers 4.5.1
System.Memory 4.5.4
System.Numerics.Vectors 4.5.0
System.Text.Json 6.0.3
System.ValueTuple 4.5.0

I'm trying to run the following code to see if a service exists:

var disco = await clientAuth.GetDiscoveryDocumentAsync(new DiscoveryDocumentRequest
{
    Address = server,
    Policy =
    {
        ValidateIssuerName = false
    }
});
if (disco.IsError) {...}

But, it keeps generating an error. I managed to drill down into the code, and finally came upon this: var result = System.Text.Json.JsonDocument.Parse("{"issuer":"https:....}").RootElement;

The error I get on this is:

Could not load file or assembly 'System.Numerics.Vectors, Version=4.1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.

at System.Text.Json.JsonReaderHelper.IndexOfOrLessThan(Byte& searchSpace, Byte value0, Byte value1, Byte lessThan, Int32 length) in /_/src/libraries/System.Text.Json/src/System/Text/Json/Reader/JsonReaderHelper.cs:line 208

at System.Text.Json.Utf8JsonReader.ConsumeString() in /_/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs:line 1276

at System.Text.Json.Utf8JsonReader.ConsumePropertyName() in /_/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs:line 1227

at System.Text.Json.Utf8JsonReader.ReadSingleSegment() in /_/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs:line 852

at System.Text.Json.Utf8JsonReader.Read() in /_/src/libraries/System.Text.Json/src/System/Text/Json/Reader/Utf8JsonReader.cs:line 272

at System.Text.Json.JsonDocument.Parse(ReadOnlySpan`1 utf8JsonSpan, JsonReaderOptions readerOptions, MetadataDb& database, StackRowStack& stack) in /_/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.cs:line 1092

at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 utf8Json, JsonReaderOptions readerOptions, Byte[] extraRentedArrayPoolBytes, PooledByteBufferWriter extraPooledByteBufferWriter) in /_/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs:line 697

at System.Text.Json.JsonDocument.Parse(ReadOnlyMemory`1 json, JsonDocumentOptions options) in /_/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs:line 271

at System.Text.Json.JsonDocument.Parse(String json, JsonDocumentOptions options) in /_/src/libraries/System.Text.Json/src/System/Text/Json/Document/JsonDocument.Parse.cs:line 316

at Citadel.Framework.APICalls.APIBase.<Setup>d__3.MoveNext() in C:\Dev\Src\Tyrus\Production\Tyrus_5_1\Citadel.Framework.APICalls\APIBase.cs:line 86

I managed to drill down to the ConsumeString method, but couldn't learn anything.

The most annoying of all is I can't load version 4.1.4 for System.Numerics.Vectors

In the Nuget Manager, only the following versions are available:

4.5.0 - current
4.4.0
4.3.0
4.1.1
4.1.0
4.0.0

I'm getting preciously little on the internet regarding this specific issue, and I've tried everything I could find without any luck. I've tried clearing my nuget cache. I can't remember how many times I've removed and readded the dependencies, I've cleaned my solution and rebuild it. I tried a number of combinations of:

<dependentAssembly>
    <assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.1.4.0" />
</dependentAssembly>
2

There are 2 best solutions below

0
On BEST ANSWER

Turns out that adding the following line in your root project file will magically make all the problems go away:

<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>

Odd that I didn't find any article suggesting this.

0
On

If you install v4.5.0 of System.Numerics.Vectors to your GAC, it automatically installs into the path for v4.1.4 in the assembly cache. If you have a project where System.Numerics.Vectors v4.5.0 is installed, navigate to the net46 path for the DLL and from elevated Visual Studio Developer Command Prompt, type gacutil -i System.Numerics.Vectors.dll.