JsonRpcClient 3.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package JsonRpcClient --version 3.0.0                
NuGet\Install-Package JsonRpcClient -Version 3.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="3.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JsonRpcClient --version 3.0.0                
#r "nuget: JsonRpcClient, 3.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=3.0.0

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

JSON RPC 2.0 Client

Provides classes for creating JSON RPC 2.0 clients in C#.

Usage

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 System.Collections.Generic;
using System.Threading.Tasks;
using JsonRpcClient.Clients;
using Newtonsoft.Json;

namespace MathJsonRpcClient
{
    public class MathClientDriver()
    {
        public void main(string[] args)
        {
            var client = new MathClient("http://localhost:5000/api/v1");
            client.add(2, 3);
            client.subtract(2, 3);
            client.divide(3, 2);
        }
    }

    public class MathClient : RpcHttpClient
    {
        public async Task<int> Add(int a, int b)
        {
            return await Call("add", new List<int>{a, b});
        }

        public async Task<int> Subtract(int a, int b)
        {
            return await Call("subtract", new List<int>{a, b});
        }

        public async Task<float> Divide(int a, int b)
        {
            return await Call("divide", new List<int>{a, b});
        }
    }
}

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.

public void main(string[] args)
{
    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 Framework net472 is compatible.  net48 was computed.  net481 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 366 3/10/2024
5.2.0 82 3/10/2024
5.1.0 372 2/11/2023
5.0.1 281 1/5/2023
5.0.0 284 12/28/2022
4.0.2 378 10/3/2022
3.0.0 553 10/18/2021
1.0.1 328 9/4/2021
1.0.0 313 8/29/2021