Infra, Ops & WorkflowSûr100100/100

Maui Networking Offline Data (.NET)

Build MAUI networking and offline data. USE FOR: typed `HttpClient`, JSON serialization, Android `10.0.2.2`, iOS simulator localhost, LAN/dev-tunnel fallback, debug cleartext, offline-first screens, SQLite/EF Core sync metadata, queues, encryption decisions, retries, cancellation. DO NOT USE FOR: auth redirects, Aspire service discovery, UI layout.

ou envoie-le directement à ton agent.

Installer dans ton projet

$ curl -fsSL https://arboristhree.xyz/cli/install-skill.mjs -o /tmp/arboris-install-skill.mjs && node /tmp/arboris-install-skill.mjs maui-networking-offline-data__dotnet-maui-labs__plugins-dotnet-maui-skills-90160c8f405c
.NET
dotnet/maui-labs
Contenu à copier
---
name: maui-networking-offline-data
description: >-
  Build MAUI networking and offline data. USE FOR: typed `HttpClient`, JSON serialization, Android `10.0.2.2`, iOS simulator localhost, LAN/dev-tunnel fallback, debug cleartext, offline-first screens, SQLite/EF Core sync metadata, queues, encryption decisions, retries, cancellation. DO NOT USE FOR: auth redirects, Aspire service discovery, UI layout.
metadata:
  origin: .NET
---

# MAUI Networking and Offline Data

Use this skill when a MAUI feature calls backend APIs, works against local
services during development, persists local data, or must behave well offline.

## Workflow

1. Inspect `MauiProgram.cs`, existing API clients, model serialization, and data
   storage packages.
2. Register API clients through DI. Prefer typed clients or named clients over
   constructing `HttpClient` in pages.
3. Define request/response DTOs and serialization options once. Prefer
   `System.Text.Json` source generation for trimmed or NativeAOT-sensitive apps.
4. Decide the local development address by runtime platform and environment.
5. Keep cleartext HTTP exceptions debug-only and platform-scoped.
6. Define offline boundaries before schema/code: name the read-only cached
   reference data, the entities users can edit while offline, and the fields
   that remain server-authoritative.
7. For SQLite/offline sync answers, show the storage initialization API before
   the entity schema: explicitly name `SQLiteAsyncConnection` plus
   `CreateTableAsync<T>` for sqlite-net, or an EF Core `DbContext` setup. Do not
   only show POCO models, sqlite-net attributes, or `db.Table<T>()` queries.
8. In every offline-sync design, include a security line: if cached data is
   sensitive, regulated, or contains customer/business PII, encrypt the local
   SQLite store, for example with SQLCipher/provider encryption, and keep the
   database key in `SecureStorage` rather than source code or `Preferences`.
9. In every offline-sync design, include a background performance line: drain
   queued sync work in bounded batches/chunks off the UI thread, and marshal only
   UI updates through `MainThread` when needed.
10. Store local data in SQLite or app data files behind a repository/service
   abstraction.
11. Add cancellation, timeout, retry, and user-facing error states.

## HttpClient DI Pattern

```csharp
builder.Services.AddHttpClient<IProductsApi, ProductsApi>(client =>
{
    client.BaseAddress = new Uri("https://api.contoso.dev/");
});
```

Keep auth headers in a delegating handler or typed client boundary. Do not add
bearer tokens in every page or ViewModel.

## Local Development Networking

| Runtime | Local backend address |
| --- | --- |
| Android emulator | Use `10.0.2.2` to reach the host machine. |
| iOS simulator | `localhost` usually reaches the Mac host. |
| Windows/Mac Catalyst app | `localhost` reaches the same machine. |
| Physical device | Use a LAN-reachable hostname/IP, reverse tunnel, dev proxy, or deployed endpoint. |

For cleartext HTTP during development:

- Android needs a debug-only network security config such as
  `networkSecurityConfig` or `UsesCleartextTraffic`. Gate it with `#if DEBUG`,
  `IsDevelopment`, or an MSBuild `Condition` on the Debug configuration so it is
  never present in Release builds.
