Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core JSON-RPC

I've created core WebAPI project and while RESTing performs quite good, there's also a need in JSON-RPC functionality. I saw things like this or this, but still don't know which one of them is preferred for organizing server and client(which is aspnetcore too) as good replacement of something like WCF.

So how to do JSON-RPC with ASP.NET Core in the right way?

like image 311
Troll the Legacy Avatar asked Feb 28 '19 06:02

Troll the Legacy


1 Answers

You can find a great example from James Newton-King.

Newtonsoft.Json creator made a project that works great with gRPC and ASP.NET.

Check here: https://github.com/aspnet/AspLabs/tree/master/src/GrpcHttpApi

And an example from the readme:

Usage:

  • Add a package reference to Microsoft.AspNetCore.Grpc.HttpApi.

  • Register services in Startup.cs with AddGrpcHttpApi().

  • Add google/api/http.proto and google/api/annotations.proto files to your project.

  • Annotate gRPC methods in your .proto files with HTTP bindings and routes:

syntax = "proto3";

import "google/api/annotations.proto";

package greet;

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

message HelloRequest {
  string name = 1;
}

message HelloReply {
  string message = 1;
}

The SayHello gRPC method can now be invoked as gRPC+Protobuf and as an HTTP API:

like image 107
Ygalbel Avatar answered Oct 05 '22 22:10

Ygalbel