I am developing a MAUI application that fetches data from an API (CoinDesk API) to display current Bitcoin price data. The data fetching works fine in Debug mode, but the API call fails to show any data in Release mode. Here is my code for fetching the data:
using Acr.UserDialogs;
using Seraphis.Model;
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;
namespace Seraphis.Api
{
class FetchCoinDeskDataApi
{
private readonly HttpClient _client;
public FetchCoinDeskDataApi()
{
_client = new HttpClient();
}
public async Task<string> FetchCoinDeskDataAsync()
{
string url = "https://api.coindesk.com/v1/bpi/currentprice.json";
try
{
var response = await _client.GetStringAsync(url);
if (string.IsNullOrEmpty(response))
{
Debug.WriteLine("Empty response received.");
UserDialogs.Instance.Toast("No data received from API.");
return null;
}
Debug.WriteLine($"Response: {response}");
return response;
}
catch (HttpRequestException httpEx)
{
Debug.WriteLine($"HTTP Error: {httpEx.Message}");
UserDialogs.Instance.Toast("Failed to fetch data. Please check your internet connection.");
return null;
}
catch (Exception ex)
{
Debug.WriteLine($"Error: {ex.Message}");
UserDialogs.Instance.Toast("An error occurred while fetching the data.");
return null;
}
}
}
}
Steps I have taken so far:
API works fine in Debug mode, but in Release mode it doesn't show the data. Error handling: I added try-catch blocks to log errors in case of failure, but nothing shows up in Release mode. HttpClient initialization: I am reusing the HttpClient across the lifetime of the app. Permissions: I have verified that the necessary internet permissions are declared for both Android (AndroidManifest.xml) and iOS (Info.plist). Logging: I use Debug.WriteLine for logging, but it doesn't show up in Release mode.
Things I suspect:
Linker/Trimming Issue: It’s possible that some parts of the code are being trimmed or removed during Release compilation. Network-related Permissions: There might be issues with network access in Release mode. Timeout/Network Issues: There could be some differences between Debug and Release that cause network requests to fail in Release mode.
What I have tried:
I have confirmed that the API endpoint is accessible, and there are no issues with the API itself. Tried to disable trimming in the .csproj file for debugging purposes (false). I also ensured that internet permissions are set correctly.
What I am looking for:
Why is the data showing up in Debug mode but not in Release mode? How can I fix this issue so that the API call works in Release mode as well? Any advice on how to debug API issues in Release mode or configurations that may need adjustment for Release?
Any help would be greatly appreciated!
x:DataType="SomeType"), as H.A.H. suggests, then you will need to use them all-the-way.