11

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?

Troll the Legacy
  • 675
  • 2
  • 7
  • 22

1 Answers1

2

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:

Pac0
  • 21,465
  • 8
  • 65
  • 74
Ygalbel
  • 5,214
  • 1
  • 24
  • 32
  • 1
    while this is interesting, link-only answers are discouraged, as the links can become invalid, and so will the whole answer. Consider incoporating some summary information to answer the question, and / or quote relevant parts from the source in the answer itself. – Pac0 Oct 16 '20 at 11:01
  • I will edit the answer. But with the logic you mentioned, even if I will put the code here, the github repo can be deleted and also the nuget package so this code will become useless. – Ygalbel Oct 16 '20 at 11:04
  • 1
    of course, adding info to use a package does not prevent the package from disappearing. This policy is not _sufficient_ to prevent an answer become invalid, but link-roting is something very common, and thus forbidding link-only answers has become a policy on Stack Overflow. As an example, many links to old microsoft doc are now invalid. However, everything (languages, framework, packages) is still working. See for instance this [meta post](https://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers) for more insight. – Pac0 Oct 16 '20 at 11:08