.NET MAUI
Networking & APIs
7 question(s)
How do you call a REST API in MAUI?
Beginner
Use HttpClient (ideally via IHttpClientFactory) to send async requests, and System.Text.Json to deserialize responses. Always await calls off the UI thread and handle non-success status codes. Set base address and default headers once on the client.
var response = await _http.GetAsync("products");
response.EnsureSuccessStatusCode();
var products = await response.Content
.ReadFromJsonAsync<List<Product>>();
Real-world example
A catalog page loads products from a JSON API and binds them to a CollectionView.
Common follow-ups: Why use IHttpClientFactory? | How do you deserialize JSON?
HttpClient
JSON
Networking & APIs
How do you check network connectivity before making a request?
Beginner
Query Connectivity.Current.NetworkAccess (Internet, Local, None) and optionally ConnectionProfiles (WiFi, Cellular). Subscribe to ConnectivityChanged to react to changes. Use it to skip requests, show offline UI, or defer syncing on metered connections.
if (Connectivity.Current.NetworkAccess != NetworkAccess.Internet)
{
await DisplayAlert("Offline", "Check your connection.", "OK");
return;
}
Real-world example
Before syncing large data, an app warns the user and waits when only cellular or no connection is available.
Common follow-ups: What is ConnectivityChanged? | How do you detect metered connections?
Connectivity
Offline
Networking & APIs
How do you handle JSON serialization efficiently in MAUI?
Intermediate
Use System.Text.Json with source-generated serialization (JsonSerializerContext) to avoid reflection, which improves performance and trims better for AOT. Configure options (camelCase, ignore nulls) once and reuse them. Source generation is especially valuable given MAUI's trimming and Native AOT direction.
[JsonSerializable(typeof(List<Product>))]
partial class ApiJsonContext : JsonSerializerContext { }
var products = JsonSerializer.Deserialize(json,
ApiJsonContext.Default.ListProduct);
Real-world example
Switching to source-generated JSON removes reflection warnings under trimming and speeds up parsing large payloads.
Common follow-ups: Why source generation over reflection? | How does it help trimming?
JSON
Performance & Optimization
Networking & APIs
How do you implement resilient HTTP calls (retries, timeouts) in MAUI?
Intermediate
Wrap HttpClient with Polly via IHttpClientFactory's AddPolicyHandler to add retry with backoff, circuit breakers, and timeouts. This handles transient failures common on mobile networks without scattering try/catch and manual retry loops.
builder.Services.AddHttpClient<IApiClient, ApiClient>()
.AddTransientHttpErrorPolicy(p =>
p.WaitAndRetryAsync(3, r => TimeSpan.FromSeconds(Math.Pow(2, r))));
Real-world example
Flaky mobile connections recover automatically as failed requests retry with exponential backoff instead of surfacing errors immediately.
Common follow-ups: What is a circuit breaker? | Where do you register Polly policies?
HttpClient
Resilience
Networking & APIs
How do you keep API calls off the UI thread and update the UI safely?
Intermediate
Await async HttpClient calls—they run I/O off the UI thread already. When you must marshal a result back to the UI from a non-UI context, use MainThread.BeginInvokeOnMainThread. Binding updates from async view-model code are marshaled by MAUI, but explicit UI mutations must be on the main thread.
var data = await _api.GetAsync(); // off UI thread
MainThread.BeginInvokeOnMainThread(() => Items.Add(data));
Real-world example
A background refresh fetches data without freezing the UI, then updates an observable collection on the main thread.
Common follow-ups: When do you need MainThread.BeginInvokeOnMainThread? | Are bindings auto-marshaled?
Threading
Performance & Optimization
Networking & APIs
How do you implement authentication token refresh transparently for API calls?
Advanced
Use a DelegatingHandler in the HttpClient pipeline that attaches the access token, detects 401 responses, refreshes the token (serialized with a lock/semaphore to avoid stampedes), and retries the request. Store tokens in SecureStorage. This centralizes auth so callers stay unaware.
class AuthHandler : DelegatingHandler
{
protected override async Task<HttpResponseMessage> SendAsync(
HttpRequestMessage req, CancellationToken ct)
{
req.Headers.Authorization = new("Bearer", await _tokens.GetAsync());
var res = await base.SendAsync(req, ct);
// on 401: refresh + retry
return res;
}
}
Real-world example
Expired tokens refresh silently mid-session so users are never bounced to the login screen during normal use.
Common follow-ups: How do you prevent concurrent refreshes? | Where do you register the handler?
Authentication
HttpClient
Security
How do you handle real-time updates (WebSockets/SignalR) in a MAUI app?
Advanced
Use the SignalR client (Microsoft.AspNetCore.SignalR.Client) or ClientWebSocket for push updates. Start the connection in a service, subscribe to hub events, and marshal updates to the UI thread. Manage the connection lifecycle with app state—reconnect on resume, stop or background appropriately on sleep.
var hub = new HubConnectionBuilder()
.WithUrl("https://api.example.com/chat").WithAutomaticReconnect().Build();
hub.On<string>("Receive", msg =>
MainThread.BeginInvokeOnMainThread(() => Messages.Add(msg)));
await hub.StartAsync();
Real-world example
A chat app receives messages instantly over SignalR and reconnects automatically after transient drops.
Common follow-ups: How do you tie connections to app lifecycle? | What is WithAutomaticReconnect?
SignalR
Real-time
App Lifecycle