auth0Client.LoginAsync Error - The type initializer for 'System.Text.Json.JsonSerializer' threw an exception


Just trying to integrate Auth0 authentication into also my first Zamarin.Forms mobile app. THe exception is thrown at this line

var auth0LoginResult = await _auth0Client.LoginAsync(new { audience = AuthenticationConfig.Audience });

The new auht0 client instance is created in the as below constructor

private Auth0Client _auth0Client;

        public AuthenticationService()
            _auth0Client = new Auth0Client(new Auth0ClientOptions
                Domain = AuthenticationConfig.Domain,
                ClientId = AuthenticationConfig.ClientId

and the auth0client options are;
public static class AuthenticationConfig
public const string Domain = “”; // Domain from Auth0 portal
public const string ClientId = “”; // ClientId from Auth0 portal
public const string Audience = “”; // Audience from Auth0 portal

The exception as follows;
Error loading discovery document: Error connecting to The type initializer for ‘System.Text.Json.JsonSerializer’ threw an exception…

And its inner exception is as follows;

Method not found: int System.Text.Encodings.Web.TextEncoder.FindFirstCharacterToEncodeUtf8(System.ReadOnlySpan`1)

I have tried installing all the system.text nuget packages including System.Text.Encodings.Web, system.Memory, System.Text.Encodings.Extensions, System.Text.Encoding

Would appreciate if anyone could give me some tips on what to do or look for to debug


I have made some progress and no longer receive this error once I used system.text.encodings.web version prerelease 6.0.0-rc.1.21451.13

I had the same problem and your solution worked. It may have something to do with this warning I get:

Warning NU1605 Detected package downgrade: System.Text.Encodings.Web from 5.0.1 to 4.5.1. Reference the package directly from the project to select a different version.
MyApp.Android → Auth0.OidcClient.Android 3.2.3 → Auth0.OidcClient.Core 3.2.3 → IdentityModel.OidcClient 4.0.0 → IdentityModel 5.1.0 → System.Text.Encodings.Web (>= 5.0.1)
MyApp.Android → System.Text.Encodings.Web (>= 4.5.1) MyApp.Android

However, I cannot seem to get the authentication result from the authenticate call. I hope that’s a configuration error

I am running into this problem now. It seems to sometimes work and most of the time not with iOS devices. Once it deploys to the device and works it will continue to work until I deploy again. I’ve tried updating all the packages above System.Text.Encodings.Web to 6.0, all previous versions as well, I’m tried System.Buffer and System.Memory, and even tried newtonsoft.json. I can’t figure out what allows it to work and what doesn’t, but it seems iOS has issues with System.Text.Json and it can’t always find the method required, but for some reason sometimes it does. I’m using VS 2022 V17.01 with a Xamarin Forms app and I can’t seem to find anything to make this consistent. Very disappointing, but the android works just fine.

Anyone have any tips or help??