TeddyAlbina / httpjsonrpcnet

Cross-platform, .NET Standard library that implements the JSON-RPC 2.0 Specification

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HttpJsonRpc

HttpJsonRpc is a cross-platform, .NET Standard library that implements the JSON-RPC 2.0 Specification, as well as streaming (up and down), on top of HTTP.

It makes it simple to create high performance, robust, light-weight API's that can be hosted in any project type. There is no dependency on ASP.NET.

HttpJsonRpc also has extensibility points for logging and dependency injection, so you can use your favorite frameworks.

Getting Started

  1. Create a class that will hold your JsonRpc methods with the JsonRpcClassAttribute.
[JsonRpcClass("math")]
public static class MathApi
{
    [JsonRpcMethod]
    public static Task<int> SumAsync(int n1, int n2)
    {
        var result = n1 + n2;

        return Task.FromResult(result);
    }
}
  1. Call the JsonRpc.Start method to start listening for JSON-RPC requests over HTTP.
class Program
{
    static void Main(string[] args)
    {
        JsonRpc.Start();

        Console.ReadLine();
    }
}
  1. The ideal way to call JSON-RPC methods is using an HTTP POST with the JSON-RPC Request in the HTTP Body but HttpJsonRpc also supports URL Parameters for simple method calls. Test the method by pasting the URL in a browser.
http://localhost:5000/?method=math.sum&n1=2&n2=2

You should get the result in JSON.

{
    "jsonrpc": "2.0",
    "result": 4
}

When calling your method in code or Postman use an HTTP POST with the JSON-RPC Request in the HTTP Body.

{
	"jsonrpc": "2.0",
	"method": "math.sum",
	"params": {
		"n1": 2,
		"n2": 2
	},
	"id": "1"
}
  1. You can add your favorite logging provider by setting the JsonRpc.LoggerFactory property. This sample uses Serilog.
Log.Logger = new LoggerConfiguration()
    .WriteTo.Console()
    .CreateLogger();

JsonRpc.LoggerFactory = new LoggerFactory().AddSerilog();
JsonRpc.Start();
  1. You can find more code samples here.

About

Cross-platform, .NET Standard library that implements the JSON-RPC 2.0 Specification

License:MIT License


Languages

Language:C# 100.0%