Refit Fails to Deserialize When Publishing .NET MAUI App to Android

70 Views Asked by At

I'm developing a .NET MAUI app that calls a web API using Refit. The app works as expected when running in an emulator or during debugging on my Samsung Galaxy S21. However, when I publish the app as an APK bundle and install it on the same phone, the API calls result in unexpected behavior.

Here's my Refit interface (IApiClient):

public interface IApiClient
{
    [Get("/api/v1/team")]
    public Task<ApiResponse<Team>> GetTeam([Query] int id);
}

This is the dependency injection:

    MauiAppBuilder builder = MauiApp.CreateBuilder();
    string apiUrl = "https://exsampel.com/";
    builder.Services.AddRefitClient<IApiClient>().ConfigureHttpClient(c => c.BaseAddress = new(apiUrl));

The problem arises with the following usage:

ApiResponse<Team> teamResponse = await _ApiClient.GetTeam(id);

In this scenario, teamResponse.Content is null when running the published app. Strangely, if I modify the Refit interface to return a string instead of Team, everything works as fine:

public interface IApiClient
{
    [Get("/api/v1/team")]
    public Task<ApiResponse<string>> GetTeam([Query] int id);
}

the Team-class

public class Team
{
    public int Id { get; set; }
    public Guid UserId { get; set; }
    public string DogsName { get; set; }
    public DateTime Created { get; set; }
}

Despite these efforts, the issue persists only when running the published app. Here are additional details:

  • No exceptions are thrown.
  • The HTTP response status code is 200OK.
  • Running on Android version 14 on Samsung Galaxy S21.

I look forward to read your answers.

0

There are 0 best solutions below