IdentityModel.OidcClient icon indicating copy to clipboard operation
IdentityModel.OidcClient copied to clipboard

System.Text.Json.JsonSerializer.Serialize - could not load file or assembly System.Text.Encodings.Web, Version=5.0.0.0

Open livicitu opened this issue 3 years ago • 3 comments

I am using IdentityModel.OidcClient 4.0.0 to connect to KeyCloack for OIDC authentication into an application compiled with Visual Studio 2019 and target .NET Framework 4.8. I have added all dependencies for this Nuget package but at runtime I got the following exceptions when trying to connect:

{"Could not load file or assembly 'System.Text.Encodings.Web, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"System.Text.Encodings.Web, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"} System.Exception {System.IO.FileLoadException}

FileName "System.Text.Encodings.Web, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51" string

HResult -2146234304 int

Message "Could not load file or assembly 'System.Text.Encodings.Web, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)" string

Source "System.Text.Json" string

StackTrace " at System.Text.Json.JsonSerializer.Serialize[TValue](TValue& value, Type inputType, JsonSerializerOptions options)\r\n at System.Text.Json.JsonSerializer.Serialize[TValue](TValue value, JsonSerializerOptions options)\r\n at IdentityModel.OidcClient.OidcClient.<EnsureConfigurationAsync>d__16.MoveNext() in //src/OidcClient/OidcClient.cs:line 374\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at IdentityModel.OidcClient.OidcClient.<LoginAsync>d__9.MoveNext() in //src/OidcClient/OidcClient.cs:line 75\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at AuthenticationClient.OIDC.OIDCSession.&lt;LoginAsync&gt;d__43.MoveNext() in C:\\....\\AuthenticationClient\\OIDC\\OIDCSession.cs:line 533\r\n at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()\r\n at AuthenticationClient.OIDC.LoginViewModel.<<InitCommands>b__12_0>d.MoveNext() in C:\...\AuthenticationClient\OIDC\LoginViewModel.cs:line 70" string

? TargetSite {System.String Serialize[TValue](TValue ByRef, System.Type, System.Text.Json.JsonSerializerOptions)} System.Reflection.MethodBase {System.Reflection.RuntimeMethodInfo}

ex.Message "Could not load file or assembly 'System.Text.Encodings.Web, Version=5.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)" string

Seems that at runtime, version 5.0.0 is searched for System.Text.Encodings.Web while at compile time version 5.0.1 is needed. Same for System.Text.Json.

Is there something I am missing? Does OidcClient 4.0.0 work with .NET Framework 4.8?

livicitu avatar May 21 '21 14:05 livicitu

Is there something I am missing? Does OidcClient 4.0.0 work with .NET Framework 4.8?

It should, it is targeting netstandard. I haven't tested it though. I am not using .NET Framework anymore...

leastprivilege avatar May 21 '21 16:05 leastprivilege

The compilation is OK but I have runtime issues as above.

Below the list of packages I have used and their versions:

<packages>
  <package id="IdentityModel" version="5.1.0" targetFramework="net48" />
  <package id="IdentityModel.OidcClient" version="4.0.0" targetFramework="net48" />
  <package id="Microsoft.Bcl.AsyncInterfaces" version="5.0.0" targetFramework="net48" />
  <package id="Microsoft.Extensions.DependencyInjection" version="5.0.0" targetFramework="net48" />
  <package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="5.0.0" targetFramework="net48" />
  <package id="Microsoft.Extensions.Logging" version="5.0.0" targetFramework="net48" />
  <package id="Microsoft.Extensions.Logging.Abstractions" version="5.0.0" targetFramework="net48" />
  <package id="Microsoft.Extensions.Options" version="5.0.0" targetFramework="net48" />
  <package id="Microsoft.Extensions.Primitives" version="5.0.0" targetFramework="net48" />
  <package id="Microsoft.IdentityModel.JsonWebTokens" version="6.11.0" targetFramework="net48" />
  <package id="Microsoft.IdentityModel.Logging" version="6.11.0" targetFramework="net48" />
  <package id="Microsoft.IdentityModel.Tokens" version="6.11.0" targetFramework="net48" />
  <package id="System.Buffers" version="4.5.1" targetFramework="net48" />
  <package id="System.Diagnostics.DiagnosticSource" version="5.0.0" targetFramework="net48" />
  <package id="System.IdentityModel.Tokens.Jwt" version="6.11.0" targetFramework="net48" />
  <package id="System.Memory" version="4.5.4" targetFramework="net48" />
  <package id="System.Numerics.Vectors" version="4.5.0" targetFramework="net48" />
  <package id="System.Reactive" version="5.0.0" targetFramework="net48" />
  <package id="System.Reactive.Linq" version="5.0.0" targetFramework="net48" />
  <package id="System.Runtime.CompilerServices.Unsafe" version="5.0.0" targetFramework="net48" />
  <package id="System.Text.Encodings.Web" version="5.0.1" targetFramework="net48" />
  <package id="System.Text.Json" version="5.0.1" targetFramework="net48" />
  <package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
  <package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
</packages>

I tested also version 3.1.2 and works perfectly fine. But that version is using Newtonsoft.Json not System.Text.Json as version 4.0.0 does.

livicitu avatar May 21 '21 16:05 livicitu

Hi everybody,

Can you please let me know if this worked for you? Is there anything we can do to bypass this issue?

Thank you

livicitu avatar Jun 03 '21 15:06 livicitu

This has been fixed after upgrading IdentityModel.OidcClient to latest version and adding the folowing bindingRedirect for System.Text.Json

		<dependentAssembly>
			<assemblyIdentity name="System.Text.Json" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
			<bindingRedirect oldVersion="0.0.0.0-6.0.0.6" newVersion="6.0.0.0" />
		</dependentAssembly>

Note that the compiler complained about not using newVersion "6.0.0.6" but with that it does not work.

livicitu avatar Sep 15 '22 11:09 livicitu

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue.

github-actions[bot] avatar Sep 30 '22 01:09 github-actions[bot]