MagicCSharp.Clients
1.1.0
See the version list below for details.
dotnet add package MagicCSharp.Clients --version 1.1.0
NuGet\Install-Package MagicCSharp.Clients -Version 1.1.0
<PackageReference Include="MagicCSharp.Clients" Version="1.1.0" />
<PackageVersion Include="MagicCSharp.Clients" Version="1.1.0" />
<PackageReference Include="MagicCSharp.Clients" />
paket add MagicCSharp.Clients --version 1.1.0
#r "nuget: MagicCSharp.Clients, 1.1.0"
#:package MagicCSharp.Clients@1.1.0
#addin nuget:?package=MagicCSharp.Clients&version=1.1.0
#tool nuget:?package=MagicCSharp.Clients&version=1.1.0
MagicCSharp.Clients
Typed HTTP clients for calling another service, written by the compiler. You write the interface; with
MagicCSharp.Generators referenced, every build writes the client class and its registration.
dotnet add package MagicCSharp.Clients
dotnet add package MagicCSharp.Generators
[MagicClient("api/leases", UrlSetting = "Services:Leasing:Url", DefaultUrl = "http://leasing")]
public interface ILeasesClient
{
[ClientGet("{leaseId}")]
Task<LeaseDto?> GetLease(long leaseId, long companyId);
[ClientPost]
Task<LeaseDto> CreateLease(CreateLeaseRequest request);
[ClientPut("{leaseId}/renewal-reminder-sent")]
Task MarkRenewalReminderSent(long leaseId);
}
The compiler writes LeasesClient and services.AddLeasesClient(configuration):
{leaseId}takes the parameter of that name, escaped. Other simple parameters go to the query string.- On a GET, object parameters (filters, paging) go to the query string; on a POST, PUT, PATCH or DELETE the one
object parameter is the JSON body. A write without one sends
{}, a DELETE without one sends no body. - A
CancellationTokenparameter is passed on. Task<T?>returnsnullwhen the service answers 404 or 204;Task<T>throws on 404.
The URL comes from configuration under UrlSetting, else DefaultUrl; with neither, registration throws naming the
client and its missing setting. A failed call throws MagicHttpClientException with StatusCode and ResponseBody; its
message names the method, the path without its query string and the status, so logging it logs neither what the
service sent back nor the values the caller asked about.
When to write it by hand: a method with real logic (skipping the call for an empty list, streaming) has no route
attribute and is written by hand in partial class LeasesClient. A member written by hand there that keeps its route
attribute is left to the hand-written body, and the route check below still checks its path. Declare a constructor
there when the client needs more services; the generator then writes none.
To leave the generated client, write class LeasesClient : MagicHttpClientBase, ILeasesClient with every method and
its registration (copy them from the generated files), and remove [MagicClient]. Keeping the route attributes keeps
the route check.
Your own base and registration
By default the client derives from MagicHttpClientBase and registers with AddMagicHttpClient. A codebase with
its own client base (authentication, retries, logging) names it once, in the library its clients share:
[assembly: MagicClientConvention(typeof(ServiceClientBase), typeof(ServiceClientRegistration), nameof(ServiceClientRegistration.AddServiceClient))]
The base offers what the generated code calls:
Task<TResponse> SendRequestAsync<TResponse>(HttpMethod method, string path, object? body, CancellationToken cancellationToken);
Task SendRequestAsync(HttpMethod method, string path, object? body, CancellationToken cancellationToken);
string WithQuery(string path, params object?[] values);
bool IsNotFound(Exception exception);
and the registration is static IServiceCollection AddServiceClient<TInterface, TClient>(this IServiceCollection services, IConfiguration configuration, string? baseUrl).
Tests
[GenerateFakeBase(typeof(ILeasesClient))]on anabstract partial classinMagicCSharp.Testinggives a fake base whose every member throws "not scripted"; the fake overrides only what its tests use.ClientRouteCheck.AssertEveryClientRouteIsServedinMagicCSharp.Testing.Clientsfails a server's test when a client path or verb matches no controller route.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Microsoft.Extensions.Configuration.Abstractions (>= 9.0.1)
- Microsoft.Extensions.Http (>= 9.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on MagicCSharp.Clients:
| Package | Downloads |
|---|---|
|
MagicCSharp.Testing.Clients
Checks MagicCSharp clients against the service they call: a server's test project calls ClientRouteCheck.AssertEveryClientRouteIsServed, and any [MagicClient] path or verb no controller route serves fails the test. Separate from MagicCSharp.Testing because it needs ASP.NET Core to read the controllers' routes. |
GitHub repositories
This package is not used by any popular GitHub repositories.