MagicCSharp.Clients 1.1.0

There is a newer version of this package available.
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
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MagicCSharp.Clients" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MagicCSharp.Clients" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="MagicCSharp.Clients" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MagicCSharp.Clients --version 1.1.0
                    
#r "nuget: MagicCSharp.Clients, 1.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MagicCSharp.Clients@1.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MagicCSharp.Clients&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=MagicCSharp.Clients&version=1.1.0
                    
Install as a Cake Tool

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 CancellationToken parameter is passed on.
  • Task<T?> returns null when 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 an abstract partial class in MagicCSharp.Testing gives a fake base whose every member throws "not scripted"; the fake overrides only what its tests use.
  • ClientRouteCheck.AssertEveryClientRouteIsServed in MagicCSharp.Testing.Clients fails a server's test when a client path or verb matches no controller route.
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last Updated
1.1.4 304 11/22/2026
1.1.3 99 11/22/2026
1.1.2 107 11/22/2026
1.1.1 96 11/21/2026
1.1.0 71 11/21/2026