From 3c935adee1c3c1d36100c69e64492ee4007524bc Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Mon, 24 Aug 2026 01:51:08 +0200 Subject: [PATCH] Refit: retarget net10.0, Refit 15.2.0, AddRefitGeneratedClient, error-handling and headers samples - RefitLibrary and Tests retargeted net6.0 -> net10.0 - Refit and Refit.HttpClientFactory 6.3.2 -> 15.2.0 - Microsoft.Extensions.Hosting 6.0.1 -> 10.0.0 - Microsoft.AspNetCore.Mvc.Testing 6.0.5 -> 10.0.0 - Microsoft.NET.Test.Sdk 16.11.0 -> 17.11.1 - Program.cs registers with AddRefitGeneratedClient(); on Refit 15.x AddRefitClient() throws NotSupportedException without the opt-in Refit.Reflection package - IUsersClient gains an ApiResponse endpoint, a query-string plus CancellationToken overload, and an interface-level [Headers] attribute; Program.cs shows the ApiResponse call site --- .../Refit/RefitLibrary/IUsersClient.cs | 7 +++++++ dotnet-client-libraries/Refit/RefitLibrary/Program.cs | 7 ++++++- .../Refit/RefitLibrary/RefitLibrary.csproj | 8 ++++---- dotnet-client-libraries/Refit/Tests/Tests.csproj | 10 +++++----- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/dotnet-client-libraries/Refit/RefitLibrary/IUsersClient.cs b/dotnet-client-libraries/Refit/RefitLibrary/IUsersClient.cs index 26b1f04a55..331a84f765 100644 --- a/dotnet-client-libraries/Refit/RefitLibrary/IUsersClient.cs +++ b/dotnet-client-libraries/Refit/RefitLibrary/IUsersClient.cs @@ -2,14 +2,21 @@ namespace RefitLibrary; +[Headers("User-Agent: CodeMaze-Sample")] public interface IUsersClient { [Get("/users")] Task> GetAll(); + [Get("/users")] + Task> GetAll([Query] string? name, CancellationToken token); + [Get("/users/{id}")] Task GetUser(int id); + [Get("/users/{id}")] + Task> GetUserResponse(int id); + [Post("/users")] Task CreateUser([Body] User user); diff --git a/dotnet-client-libraries/Refit/RefitLibrary/Program.cs b/dotnet-client-libraries/Refit/RefitLibrary/Program.cs index 3aca13dd13..3f2f042fd6 100644 --- a/dotnet-client-libraries/Refit/RefitLibrary/Program.cs +++ b/dotnet-client-libraries/Refit/RefitLibrary/Program.cs @@ -7,7 +7,7 @@ .ConfigureServices((_, services) => { services - .AddRefitClient() + .AddRefitGeneratedClient() .ConfigureHttpClient(c => c.BaseAddress = new Uri("https://jsonplaceholder.typicode.com/")); }).Build(); @@ -33,4 +33,9 @@ await usersClient.DeleteUser(userId); Console.WriteLine("User deleted"); +var response = await usersClient.GetUserResponse(999); + +if (!response.IsSuccessStatusCode) + Console.WriteLine($"Failed: {response.StatusCode}"); + public partial class Program { } diff --git a/dotnet-client-libraries/Refit/RefitLibrary/RefitLibrary.csproj b/dotnet-client-libraries/Refit/RefitLibrary/RefitLibrary.csproj index a9865d1dcf..412733dd58 100644 --- a/dotnet-client-libraries/Refit/RefitLibrary/RefitLibrary.csproj +++ b/dotnet-client-libraries/Refit/RefitLibrary/RefitLibrary.csproj @@ -2,15 +2,15 @@ Exe - net6.0 + net10.0 enable enable - - - + + + diff --git a/dotnet-client-libraries/Refit/Tests/Tests.csproj b/dotnet-client-libraries/Refit/Tests/Tests.csproj index dca4934952..67f2b08fc4 100644 --- a/dotnet-client-libraries/Refit/Tests/Tests.csproj +++ b/dotnet-client-libraries/Refit/Tests/Tests.csproj @@ -1,17 +1,17 @@ - net6.0 + net10.0 enable false - - - - + + + + runtime; build; native; contentfiles; analyzers; buildtransitive