- iOS/Mac Catalyst need an App Transport Security (`NSAppTransportSecurity`)
  exception for debug HTTP, also guarded by a Debug-only condition.
- Release builds should use HTTPS and remove broad cleartext exceptions.

## Connectivity Check

Use MAUI connectivity APIs to decide when to show offline UI and when to drain
sync queues:

```csharp
public sealed class SyncService(IConnectivity connectivity)
{
    public bool IsOnline => connectivity.NetworkAccess == NetworkAccess.Internet;
}
```

Subscribe to `ConnectivityChanged` from a long-lived service when the app should
resume sync on reconnect, and unsubscribe when the owner is disposed. Only
`NetworkAccess.Internet` indicates a routable connection. In migrated apps,
replace `Xamarin.Essentials.Connectivity` with `Microsoft.Maui.Networking`
APIs or injected `IConnectivity`.

## JSON Guidance

- Use one `JsonSerializerOptions` instance for casing, enum conversion, and null
  handling.
- Prefer source-generated `JsonSerializerContext` when the app is trimmed,
  NativeAOT-sensitive, or serializes many known DTOs.
- Separate DTOs from database entities when local schema and API shape evolve at
  different speeds.

## SQLite and Offline Sync

Use SQLite for structured local state, offline queues, and cached server data.
Start offline-sync guidance with an explicit boundary list or table so the
answer does not imply every local row is editable. For example:

| Boundary | Examples | Sync behavior |
|----------|----------|---------------|
| Read-only cached reference data | product catalog, feature flags, lookup values | pull/refresh from server; do not enqueue local edits |
| Offline-editable user data | draft orders, notes, inspection forms | local writes get dirty state and outbox entries |
| Server-authoritative data | payment status, inventory counts, account roles | display cached values but require online server confirmation before mutation |

For sqlite-net, this usually means a `SQLiteAsyncConnection` and
`CreateTableAsync<T>` setup before any `db.Table<T>()` queries. For EF Core, keep
the local schema behind a `DbContext`.

Always include a short initialization snippet in offline-sync guidance so the
storage layer is concrete, not just the row shape or sqlite-net attributes.
Immediately after the boundary table or storage initialization, include a
one-sentence security decision for sensitive offline data, naming encryption
such as SQLCipher and a `SecureStorage`-protected key when applicable.
Also include a one-sentence sync performance decision using the words
`background` and `batch` or `chunk`, for example: "Drain the outbox on a
background worker in small batches/chunks and use `MainThread` only to update
progress UI."

```csharp
var db = new SQLiteAsyncConnection(databasePath);
await db.CreateTableAsync<Order>();
await db.CreateTableAsync<SyncOperation>();
```

Common fields for syncable rows:

- Server ID and local ID.
- `UpdatedAt`, `ETag`, row version, or another concurrency token.
- Dirty state such as `PendingCreate`, `PendingUpdate`, or `PendingDelete`.
- Tombstone/deleted marker when deletes must sync.

Keep sync boundaries explicit:

- Cache read-only reference data separately from editable offline data and say
  which local tables are never enqueued for upload.
- Retry idempotent operations automatically; ask the user before replaying
  non-idempotent actions.
- Resolve conflicts with a documented policy: server wins, client wins, field
  merge, or user decision.
- Persist queued operations before sending them so app termination does not lose
  offline edits.
- Encrypt the local SQLite database when it stores sensitive business data.
  Options include SQLCipher-based packages or provider-specific SQLite
  encryption. Store the database key in `SecureStorage`, not in source code or
  `Preferences`.
- Page or chunk large sync operations and write in bounded batches so the app
  stays responsive and avoids mobile memory spikes.
- Drain sync queues on background work, not the UI thread; marshal only progress
  or completion updates back with `MainThread.BeginInvokeOnMainThread` when UI
  needs to change.

## Retry and Error Handling

- Pass `CancellationToken` from UI commands and lifecycle shutdown paths.
- Use short connection timeouts and clear user messages for no network,
  unauthorized, validation, and server errors.
