Skip to main content

# Transcoding

- what is is gRPC transcoding
- exposing our services as HTTP
- Consuming methods from the browser?!
- How routing works with gRPC transconding

# what is is gRPC transcoding

gRPC JSON transcoding is an extension for ASP.NET Core that creates RESTful JSON APIs for gRPC services. Once configured, transcoding allows apps to call gRPC services with familiar HTTP concepts:

- HTTP verbs
- URL parameter binding
- JSON requests/responses gRPC can still be used to call services, but gives us flexibility API endpoint. We can do that.

gRPC transcoding tries to replace an experimental library that was bord from the HTTP API rule below: https://cloud.google.com/dotnet/docs/reference/Google.Api.CommonProtos/latest/Google.Api.HttpRule rectly. Transcoding in ASP.NET Core offers advantages to .NET app developers:

- Less complex: Both gRPC services and mapped RESTful JSON API run out of one ASP.NET Core app, and it runs inside the app(in the same proc)
- Better performance: Transcoding deserializes JSON to Protobuf messages and invokes the gRPC service directly. There are significant performance benefits in doing this in-process versus making a new gRPC call to a different server.
- Lower cost: Fewer servers result in a smaller monthly hosting bill.

## Enabling gRPC JSON transcoding​

1. Install `Microsoft.AspNetCore.Grpc.JsonTranscoding` NuGet Package

2. In Program.cs add `builder.Services.AddGrpc().AddJsonTranscoding();`

3. In our service add a folder named `google`

4. In the google folder, add a folder named api.

5. add one file named http.proto

6. get the file here: and paste the code below http.proto

7. add another file named annotations.proto.

8. get the file here: annotations.proto

9. Import the annotations.proto in the `speaker.proto` file

# Annotate gRPC methods

gRPC methods must be annotated with an HTTP rule before they support transcoding. The HTTP rule includes information about how to call the gRPC method, such as the HTTP method and route.

1. Expose the 'Get' method as an API endpoint method

Title

    rpc SayHello (HelloRequest) returns (HelloReply) {
        option (google.api.http) = {
        get: "/v1/greeter/{name}"
    };
    }

## Exposing a Post​

Exposing a POST

    rpc SayHelloFrom (HelloRequestFrom) returns (HelloReply) {
        option (google.api.http) = {
          post: "/v1/greeter"
          body: "*"
        };
      }

## Exposing a PUT​

Exposing a PUT

    rpc SayHelloFrom (HelloRequestFrom) returns (HelloReply) {
        option (google.api.http) = {
          put: "/v1/greeter"
          body: "*"
        };
      }

## Exposing a DELETE​

Exposing a DELETE

    rpc SayHelloFrom (HelloRequestFrom) returns (HelloReply) {
        option (google.api.http) = {
          delete: "/v1/greeter/{name}"
          body: "*"
        };
      }

When we expose something as an endpoint, we need to take care of how it is exposed. We basically tell the IDL that :" this is the service definition, that also 'translates' as this specific endpoint"

Traditional gRPC over HTTP/2 supports streaming in all directions. Transcoding is limited to server streaming only. Client streaming and bidirectional streaming methods aren't supported.

# Let's write some code

swagger/index.html