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.
- Create a class that will hold your
JsonRpc
methods with theJsonRpcClassAttribute
.
[JsonRpcClass("math")]
public static class MathApi
{
[JsonRpcMethod]
public static Task<int> SumAsync(int n1, int n2)
{
var result = n1 + n2;
return Task.FromResult(result);
}
}
- Call the
JsonRpc.Start
method to start listening forJSON-RPC
requests overHTTP
.
class Program
{
static void Main(string[] args)
{
JsonRpc.Start();
Console.ReadLine();
}
}
- The ideal way to call
JSON-RPC
methods is using anHTTP POST
with theJSON-RPC Request
in theHTTP Body
butHttpJsonRpc
also supportsURL Parameters
for simple method calls. Test the method by pasting theURL
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"
}
- 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();
- You can find more code samples here.