grpc-dotnet
grpc-dotnet copied to clipboard
JSON/REST transcoding
Hi, first, thanks for all the work you put into this library! I'm really looking forward to using it!!
Are there any plans to support the REST-like HTTP/JSON transcoding (https://cloud.google.com/endpoints/docs/grpc/transcoding ) via the http service annotations?
Having that together with OpenAPI/Swagger is a HUGE help during development when you can just call the API via JSON objects.
2020/01/14 UPDATE: https://github.com/grpc/grpc-dotnet/issues/167#issuecomment-574005197
2020/04/24 UPDATE:
- Introducing gRPC HTTP API blog post
- Microsoft.AspNetCore.Grpc.HttpApi is added to NuGet.org
2020/12/04 UPDATE:
- Create JSON Web APIs from gRPC is on docs.microsoft.com
- Microsoft.AspNetCore.Grpc.Swagger is added to NuGet.org
2022/02/16 UPDATE:
Thanks for all the interest in this feature. I'm happy to say that the .NET team is investing in JSON/REST transcoding. The aim is to publish this feature along with .NET 7.
Details:
- .NET 7 or later will be required
- Will continue to be a NuGet package
- I estimate a preview will ship in .NET 7 preview 3 or preview 4.
- A final version will ship with .NET 7 in November.
Development work is tracked at [EPIC] gRPC JSON transcoding.
No plans to do it for 3.0. Beyond 3.0, it depends on how many people ask for it.
We're keeping this issue open to solicit feedback and see how much demand there exists for this feature.
FTR, regardless of whether this gets implemented or not, there are multiple proxies that currently offer this functionality: https://github.com/grpc-ecosystem/grpc-gateway https://www.envoyproxy.io/docs/envoy/latest/configuration/http_filters/grpc_json_transcoder_filter
We need HTTP/JSON to gRPC transcoding so that same plumbing/service can be used to support REST and gRPC clients
This would be super helpful to be provided out of the box. As opposed to setting up grpc gateway
Also interested. We use grpc gateway in golang but need the same functionality in dotnet as we are a big .net consumer.
Also interested in this. While there are 3rd party tools that can do this, a simpler implementation is always better.
if i had to start again, I'd probably use an envoy proxy instead similar to that: https://blog.envoyproxy.io/envoy-and-grpc-web-a-fresh-new-alternative-to-rest-6504ce7eb880 so at least I can have the same solution and transcoding for all the languages
That would be very cool since would allow smooth transition from JSON API to GRPC
We're ru
We have an experimental project that allows JSON/REST with gRPC here: https://github.com/aspnet/AspLabs/tree/master/src/GrpcHttpApi
The README.md at the link has details about how to use it and its status as a project.
Looking forward to this feature.
Great job! I think this is a must have for dotnet core framework and tooling. Why proxy gRPC calls when you don't have to. Much more elegant IMO.
This is very helpful. Should it be possible to use Swagger (for example Swashbuckle) with this setup? For now it seems it cannot find any operations.
There is currently no way to generate Swagger. I think a Swashbuckle extension would need to be created that lets Swashbuckle understand gRPC endpoints and protobuf schemas.
Thoughts on support for Azure Functions?
cc @jeffhollan
impressive. I hope to find these functionalities in production soon
Great project. I think GrpcHttpApi should be part of dotnet 5. Waiting for AuthContext implementation... ;-)
The gRPC HTTP API is a great solution - almost every .net application that is adding on gRPC will benefit in maintaining existing external REST interfaces via this solution. We tried it within our team and it works seamlessly on .net core 3.0. However our application is being implemented using .net core 3.1 - any plans on moving the gRPC HTTP API forward?
Does it not work on 3.1?
Hi there, I followed the instructions from https://www.nuget.org/packages/Microsoft.AspNetCore.Grpc.HttpApi... Installed the pre-release version of Microsoft.AspNetCore.Grpc.HttpApi and Microsoft.AspNetCore.Grpc.Swagger (0.1.0-alpha.20254.1) from NuGet official library. However, when opening swagger UI it says "No operations defined in spec!" and the endpoints are not reachable through HTTP (getting 404)... I'm using .NET Core 3.1, I think a similar issue was already reported here: https://github.com/grpc/grpc-dotnet/issues/745. Do you know if Grpc.Swagger works for 3.1?
This is the Startup.cs
using GrpcGateway.Server; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.OpenApi.Models;
namespace GrpcGateway { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddGrpcHttpApi();
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "gRPC HTTP API Example", Version = "v1" });
});
services.AddGrpcSwagger();
//services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseDefaultFiles();
app.UseStaticFiles();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "gRPC HTTP API Example V1");
});
app.UseRouting();
//app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapGrpcService<GreeterService>();
});
}
}
}
Thanks for the details Alejandro.. James - please let us know if you have any info for us or need any details about the issue. Thanks so much for responding so quickly - much appreciated!
It should work in 3.0. I haven't got time to debug this, but feel free to copy the source code for those packages to your machine and step through the code to find out why you get that message.
Thanks again for responding back. We'll debug into the source code - fingers crossed. We were not sure if there were known issues with 3.1 ("Beyond 3.0, it depends on how many people ask for it")... In any case, we'll share what we find - in case it helps any one else.
Works fine in 3.1 - https://github.com/aspnet/AspLabs/tree/master/src/GrpcHttpApi/sample
Many thanks!! We were able to make progress - adding a reference to Grpc.AspNetCore.Server and Grpc.Tools made the difference. Those two packages were not required to generate the server stubs in the latest version of Grpc.AspNetCore but looks like they were needed for Microsoft.AspNetCore.Grpc.HttpApi.
It works in 3.1. In case this helps someone else. In my case the issue was in the proto files, the Proto folder needs to be placed in the solution's root folder and create a service reference in the project using the main .proto file (the one who imports annotations.proto) marking it to generate the server class. I was placing those files in the project folder and generating the server stub directly from it (without the service reference), in that way it doesn't work.
Has Enum type handling already implemented? I have a field which is completely missing from JSON result.
It should work.
Can you post your proto file?
Sure. Thank You.
Using enum, bookDefinition/status is missing:
{
"id": "1",
"isbn": "98765",
"author": "Author",
"description": "Description",
"isActive": true,
"bookGroups": [
"1",
"2"
],
"bookDefinition": {
"id": "1",
"content": "A longer text...",
"version": 1
},
"readerPermissions": [
"15",
"23"
],
"availableVersions": [
1
]
}
Using int32 or string, bookDefinition/status exists:
{
"id": "1",
"isbn": "98765",
"author": "Author",
"description": "Description",
"isActive": true,
"bookGroups": [
"1",
"2"
],
"bookDefinition": {
"id": "1",
"content": "A longer text...",
"version": 1,
"status": 2
},
"readerPermissions": [
"15",
"23"
],
"availableVersions": [
1
]
}