JsonRpcClient 5.2.1
dotnet add package JsonRpcClient --version 5.2.1
NuGet\Install-Package JsonRpcClient -Version 5.2.1
<PackageReference Include="JsonRpcClient" Version="5.2.1" />
paket add JsonRpcClient --version 5.2.1
#r "nuget: JsonRpcClient, 5.2.1"
// Install JsonRpcClient as a Cake Addin #addin nuget:?package=JsonRpcClient&version=5.2.1 // Install JsonRpcClient as a Cake Tool #tool nuget:?package=JsonRpcClient&version=5.2.1
JSON RPC 2.0 Client
Provides classes for creating JSON RPC 2.0 clients in C#.
By default this supports HTTP with RpcHttpClient
and WebSockets with RpcWsClient
.
Support for other transport protocols can be added by writing a class that extends RpcClient
.
Usage Example
Supposing the JSON RPC server defines the methods "add", "subtract", and "divide", expecting requests like this:
{
"id": 1,
"method": "add",
"params": [2, 3],
"jsonrpc": "2.0"
}
{
"id": 2,
"method": "subtract",
"params": [2, 3],
"jsonrpc": "2.0"
}
{
"id": 3,
"method": "divide",
"params": [3, 2],
"jsonrpc": "2.0"
}
Defining and using the corresponding client would look like this:
using JsonRpcClient.Clients;
public class MathClient : RpcHttpClient
{
public MathClient(string baseUri) : base(baseUri)
{
}
public async Task<long> Add(int a, int b)
{
return (long) (await Call("add", new List<int> {a, b}) ?? throw new InvalidOperationException());
}
public async Task<long> Subtract(int a, int b)
{
return (long) (await Call("subtract", new List<int> {a, b}) ?? throw new InvalidOperationException());
}
public async Task<double> Divide(int a, int b)
{
return (double) (await Call("divide", new List<int> {a, b}) ?? throw new InvalidOperationException());
}
}
This client will form request bodies, send them to the server and return the result.
Errors
If the server responds with an error, an RpcError is thrown. There is an RpcError for each standard JSON RPC 2.0 error, each of them extends RpcError.
var client = new MathClient("http://localhost:5000/api/v1");
try
{
await client.Add("two", "three");
}
catch (InvalidParams e)
{
Console.WriteLine(e);
}
try
{
await client.Divide(0, 0);
}
catch (ServerError e)
{
Console.WriteLine(e);
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
-
net6.0
- Newtonsoft.Json (>= 13.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.