- Retry only transient failures such as timeouts and 5xx responses.
- Use exponential backoff with jitter for background sync.
- Do not silently swallow sync failures; surface pending state and retry status.

## Validation Checklist

- API clients are registered in DI and are not created directly in UI code.
- Local dev base addresses are platform-aware.
- Cleartext HTTP exceptions are debug-only.
- SQLite state has an explicit sync/conflict boundary and names the concrete
  storage API, such as `SQLiteAsyncConnection` with `CreateTableAsync<T>` or an
  EF Core `DbContext`.
- Sensitive local data has an encryption decision, such as SQLCipher plus a key
  stored in `SecureStorage`.
- Requests support cancellation and distinguish transient from permanent errors.
Colle ce Markdown dans ton agent ou utilise les boutons ci-dessus pour l’écrire dans ton projet.

Ce que fait Maui Networking Offline Data (.NET)

Android needs a debug-only network security config such as
iOS/Mac Catalyst need an App Transport Security (`NSAppTransportSecurity`)
Release builds should use HTTPS and remove broad cleartext exceptions.
Use one `JsonSerializerOptions` instance for casing, enum conversion, and null

Comment utiliser Maui Networking Offline Data (.NET)

1
Copie le prompt
Un clic copie le prompt packagé (ou l'envoie à ton agent).
2
L'agent installe le skill
Il ajoute le SKILL.md et ses ressources à ton projet.
3
Activation automatique
Le skill s'active dès que le contexte correspond.

Déclencheurs pour Maui Networking Offline Data (.NET)

Dis simplement à ton agent quelque chose comme :

Applique le skill maui-networking-offline-data__dotnet-maui-labs__plugins-dotnet-maui-skills-90160c8f405c à cette tâche
Utilise maui-networking-offline-data__dotnet-maui-labs__plugins-dotnet-maui-skills-90160c8f405c pour améliorer cette implémentation
Passe en revue ce sujet avec maui-networking-offline-data__dotnet-maui-labs__plugins-dotnet-maui-skills-90160c8f405c

Skills liés à Maui Networking Offline Data (.NET)

Add CLI Command (.NET)
Add or change a dotnet CLI command, subcommand, or option across the relevant CLI projects. USE FOR: adding or changing a dotnet CLI command/subcommand, adding a new Option<T> or Argument<T>, registering a subcommand in the command tree, changing an option's help description or a command's runtime message, wiring a command parser, or updating --help output.
Add Dotnet Aot Command (.NET)
Add, enable, or review a dotnet CLI command or feature in the Native AOT CLI (src/Cli/dotnet-aot) and prove its compatibility. USE FOR: migrating a command or option to AOT, reviewing an AOT migration PR, defining conservative eligibility and managed fallback, changing AotSourceFiles.props or AotDependencies.props, validating AOT/managed parity, NativeAOT-publishing tests, checking binary-size impact, or using the dn harness and separated SDK layout. DO NOT USE FOR: resolving IL trim/AOT analyzer warnings alone (use dotnet-aot-compat), running dotnet.Tests incrementally (use incremental-test), or pure managed CLI work.
Add Net Analyzer (.NET)
Add, port, or change a .NET code analysis rule (CA####) under src/Microsoft.CodeAnalysis.NetAnalyzers. USE FOR: implementing a new CA analyzer and its code fixer, porting a rule or PR from the retired dotnet/roslyn-analyzers repo, allocating a diagnostic ID from DiagnosticCategoryAndIdRanges.txt, choosing RuleLevel/severity/category, wiring resx + xlf strings, recording the rule in AnalyzerReleases.Unshipped.md, regenerating the analyzer documentation/sarif files, and writing MSTest analyzer/code-fix tests with the VerifyCS/VerifyVB harness. DO NOT USE FOR: NETSDK#### MSBuild diagnostics (src/Tasks), CS####/BC#### compiler diagnostics or IDE#### analyzers (dotnet/roslyn), or CONTAINER#### diagnostics (src/Containers).