JsonRpcClient 5.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package JsonRpcClient --version 5.0.0                
NuGet\Install-Package JsonRpcClient -Version 5.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JsonRpcClient" Version="5.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JsonRpcClient --version 5.0.0                
#r "nuget: JsonRpcClient, 5.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install JsonRpcClient as a Cake Addin
#addin nuget:?package=JsonRpcClient&version=5.0.0

// Install JsonRpcClient as a Cake Tool
#tool nuget:?package=JsonRpcClient&version=5.0.0                

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
{
    client.add("two", "three");
}
catch (InvalidParams e)
{
    Console.WriteLine(e);
}

try
{
    client.divide(0, 0);
}
catch (ServerError e)
{
    Console.WriteLine(e);
}
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.2.1 558 3/10/2024
5.2.0 86 3/10/2024
5.1.0 376 2/11/2023
5.0.1 285 1/5/2023
5.0.0 288 12/28/2022
4.0.2 384 10/3/2022
3.0.0 559 10/18/2021
1.0.1 336 9/4/2021
1.0.0 319 8/29/2021