r/Blazor • • 22h ago

I got tired of writing every Interactive Auto service 3 times, so I built a source generator for it (Blazor.AutoGen 1.0)

11 Upvotes

If you use @rendermode InteractiveAuto, you know this one. Your component runs on the server first and then in WebAssembly, where it can't reach your database. So every service ends up needing:

  1. an interface
  2. a server implementation
  3. a client implementation that calls the API with HttpClient
  4. an API endpoint that forwards to the server implementation
  5. DI registrations in both Program.cs files

Items 3 to 5 are boilerplate you keep in sync by hand, forever. The Blazor team tracks this in dotnet/aspnetcore#60312.

Blazor.AutoGen is a Roslyn source generator that writes 3 to 5 for you. You write this:

[Remote]
public interface IWeatherService
{
    Task<WeatherForecast[]> GetForecastAsync(int days, CancellationToken ct = default);

    [Authorize]
    Task<string> GetSignedInUserAsync();
}

Then you implement it once on the server and add three lines:

// .Client/Program.cs
builder.Services.AddRemoteClients();

// Server Program.cs
builder.Services.AddRemoteServices();
app.MapRemoteServices();

Then @inject IWeatherService works anywhere. On the server (prerendering, Interactive Server) it calls your real class in-process with no HTTP. In WASM it goes through a generated HttpClient proxy. The component can't tell which one it got.

What else it does:

  • No generated code in git. It's produced on every build, and your IDE shows it read-only.
  • Your auth works as is. [Authorize], roles and policies work with cookies, JWT or any scheme. Signed-out calls get a 401, not a redirect to a login page. The same rules are checked for in-process calls.
  • HTTP verbs from method names. GetAsync becomes a GET and DeleteAsync a DELETE. Route parameters like {id:int} work, and you can set routes your own way.
  • Streaming and files. Return IAsyncEnumerable<T> to stream items, upload with Stream or IBrowserFile, and download with Task<Stream> or Task<RemoteFile>.
  • DataAnnotations validation is on by default. Bad requests get a 400 with problem details.
  • CSRF protection for GET and DELETE is on by default.
  • Optional extras: OpenAPI descriptions and Brotli/gzip response compression.
  • More hosts: NativeAOT and trimming work with a source-generated JsonSerializerContext. It also runs in MAUI, WPF and WinForms Blazor Hybrid.
  • Call filters (IRemoteCallFilter) wrap every proxy call like middleware, for retries, headers or logging.
  • Escape hatch: [Remote(Exclude = true)] lets you hand-write the client for one awkward method.
  • Build-time checks: analyzers fail the build on contracts that wouldn't work over HTTP, instead of failing at runtime.

Is it fast and tested? The generator takes about 0.3 ms per interface on a full build and is fully incremental, so it doesn't slow down your IDE. There are 652 tests, and I tried it on a large production app before tagging 1.0.

dotnet add package Blazor.AutoGen

GitHub: https://github.com/arthastheking113/Blazor.AutoGen
NuGet: https://www.nuget.org/packages/Blazor.AutoGen

It's MIT licensed. I'd really like feedback, especially edge cases in your own Interactive Auto apps that it doesn't handle. Issues and PRs are welcome.


r/Blazor • • 23h ago

Rider on Mac .net 11 Blazor WASM hot reload not working

Thumbnail reddit.com
0 Upvotes