$ show reactiveui/refit
Refit is a .NET library that automatically generates type-safe HTTP API clients from C# interfaces decorated with attributes like [Get], [Post], [Header], [Body], and [Query]. It operates via two parallel request-building paths: a Roslyn source generator that emits inline request construction at compile time (trim/AOT-safe), and a reflection-based fallback for unsupported shapes. The library integrates deeply with Microsoft.Extensions.DependencyInjection and IHttpClientFactory, providing extension methods like AddRefitClient<T> and AddKeyedRefitClient for managed HTTP client lifecycles. It supports multiple serializers (System.Text.Json, Newtonsoft.Json, XML), content compression (gzip, Brotli, Zstandard), streaming responses (IAsyncEnumerable, JSON Lines, SSE), multipart form data, and per-request timeouts and authorization via DelegatingHandler pipelines. The architecture is modular: the core Refit package provides attributes, serialization interfaces, and the runtime GeneratedRequestRunner; Refit.HttpClientFactory adds DI integration; Refit.Newtonsoft.Json and Refit.Xml provide alternative serializers; Refit.Reflection offers the runtime request builder; and Refit.Testing supplies StubHttp and Reply for unit testing without a live server. The source generator (InterfaceStubGeneratorV2) is an incremental generator that parses interfaces and emits partial classes into the consumer assembly, using value-equatable models (ImmutableEquatableArray) for caching. The reflection path caches parsed method metadata and builds requests via delegates, ensuring parity with generated code. Key entry points include RestService.For<T>() for creating clients, AddRefitClient<T>() for DI registration, and RefitSettings for central configuration. External dependencies are minimal: System.Text.Json (built-in), Microsoft.Extensions.Http, and optionally Newtonsoft.Json or System.Xml. The library is extensively tested with unit tests, integration tests, snapshot-based generator tests, and benchmarks measuring allocation and throughput. It targets .NET 8/9/10/11 and .NET Framework 4.6.2+, with polyfills for missing BCL types.
The LLM identified 4 files worth highlighting for reuse:
A struct implementing IImmutableEquatableArrayEnumerable<T> that provides sequence equality for arrays used in Roslyn incremental generator caching. It wraps a T[] and overrides Equals/GetHashCode using structural comparison, enabling the generator to detect unchanged inputs and avoid re-emitting code. This pattern is reusable in any incremental generator that needs to compare model collections without boxing or allocation.
A ref struct that builds strings using a stack-allocated buffer (Span<char>) and falls back to ArrayPool<char>.Shared when the buffer is exceeded. It provides Append, AppendSpanFormattable, and ToString methods, and is used extensively in path/query construction to avoid heap allocations. This is a high-performance pattern for any .NET code that needs to build strings in hot paths without allocating.
A streaming JSON deserializer that reads Utf8JsonReader from a stream and yields deserialized objects as IAsyncEnumerable<T>. It supports three modes: JSON arrays, newline-delimited JSON (JSON Lines), and Server-Sent Events (SSE). This is a reusable component for any .NET application that needs to process large JSON payloads incrementally without buffering the entire response.
A DelegatingHandler that creates a new IServiceScope per request to resolve an authorization token provider (IAuthorizationHeaderValueProvider). This prevents scope bleeding across pooled HttpClient handlers and ensures per-request token resolution. The pattern is reusable for any HTTP client that needs scoped dependencies (e.g., per-request user context) in an IHttpClientFactory pipeline.