Configs2026年5月23日·1 分钟阅读

Refit — Type-Safe REST Client for .NET

A .NET library that turns REST API definitions into live, type-safe interfaces using source generation, eliminating manual HttpClient boilerplate.

Agent 就绪

这个资产可以被 Agent 直接读取和安装

TokRepo 同时提供通用 CLI 命令、安装契约、metadata JSON、按适配器生成的安装计划和原始内容链接,方便 Agent 判断适配度、风险和下一步动作。

Stage only · 29/100Stage only
Agent 入口
任意 MCP/CLI Agent
类型
CLI Tool
安装
Single
信任
信任等级:Established
入口
Refit Overview
通用 CLI 安装命令
npx tokrepo install 013640ba-56c5-11f1-9bc6-00163e2b0d79

Introduction

Refit is a type-safe REST client library for .NET inspired by Retrofit for Android. You define an interface describing your API endpoints, and Refit generates the implementation at compile time using source generators, turning HTTP calls into simple method invocations.

What Refit Does

  • Generates HTTP client implementations from annotated C# interfaces
  • Maps method parameters to URL segments, query strings, headers, and body content
  • Serializes request bodies and deserializes responses automatically
  • Integrates with HttpClientFactory for connection pooling and lifecycle management
  • Supports multipart uploads, streaming, and generic API response wrappers

Architecture Overview

Refit uses C# source generators to analyze interface definitions at compile time and emit concrete HttpClient-based implementations. Each method attribute ([Get], [Post], etc.) maps to an HTTP verb, and parameter attributes control where values are placed in the request. At runtime, the generated code uses HttpClient directly, avoiding reflection overhead. Serialization defaults to System.Text.Json with Newtonsoft.Json available as an option.

Self-Hosting & Configuration

  • Install via NuGet: dotnet add package Refit.HttpClientFactory
  • Define API interfaces with HTTP method attributes and route templates
  • Register with DI: builder.Services.AddRefitClient<IMyApi>()
  • Configure base address, default headers, and timeout via HttpClient options
  • Compatible with .NET 6 through .NET 9

Key Features

  • Compile-time source generation with no runtime reflection
  • Automatic parameter serialization for URL, query, header, and body
  • Support for ApiResponse wrapper with status code and headers access
  • Multipart file upload via StreamPart and ByteArrayPart
  • Dynamic header injection through [HeaderCollection] and [Authorize]

Comparison with Similar Tools

  • RestSharp — fluent HTTP client; more manual, no interface-based generation
  • HttpClient (raw) — built-in .NET HTTP; full control but verbose boilerplate
  • Flurl — fluent URL and HTTP builder; convenient but no compile-time type safety
  • RestEase — similar interface-driven approach; uses runtime proxy generation instead of source gen
  • NSwag/OpenAPI Generator — generates clients from OpenAPI specs; different workflow, spec-first

FAQ

Q: Does Refit use reflection at runtime? A: No. Since version 6, Refit uses compile-time source generators to create implementations, avoiding reflection entirely.

Q: Can I add authentication headers to all requests? A: Yes. Use the [Headers] attribute on the interface for static headers, or configure a DelegatingHandler that adds bearer tokens dynamically via HttpClientFactory.

Q: How do I handle error responses? A: Return ApiResponse instead of T. This wrapper includes the HTTP status code, headers, and error content, allowing you to handle non-success responses without exceptions.

Q: Can I use Refit with Polly for resilience? A: Yes. When registering the Refit client via AddRefitClient, chain AddStandardResilienceHandler() from Microsoft.Extensions.Http.Resilience to add retry and circuit breaker policies.

Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产