GrpcJsonTranscoder 0.2.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package GrpcJsonTranscoder --version 0.2.0                
NuGet\Install-Package GrpcJsonTranscoder -Version 0.2.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="GrpcJsonTranscoder" Version="0.2.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add GrpcJsonTranscoder --version 0.2.0                
#r "nuget: GrpcJsonTranscoder, 0.2.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install GrpcJsonTranscoder as a Cake Addin
#addin nuget:?package=GrpcJsonTranscoder&version=0.2.0

// Install GrpcJsonTranscoder as a Cake Tool
#tool nuget:?package=GrpcJsonTranscoder&version=0.2.0                

Grpc-Json Transcoder project

Price version

This is a filter that allows a RESTful JSON API client (Ocelot Gateway) to send requests to .NET Web API (Aggregation Service) over HTTP and get proxied to a gRPC service (on behind).

This project is inspired by grpc-gateway which is totally for golang, grpc-dynamic-gateway is for nodejs. And especially, Envoy gRPC-JSON transcoder is the best of transcoding in this area, but it is only on the infrastructure level. You also can use it just like my project used at coolstore-microservices.

gRPC parser borrows the idea from Ocelot.GrpcHttpGateway code-based.

Give a Star!

If you liked GrpcJsonTranscoder project or if it helped you, please give a star ⭐ for this repository. That will not only help strengthen our .NET community but also improve cloud-native apps development skills for .NET developers in around the world. Thank you very much 👍

Check out my blog or say hi on Twitter!

How to run it!

$ docker-compose up

or

$ bash
$ start.sh # I haven't done it yet :p

In the mean time, we open up the visual studio, run multiple projects included OcelotGateway, AggregationRestApi, ProductCatalogGrpcServer and GreatGrpcServer

  • OcelotGateway (.NET Core 2.2): http://localhost:5000
  • AggregationRestApi (.NET Core 3.0): http://localhost:5001
  • ProductCatalogGrpcServer (.NET Core 3.0): http://localhost:5002
  • GreatGrpcServer (.NET Core 3.0): http://localhost:5003

Test it as below:

$ curl -X GET -H 'content-type: application/grpc' -k http://localhost:5000/say/Bob
$ {"Message":"Hello Bob"}
$ curl -X GET -H 'content-type: application/grpc' -k http://localhost:5000/products
$ {"Products":[{"Id":1,"Name":"product 1","Quantity":52,"Description":"description of product 1"},...]}
$ curl -X POST -H 'content-type: application/grpc' -d '{ "name": "product 1", "quantity": 1, "description": "this is product 1" }' -k http://localhost:5000/products
$ {"Product":{"Id":915,"Name":"product 1 created","Quantity":1,"Description":"this is product 1 created"}}

How to understand it!

The project aims to .NET community and its ecosystem which leverage the power of Ocelot Gateway which is very powerful in the gateway components were used by various of companies and sample source code when we try to adopt the microservices architecture project.

alternate text is missing from this package README image

We will normally use Ocelot configuration for the transcode process, the main parser and transformation processes are only happening at aggregation service level so that you will easy to upgrade Ocelot in case we need, but not affect to the grpc-json transcode seats in the aggregation service.

// ocelot.json
{
  "ReRoutes": [
    {
      "UpstreamPathTemplate": "/say/{name}",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamPathTemplate": "/Greet.Greeter/SayHello",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5001
        }
      ]
    },
    {
      "UpstreamPathTemplate": "/products",
      "UpstreamHttpMethod": [ "Get" ],
      "DownstreamPathTemplate": "/ProductCatalog.Product/GetProducts",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5001
        }
      ]
    },
    {
      "UpstreamPathTemplate": "/products",
      "UpstreamHttpMethod": [ "Post" ],
      "DownstreamPathTemplate": "/ProductCatalog.Product/CreateProduct",
      "DownstreamScheme": "http",
      "DownstreamHostAndPorts": [
        {
          "Host": "localhost",
          "Port": 5001
        }
      ]
    }
  ],
  "GlobalConfiguration": {
    "BaseUrl": "http://localhost:5000"
  }
}

and,

// Program.cs
var configuration = new OcelotPipelineConfiguration
{
    PreQueryStringBuilderMiddleware = async (ctx, next) =>
    {
        var routes = ctx.TemplatePlaceholderNameAndValues;
        ctx.DownstreamRequest.Headers.Add(
            "x-grpc-routes",
            JsonConvert.SerializeObject(routes.Select(x => new NameAndValue { Name = x.Name, Value = x.Value })));
        await next.Invoke();
    }
};

app.UseOcelot(configuration).Wait();

More at https://github.com/thangchung/GrpcJsonTranscoder/tree/master/samples/OcelotGateway

Then we only put some of the json configurations into appsettings.json inside aggregation service to point it to other gRPC services we need.

// appsettings.json
"GrpcJsonTranscoder": {
  "GrpcMappers": [
    {
      "GrpcMethod": "/Greet.Greeter/SayHello",
      "GrpcHost": "127.0.0.1:5003"
    },
    {
      "GrpcMethod": "/ProductCatalog.Product/GetProducts",
      "GrpcHost": "127.0.0.1:5002"
    },
    {
      "GrpcMethod": "/ProductCatalog.Product/CreateProduct",
      "GrpcHost": "127.0.0.1:5002"
    }
  ]
}
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddGrpcJsonTranscoder(() => new GrpcAssemblyResolver().ConfigGrpcAssembly(typeof(Greeter.GreeterClient).Assembly));
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...

    app.UseGrpcJsonTranscoder();
    
    ...
}

More at https://github.com/thangchung/GrpcJsonTranscoder/tree/master/samples/AggregationRestApi

Don't believe what I said. Try it!

We haven't tested it with stream and full-duplex transport protocols yet. So we feel free to contribute by the .NET community.

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :p
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.5.0 1,636 2/23/2020
0.4.0 706 9/10/2019
0.3.0 612 8/17/2019
0.2.0 562 8/16/2019
0.1.0 545 8/10/2019