Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce MagicOnion.Server.JsonTranscoding #871

Merged
merged 21 commits into from
Nov 28, 2024
Merged

Conversation

mayuki
Copy link
Member

@mayuki mayuki commented Nov 20, 2024

This PR introduces JSON transcoding support and Swagger integration.

These changes enhance the MagicOnion framework by adding support for JSON transcoding for Unary services and Swagger, making it easier to build and document gRPC services with JSON endpoints.

The feature inspired by Microsoft.AspNetCore.Grpc.JsonTranscoding.

This is the successor to the previous MagicOnion.Server.HttpGateway, but it is a completely new implementation and is not compatible, and is mainly intended for development support purposes.

Warning

This feature is not intented for use in the Production environment.

If you want to provide Web-based (HTTP/1) APIs, we strongly recommend using ASP.NET Core Web API instead.

By default, JsonTranscoding cannot be enabled in the Production environment. You need to change MagicOnionJsonTranscodingOptions.AllowEnableInNonDevelopmentEnvironment to true.

var builder = WebApplication.CreateBuilder(args);

// Add MagicOnion services to the container and enable JSON transcoding feature.
builder.Services.AddMagicOnion().AddJsonTranscoding();
// Add MagicOnion JSON transcoding Swagger support.
builder.Services.AddMagicOnionJsonTranscodingSwagger();
// Add Swagger generator services.
builder.Services.AddSwaggerGen(options =>
{
    // Reflect the XML documentation comments of the service definition in Swagger.
    options.IncludeMagicOnionXmlComments(Path.Combine(AppContext.BaseDirectory, "JsonTranscodingSample.Shared.xml"));
});

var app = builder.Build();

// Configure the HTTP request pipeline.

// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
if (app.Environment.IsDevelopment())
{
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    });
}

app.MapMagicOnionService();
app.MapGet("/", () => "Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");

app.Run();

image

@mayuki mayuki marked this pull request as draft November 20, 2024 06:42
@mayuki mayuki marked this pull request as ready for review November 28, 2024 06:09
@mayuki mayuki merged commit 42eab71 into main Nov 28, 2024
4 checks passed
@mayuki mayuki deleted the feature/JsonTranscoding branch November 28, 2024 08:41
@mayuki mayuki changed the title MagicOnion.Server.JsonTranscoding Introduce MagicOnion.Server.JsonTranscoding Nov 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant