You write an interface and one server implementation. The generator writes the WebAssembly HttpClient proxy, the API endpoints and the DI registrations, at compile time.
dotnet add package Blazor.AutoGen --prerelease
// You write this. Nothing else. using Blazor.AutoGen; [Remote] public interface IWeatherService { Task<WeatherForecast[]> GetForecastAsync(int days); [Authorize] Task<string> GetSignedInUserAsync(); }
// Generated in the .Client project (simplified) internal sealed class WeatherServiceProxy(HttpClient client) : IWeatherService { // "Get..." makes it a GET; days goes in the query string. public async Task<WeatherForecast[]> GetForecastAsync(int days) { using var message = new HttpRequestMessage(HttpMethod.Get, "_remote/MyApp.IWeatherService/GetForecastAsync?days=" + days); using var response = await client.SendAsync(message); response.EnsureSuccessStatusCode(); return await response.Content.ReadFromJsonAsync<WeatherForecast[]>(); } // ... }
// Generated in the server project (simplified) internal static class RemoteEndpoints { public static RouteGroupBuilder MapRemoteServices(this IEndpointRouteBuilder app) { var group = app.MapGroup(""); group.MapGet("_remote/MyApp.IWeatherService/GetForecastAsync", ...); group.MapGet("_remote/MyApp.IWeatherService/GetSignedInUserAsync", ...) .RequireAuthorization(); // from [Authorize] return group; } public static IServiceCollection AddRemoteServices(this IServiceCollection services) { services.TryAddScoped<IWeatherService, WeatherService>(); return services; } }
A component runs on the server first, then in the browser on WebAssembly, where it can't reach your database. So every service needs five pieces kept in sync by hand. The Blazor team tracks this as dotnet/aspnetcore#60312.
IWeatherService interfaceWeatherService server implementationWeatherClient with HttpClient callsMapGet / MapPost(...) endpoint per methodProgram.cs files[Remote] IWeatherService interfaceWeatherService server implementationOnly the first two are in your repository. The compiler generates the rest on every build, so there's nothing to commit or keep in sync.
Your component injects the interface. Where it runs decides what it gets.
Prerendering and Interactive Server resolve WeatherService straight from DI. A direct method call, no HTTP, no serialization.
Moving an existing app? Add [Remote], then delete your hand-written client service, endpoint and registrations.
<!-- Server.csproj and Client.csproj. Source generators don't flow through project references, so each project gets its own reference. --> <PackageReference Include="Blazor.AutoGen" Version="0.1.0-preview.13" />
// MyApp.Client/IWeatherService.cs using Blazor.AutoGen; using Microsoft.AspNetCore.Authorization; [Remote] public interface IWeatherService { Task<WeatherForecast[]> GetForecastAsync(int days, CancellationToken ct = default); [Authorize] Task<string> GetSignedInUserAsync(); }
// MyApp/WeatherService.cs: this code never ships to the browser public class WeatherService(AppDbContext db, AuthenticationStateProvider auth) : IWeatherService { // Real logic: database, files, secrets... }
// .Client/Program.cs builder.Services.AddRemoteClients(); // Server Program.cs builder.Services.AddRemoteServices(); // registers WeatherService as IWeatherService app.MapRemoteServices(); // maps the /_remote/... endpoints // Any component @rendermode InteractiveAuto @inject IWeatherService WeatherService
Generated code is plain, internal C# you can read in your IDE, rebuilt by the compiler every time, so none of it lands in your repository. It plugs into the ASP.NET Core features you already use.
[Authorize], roles, policies, cookies, Identity and JWT bearer all work. Signed-out calls get a 401, never a redirect, and GET and DELETE endpoints refuse cross-site links and embeds (CSRF), on by default.
IRemoteCallFilter wraps every proxy call like middleware: rewrite arguments, add headers, short-circuit, retry, or log failures to your own logger.
Name routes per interface or method, or match your controllers project-wide with RemoteConvention.
GET, POST, PUT, PATCH and DELETE, picked from the method name or set with [Remote(Method = ...)]. GET and DELETE send exact, culture-proof query strings.
By default GetAsync is a GET, UpdateAsync a PUT, DeleteAsync a DELETE, with nothing to set up. Wrong methods get a proper 405.
Opt in with compressResponses: true and large results go out Brotli-compressed, about 80% smaller.
Duplicate routes, controller clashes and DTOs that won't survive JSON are compiler diagnostics, not runtime surprises.
The same proxies run in .NET MAUI, WPF and WinForms hybrid apps, pointed at your server with your own HttpClient.
Opt in per method with [Remote]. Unmarked methods get no endpoint, so server-only operations are never exposed.
Request bodies can be resent, so your DelegatingHandlers, Polly policies and 401-refresh patterns work as they are.
System.Text.Json on the wire, configured once for both sides. Newtonsoft MVC controllers can live next to it.
The compiler generates the code on every build and caches it between edits, so your IDE stays fast even with 100 remote interfaces. Nothing generated goes into git.
Speed: BenchmarkDotNet on real Kestrel over loopback. Size: the sample app published for WebAssembly, compared file by file. .NET 10, Release, one developer machine; compare the numbers with each other, not as absolutes.
The generated types are sealed classes, not structs: JsonContent would box a struct request anyway, and every struct type would get its own copy of the generic call code, growing the download. A call takes about 100 µs, mostly network and JSON.
The sample app published in Release with 300 extra methods. Per method the generated code costs about 58 bytes, one-async-method-per-call clients about 184 and the leanest helper-based clients about 20; with a fixed cost of about 8 KB, the generated code is smaller than the typical style from about 80 methods on. .NET 8, 9 and 10 apps get the package's net8.0 build, so they don't download the netstandard.wasm compatibility file.
The generator checks your contracts as you type and points at the exact line.
#pragma and .editorconfig severities work as usualerror BAG011: Two remote endpoints share a route error BAG012: Remote route matches a controller action warning BAG010: DTO relies on Newtonsoft [JsonProperty] error BAG016: GET parameter can't go in a query string warning BAG008: Parameter type can't be sent as JSON info BAG014: Your controllers use a route pattern; add [assembly: RemoteConvention("api/[service]/[method]")]
The generator looks at what each project references and emits exactly what it needs.
Free and open source under the MIT license.
dotnet add package Blazor.AutoGen --prerelease