JsonRpcClient 1.0.0

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

// Install JsonRpcClient as a Cake Tool
#tool nuget:?package=JsonRpcClient&version=1.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)
        {
            var v = await Request("add", new List<int>{a, b});
            return JsonConvert.DeserializeObject<int>(v.ToString());
        }

        public async Task<int> Subtract(int a, int b)
        {
            var v = await Request("subtract", new List<int>{a, b});
            return JsonConvert.DeserializeObject<int>(v.ToString());
        }

        public async Task<float> Divide(int a, int b)
        {
            var v = await Request("divide", new List<int>{a, b});
            return JsonConvert.DeserializeObject<float>(v.ToString());
        }
    }
}

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.multiply(2, 3);
    }
    catch (MethodNotFound e)
    {
        Console.WriteLine(e);
    }

    try
    {
        client.add("two", "three");
    }
    catch (InvalidParams e)
    {
        Console.WriteLine(e);
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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