- Install the packages
- Declare the interface in a shared library
- Mark the server and the client
- Register the services
Install the Packages
| Package |
Install in |
Contents |
RForge.Blazor.AutoApi.Abstractions |
The shared library |
Attributes, RfHttpMethod and the exception types. No ASP.NET dependency. |
RForge.Blazor.AutoApi.Generator |
The server project and the client project |
The source generator and its diagnostics. It adds no runtime assembly. |
Command Line
dotnet add MyApp.Shared package RForge.Blazor.AutoApi.Abstractions
dotnet add MyApp package RForge.Blazor.AutoApi.Generator
dotnet add MyApp.Client package RForge.Blazor.AutoApi.Generator
The RForge.Blazor package installs the whole family instead, including both AutoApi packages. Install
it in each project that needs a generator, because NuGet does not pass analyzers through project references.
MyApp.Shared -> Abstractions (interfaces, DTOs, JsonSerializerContext)
MyApp -> Generator + Shared (server host, real implementations)
MyApp.Client -> Generator + Shared (WebAssembly project)
Declare the Interface
Put the interface, its data types and a JsonSerializerContext in the shared library. The context has to
live in a referenced assembly, because the generator reads it from there. A context in the same
project as the marker class looks empty to the generator and reports RFAA009.
[assembly: RForgeAutoApiDefaults(JsonContext = typeof(AppJsonContext))]
[RForgeAutoApi]
public interface IOrderService
{
Task<OrderDto?> GetOrderAsync(int id, CancellationToken cancellationToken = default);
}
[JsonSourceGenerationOptions(JsonSerializerDefaults.Web)]
[JsonSerializable(typeof(OrderDto))]
[JsonSerializable(typeof(int))]
public partial class AppJsonContext : JsonSerializerContext { }
Mark the Server and the Client
// Server project
[RForgeAutoApiHost(typeof(IOrderService))]
public static partial class AutoApiEndpoints { }
// Client project
[RForgeAutoApiClient(typeof(IOrderService))]
public static partial class AutoApiClients { }
Use one host class and one client class per project (RFAA014).
Register the Services
// Program.cs (server)
builder.Services.AddScoped<IOrderService, OrderService>(); // your real implementation
builder.Services.AddRForgeAutoApiServer(); // call AFTER your registrations
// ...
app.MapRForgeAutoApi();
// Program.cs (WebAssembly client)
builder.Services.AddRForgeAutoApi();
Requirements
| Requirement |
Details |
| .NET |
.NET 8, 9 or 10. The generator runs on the .NET 8 SDK and newer. |
| Services with authorization |
Must be Scoped or Transient. A Singleton cannot see the current user, so registering one throws at startup. |
| Deployment |
Reordering overloads changes their generated routes, so deploy the server and the client together.
|
Which Package Do I Install?
| You want |
Install |
| The UI components only |
RForge.Blazor.Components |
| Typed events and state only |
RForge.Blazor.Events |
| An API generated from a service interface |
RForge.Blazor.AutoApi.Abstractions and RForge.Blazor.AutoApi.Generator |
| Everything |
RForge.Blazor |