1

I am using Refit 6.1.15 in a Xamarin forms project. My code works exactly as expected on Android, but on iOS, I get a "The type initializer for 'System.Text.Json.JsonSerializer' threw an exception." when I execute my api.

I am using Microsoft.Extensions for DI - my configuration of my service in my Startup.cs class looks like this:

    services.AddRefitClient<IAuthorizeApi>()
            .ConfigureHttpClient(c => c.BaseAddress = new Uri(BaseAddress))
            .AddTransientHttpErrorPolicy(builder => builder.WaitAndRetryAsync(new[]
            {
                TimeSpan.FromSeconds(1),
                TimeSpan.FromSeconds(5),
                TimeSpan.FromSeconds(15)
            }));

My IAuthorizeAPI looks like this:

using System;
using System.Threading.Tasks;
using CivicMobile.Models;
namespace CivicMobile.Services
{
public interface IAuthorizeApi

{
    [Post("/v1/DataStage/UserAuthentication/Authenticate?companyCode={queryParms.companyCode}&username={queryParms.username}&password={queryParms.password}&deviceIdentifier={queryParms.deviceIdentifier}")]
    [QueryUriFormat(UriFormat.Unescaped)]
    
    Task<ApiResponse<AuthResponse>> Login(Authorize queryParms);
}

My call that throws the error (in my ViewModel) is:

    var authToken = await _authenticateService.Login(queryParms);

The return value for the Login (wrapped in ApiResponse) looks like this:

namespace CivicMobile.Models
{
public class AuthResponse
{


    [AliasAs("access_token")]
    public string AccessToken { get; set; }

    [AliasAs("token_type")]
    public string TokenType { get; set; }

    [AliasAs("expires_in")]
    public int ExpiresIn { get; set; }

    [AliasAs("userName")]
    public string Username { get; set; }

    [AliasAs("userIdentifier")]
    public string UserIdentifier { get; set; }

    [AliasAs(".issued")]
    public string IssuedAt { get; set; }

    [AliasAs(".expires")]
    public string ExpiresAt { get; set; }
}

I have replaced [AliasAs()] with [JsonPropertyName()] but the results are the same.

This error ONLY occurs on iOS - not on Android. Any suggestions?

2 Answers2

1

Add the following code in your iOS(.csproj ):

<ItemGroup>
    <PackageReference Include="System.Memory" Version="4.5.4">
        <IncludeAssets>none</IncludeAssets>
    </PackageReference>
    <PackageReference Include="System.Buffers" Version="4.5.1">
    < IncludeAssets>none</IncludeAssets>
    </PackageReference>
</ItemGroup>
Wen xu Li
  • 1,698
  • 1
  • 4
  • 7
0

I took refit out of my DI Container and the problem went away entirely. No other changes in my code at all. I will try another DI system as I prefer to use DI in this app and refit.

Another update - I had AddHttpClient as well as the AddRefitClient in my ConfigureServices method. It was actually dead code (as I migrated to Refit but never got rid of dead code). That caused my POST to return a proper ApiResponse object with Content that was deserialized properly. So back to what I had planned in the beginning - thanks for your suggestion - it was helpful on another issue (a very large dataset returning - different Api).