Cysharp / MagicOnion

Unified Realtime/API framework for .NET platform and Unity.
MIT License
3.9k stars 433 forks source link

Rework gRPC method binding logic #864

Closed mayuki closed 1 month ago

mayuki commented 1 month ago

This PR re-implements internal gRPC method binding logics.

Before this PR, The service registration logic for Unary/StreamingHub was independent from Grpc.AspNetCore.Server framework. Now, the registration logic of MagicOnion is being re-implemented more tightly based on Grpc.AspNetCore.Server.

This allows more flexible configuration of the MagicOnion gRPC service endpoint. MagicOnion endpoints can be registered in multiple calls.

var builder = WebApplicationBuilder.CreateBuilder(args);
builder.Services.AddMagicOnion();

var app = builder.Build();

// Register all Unary/StreamingHub services from loaded assemblies.
// app.MapMagicOnionService();

// Consumers endpoints
app.MapMagicOnionService([typeof(GreeterService), typeof(ChatHub)]);

// Administration endpoints
app.MapMagicOnionService([typeof(AdministrationService)])
    .RequireHost("*:6000")
    .RequireAuthorization();

app.Run();

Breaking changes

Obsolete AddMagicOnion(Assembly[] searchAssemblies) and AddMagicOnion(IEnumerable<Type> searchTypes)

services.AddMagicOnion(Assembly[] searchAssemblies) and services.AddMagicOnion(IEnumerable<Type> searchTypes) no longer work. Use app.MapMagicOnionService instead.

// Before
var builder = WebApplicationBuilder.CreateBuilder(args);
builder.Services.AddMagicOnion([typeof(MyService), typeof(MyHub)]);

var app = builder.Build();
app.MapMagicOnionService();
app.Run();
// After
var builder = WebApplicationBuilder.CreateBuilder(args);
builder.Services.AddMagicOnion();

var app = builder.Build();
app.MapMagicOnionService([typeof(MyService), typeof(MyHub)]);
app.Run();