Nethereum.JsonRpc.Client 5.8.0

Prefix Reserved
dotnet add package Nethereum.JsonRpc.Client --version 5.8.0
                    
NuGet\Install-Package Nethereum.JsonRpc.Client -Version 5.8.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="Nethereum.JsonRpc.Client" Version="5.8.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Nethereum.JsonRpc.Client" Version="5.8.0" />
                    
Directory.Packages.props
<PackageReference Include="Nethereum.JsonRpc.Client" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Nethereum.JsonRpc.Client --version 5.8.0
                    
#r "nuget: Nethereum.JsonRpc.Client, 5.8.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.
#:package Nethereum.JsonRpc.Client@5.8.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Nethereum.JsonRpc.Client&version=5.8.0
                    
Install as a Cake Addin
#tool nuget:?package=Nethereum.JsonRpc.Client&version=5.8.0
                    
Install as a Cake Tool

Nethereum.JsonRpc.Client

Core JSON-RPC abstraction layer for Ethereum node communication.

Overview

Nethereum.JsonRpc.Client provides the fundamental abstraction layer for all JSON-RPC communication with Ethereum nodes. It defines the core interfaces and base classes that all RPC client implementations must implement, enabling pluggable transport mechanisms (HTTP, WebSocket, IPC) while maintaining consistent error handling, request interception, and batch processing.

Key Features:

  • Transport-agnostic RPC abstraction (HTTP, WebSocket, IPC)
  • Request/response message handling
  • Batch request support
  • Request interception for logging/monitoring
  • Consistent error handling across transports
  • Basic authentication support
  • Streaming/subscription support

Use Cases:

  • Building custom RPC client implementations
  • Implementing request logging and monitoring
  • Creating custom transport mechanisms
  • Testing and mocking RPC communication
  • Building middleware for RPC calls

Installation

dotnet add package Nethereum.JsonRpc.Client

Note: This is typically used as a dependency by concrete client implementations. Most users will use:

  • Nethereum.JsonRpc.RpcClient - HTTP/HTTPS client
  • Nethereum.JsonRpc.WebSocketClient - WebSocket client
  • Nethereum.JsonRpc.IpcClient - IPC client

Dependencies

Nethereum:

  • Nethereum.Hex - Hex encoding/decoding utilities

External:

  • Microsoft.Extensions.Logging.Abstractions (v6.0.0+) - Logging support (conditional dependency for modern frameworks)

JSON Serialization (Peer Dependencies):

  • Supports Newtonsoft.Json (if available in consuming application)
  • Supports System.Text.Json (.NET 6.0+, if available in consuming application)
  • These are not included by this package - your application chooses the JSON library

Quick Start

using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth;

// Use concrete implementation (RpcClient)
var client = new RpcClient(new Uri("http://localhost:8545"));

// Use through higher-level RPC services
var ethBlockNumber = new EthBlockNumber(client);
var blockNumber = await ethBlockNumber.SendRequestAsync();

Console.WriteLine($"Current block: {blockNumber.Value}");

Core Interfaces

IClient

Main interface for JSON-RPC communication:

public interface IClient : IBaseClient
{
    // Send single request
    Task<T> SendRequestAsync<T>(RpcRequest request, string route = null);
    Task<T> SendRequestAsync<T>(string method, string route = null, params object[] paramList);

    // Send batch request
    Task<RpcRequestResponseBatch> SendBatchRequestAsync(RpcRequestResponseBatch rpcRequestResponseBatch);

    // Low-level message sending
    Task<RpcResponseMessage> SendAsync(RpcRequestMessage rpcRequestMessage, string route = null);
}

IBaseClient

Base interface with common properties:

public interface IBaseClient
{
    RequestInterceptor OverridingRequestInterceptor { get; set; }
    TimeSpan ConnectionTimeout { get; set; }
}

Usage Examples

Example 1: Basic RPC Requests

using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth;
using Nethereum.Hex.HexTypes;

// Create HTTP client
var client = new RpcClient(new Uri("http://localhost:8545"));

// Use with RPC services
var ethChainId = new EthChainId(client);
HexBigInteger chainId = await ethChainId.SendRequestAsync();

var ethAccounts = new EthAccounts(client);
string[] accounts = await ethAccounts.SendRequestAsync();

Console.WriteLine($"Chain ID: {chainId.Value}");
Console.WriteLine($"Accounts: {string.Join(", ", accounts)}");

Example 2: Direct Method Calls

using Nethereum.JsonRpc.Client;
using Newtonsoft.Json.Linq;

var client = new RpcClient(new Uri("http://localhost:8545"));

// Direct JSON-RPC method call
var blockNumber = await client.SendRequestAsync<string>("eth_blockNumber");
Console.WriteLine($"Block: {blockNumber}");

// With parameters
var balance = await client.SendRequestAsync<string>(
    "eth_getBalance",
    null, // route
    "0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb", // address
    "latest" // block parameter
);
Console.WriteLine($"Balance: {balance}");

Example 3: Batch Requests

using Nethereum.JsonRpc.Client;

var client = new RpcClient(new Uri("http://localhost:8545"));

// Create batch request
var batch = new RpcRequestResponseBatch();

// Add multiple requests
var blockNumberRequest = new RpcRequestMessage(1, "eth_blockNumber");
var chainIdRequest = new RpcRequestMessage(2, "eth_chainId");
var gasPriceRequest = new RpcRequestMessage(3, "eth_gasPrice");

batch.BatchItems.Add(new RpcRequestResponseBatchItem<string>(blockNumberRequest));
batch.BatchItems.Add(new RpcRequestResponseBatchItem<string>(chainIdRequest));
batch.BatchItems.Add(new RpcRequestResponseBatchItem<string>(gasPriceRequest));

// Send batch
var batchResult = await client.SendBatchRequestAsync(batch);

// Process results
foreach (var item in batchResult.BatchItems)
{
    if (item.HasError)
    {
        Console.WriteLine($"Request {item.RpcRequestMessage.Id} failed: {item.RpcError.Message}");
    }
    else
    {
        Console.WriteLine($"Request {item.RpcRequestMessage.Id}: {item.GetResponse<string>()}");
    }
}

Example 4: Request Interception for Logging

using Nethereum.JsonRpc.Client;
using System.Diagnostics;

// Custom request interceptor
public class LoggingInterceptor : RequestInterceptor
{
    public override async Task<object> InterceptSendRequestAsync<T>(
        Func<RpcRequest, string, Task<T>> interceptedSendRequestAsync,
        RpcRequest request,
        string route = null)
    {
        var sw = Stopwatch.StartNew();
        Console.WriteLine($"[REQUEST] {request.Method} - Params: {string.Join(", ", request.RawParameters ?? Array.Empty<object>())}");

        try
        {
            var result = await base.InterceptSendRequestAsync(interceptedSendRequestAsync, request, route);
            sw.Stop();
            Console.WriteLine($"[RESPONSE] {request.Method} - {sw.ElapsedMilliseconds}ms");
            return result;
        }
        catch (Exception ex)
        {
            sw.Stop();
            Console.WriteLine($"[ERROR] {request.Method} - {sw.ElapsedMilliseconds}ms - {ex.Message}");
            throw;
        }
    }
}

// Usage
var client = new RpcClient(new Uri("http://localhost:8545"));
client.OverridingRequestInterceptor = new LoggingInterceptor();

var ethBlockNumber = new EthBlockNumber(client);
var blockNumber = await ethBlockNumber.SendRequestAsync();
// Output: [REQUEST] eth_blockNumber - Params:
// Output: [RESPONSE] eth_blockNumber - 45ms

Example 5: Error Handling

using Nethereum.JsonRpc.Client;

var client = new RpcClient(new Uri("http://localhost:8545"));

try
{
    // Invalid method call
    var result = await client.SendRequestAsync<string>("invalid_method");
}
catch (RpcResponseException ex)
{
    // Standard RPC error
    Console.WriteLine($"RPC Error {ex.RpcError.Code}: {ex.RpcError.Message}");
    if (ex.RpcError.Data != null)
    {
        Console.WriteLine($"Error data: {ex.RpcError.Data}");
    }
}
catch (RpcClientTimeoutException ex)
{
    // Request timeout
    Console.WriteLine($"Request timed out: {ex.Message}");
}
catch (RpcClientUnknownException ex)
{
    // Network or other errors
    Console.WriteLine($"Unknown error: {ex.Message}");
    Console.WriteLine($"Inner exception: {ex.InnerException?.Message}");
}

Example 6: Authentication with Basic Auth

using Nethereum.JsonRpc.Client;
using System.Net.Http.Headers;
using System.Text;

// Option 1: URL-based authentication
var clientWithAuth = new RpcClient(
    new Uri("http://username:password@localhost:8545")
);

// Option 2: Explicit AuthenticationHeaderValue
var credentials = Convert.ToBase64String(
    Encoding.UTF8.GetBytes("username:password")
);
var authHeader = new AuthenticationHeaderValue("Basic", credentials);

var client = new RpcClient(
    new Uri("http://localhost:8545"),
    authHeaderValue: authHeader
);

var blockNumber = await client.SendRequestAsync<string>("eth_blockNumber");
Console.WriteLine($"Authenticated request successful: {blockNumber}");

Example 7: Custom Connection Timeout

using Nethereum.JsonRpc.Client;

var client = new RpcClient(new Uri("http://localhost:8545"));

// Default timeout is 120 seconds
Console.WriteLine($"Default timeout: {client.ConnectionTimeout.TotalSeconds}s");

// Set custom timeout
client.ConnectionTimeout = TimeSpan.FromSeconds(10);

try
{
    var ethBlockNumber = new EthBlockNumber(client);
    var blockNumber = await ethBlockNumber.SendRequestAsync();
}
catch (RpcClientTimeoutException ex)
{
    Console.WriteLine($"Request timed out after 10 seconds: {ex.Message}");
}

Example 8: Building RPC Requests

using Nethereum.JsonRpc.Client;

// Using RpcRequestBuilder
var builder = new RpcRequestBuilder("eth_getBlockByNumber");

// Build request with parameters
var request = builder.BuildRequest(
    id: 1,
    paramList: new object[] { "0x1b4", true }
);

Console.WriteLine($"Method: {request.Method}");
Console.WriteLine($"ID: {request.Id}");
Console.WriteLine($"Params: {string.Join(", ", request.RawParameters)}");

// Send using client
var client = new RpcClient(new Uri("http://localhost:8545"));
var result = await client.SendRequestAsync<object>(request);

Example 9: Partial Batch Success Handling

using Nethereum.JsonRpc.Client;

var client = new RpcClient(new Uri("http://localhost:8545"));

var batch = new RpcRequestResponseBatch();

// Add valid and invalid requests
batch.BatchItems.Add(new RpcRequestResponseBatchItem<string>(
    new RpcRequestMessage(1, "eth_blockNumber")
));
batch.BatchItems.Add(new RpcRequestResponseBatchItem<string>(
    new RpcRequestMessage(2, "invalid_method") // This will fail
));
batch.BatchItems.Add(new RpcRequestResponseBatchItem<string>(
    new RpcRequestMessage(3, "eth_chainId")
));

// Accept partial success
batch.AcceptPartiallySuccessful = true;

var result = await client.SendBatchRequestAsync(batch);

// Process mixed results
int successCount = 0;
int errorCount = 0;

foreach (var item in result.BatchItems)
{
    if (item.HasError)
    {
        errorCount++;
        Console.WriteLine($"Request {item.RpcRequestMessage.Id} failed: {item.RpcError.Message}");
    }
    else
    {
        successCount++;
        Console.WriteLine($"Request {item.RpcRequestMessage.Id} succeeded: {item.GetResponse<string>()}");
    }
}

Console.WriteLine($"Success: {successCount}, Errors: {errorCount}");

API Reference

ClientBase

Abstract base class for client implementations:

public abstract class ClientBase : IClient
{
    public RequestInterceptor OverridingRequestInterceptor { get; set; }
    public TimeSpan ConnectionTimeout { get; set; }

    protected abstract Task<RpcResponseMessage> SendAsync(RpcRequestMessage request, string route = null);
    protected abstract Task<RpcResponseMessage[]> SendAsync(RpcRequestMessage[] requests);

    protected void HandleRpcError(RpcResponseMessage response, string reqMsg);
}

RpcRequest

Represents a JSON-RPC request:

public class RpcRequest
{
    public object Id { get; set; }
    public string Method { get; private set; }
    public object[] RawParameters { get; private set; }
}

RpcError

Represents a JSON-RPC error:

public class RpcError
{
    public int Code { get; set; }
    public string Message { get; set; }
    public object Data { get; set; }
}

Important Notes

Transport Implementations

This package provides abstractions only. Use concrete implementations:

Package Transport Use Case
Nethereum.JsonRpc.RpcClient HTTP/HTTPS Standard node communication
Nethereum.JsonRpc.WebSocketClient WebSocket Real-time subscriptions
Nethereum.JsonRpc.IpcClient IPC Local node communication
Nethereum.JsonRpc.SystemTextJsonRpcClient HTTP (System.Text.Json) .NET 6+ with System.Text.Json

Error Types

Exception Description
RpcResponseException Standard JSON-RPC error response
RpcClientTimeoutException Request exceeded ConnectionTimeout
RpcClientUnknownException Network or other unexpected errors

Batch Requests

  • Batch requests improve performance by reducing network round trips
  • Partial success requires AcceptPartiallySuccessful = true
  • Each item in the batch has independent error handling
  • Request IDs must be unique within a batch

Request Interception

Use RequestInterceptor for:

  • Logging all RPC requests/responses
  • Performance monitoring
  • Request/response transformation
  • Caching layer implementation
  • Authentication injection

Thread Safety

  • IClient implementations should be thread-safe after initialization
  • Connection pooling is managed by concrete implementations (e.g., RpcClient)
  • Request interceptors must be thread-safe if used concurrently

Core Abstractions

  • Nethereum.JsonRpc.Client - This package (abstraction layer)

Concrete Implementations

  • Nethereum.JsonRpc.RpcClient - HTTP/HTTPS client (Newtonsoft.Json)
  • Nethereum.JsonRpc.SystemTextJsonRpcClient - HTTP client (System.Text.Json)
  • Nethereum.JsonRpc.WebSocketClient - WebSocket client
  • Nethereum.JsonRpc.WebSocketStreamingClient - Streaming WebSocket client
  • Nethereum.JsonRpc.IpcClient - IPC client

Used By

  • Nethereum.RPC - High-level RPC services
  • Nethereum.Web3 - Complete Web3 API
  • All Nethereum client implementations

Additional Resources

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  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 is compatible.  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.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net451 is compatible.  net452 was computed.  net46 was computed.  net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (8)

Showing the top 5 NuGet packages that depend on Nethereum.JsonRpc.Client:

Package Downloads
Nethereum.RPC

Nethereum.RPC Ethereum Core RPC Class Library to interact via RPC with an Ethereum client, for example geth.

Nethereum.JsonRpc.RpcClient

JsonRpc Rpc Client Nethereum provider

Nethereum.JsonRpc.WebSocketClient

Nethereum.JsonRpc WebSocketClient

Nethereum.JsonRpc.IpcClient

Nethereum.JsonRpc IpcClient for NamedPipes and UnixSockets Class Library

Wonka.Eth

Relying heavily on the Nethereum project, this library contains classes that interact with the Ethereum foundation and that extend the Wonka engine, particulary the base class WonkaBizRulesEngine in the Wonka.BizRulesEngine library. With the funtionality provided here, Wonka becomes a business rules engine for both the .NET platform and the Ethereum platform, one that is inherently metadata-driven and serves as a reference implementation for EIP-2746. Once the rules are written into a markup language and are parsed/deserialized by the .NET form of the engine, these rules can then be serialized onto the blockchain using Nethereum, and stored within a smart contract (i.e., the Ethereum version of the engine) built using the Solidity language. The Ethereum version of this engine can also be deployed as a contract by this library. After providing a number of rules and populating a record, a user can submit the populated record for validation by the rules engine, whether it exists in .NET or the blockchain.

GitHub repositories (3)

Showing the top 3 popular GitHub repositories that depend on Nethereum.JsonRpc.Client:

Repository Stars
ChainSafe/web3.unity
🕹 Unity SDK for building games that interact with blockchains.
unoplatform/Uno.Samples
A collection of code samples for the Uno Platform
biheBlockChain/MyLinkToken
开源链克口袋,玩客币钱包
Version Downloads Last Updated
5.8.0 2,525 1/6/2026
5.0.0 306,104 5/28/2025
4.29.0 229,262 2/10/2025
4.28.0 73,478 1/7/2025
4.27.1 13,420 12/24/2024
4.27.0 2,143 12/24/2024
4.26.0 103,617 10/1/2024
4.25.0 25,064 9/19/2024
4.21.4 98,992 8/9/2024
4.21.3 13,704 7/22/2024
4.21.2 69,415 6/26/2024
4.21.1 3,151 6/26/2024
4.21.0 13,437 6/18/2024
4.20.0 319,521 3/28/2024
4.19.0 86,187 2/16/2024
4.18.0 303,325 11/21/2023
4.17.1 79,911 9/28/2023
4.17.0 18,503 9/27/2023
4.16.0 124,073 8/14/2023
4.15.2 128,556 7/11/2023
4.15.1 5,923 7/11/2023
4.15.0 6,417 7/11/2023
4.14.0 195,438 3/19/2023
4.13.0 135,203 2/18/2023
4.12.0 272,667 12/9/2022
4.11.0 178,325 10/27/2022
4.9.0 118,031 9/27/2022
4.8.0 183,044 8/24/2022
4.7.0 176,957 7/20/2022
4.6.1 144,719 6/18/2022
4.6.0 13,483 6/16/2022
4.5.0 411,377 5/13/2022
4.4.1 110,401 4/27/2022
4.4.0 17,439 4/27/2022
4.3.0 75,804 4/12/2022
4.2.0 179,974 2/18/2022
4.1.1 501,068 11/4/2021
4.1.0 33,476 10/15/2021
4.0.5 144,409 8/12/2021
4.0.4 10,891 8/10/2021
4.0.3 11,054 8/8/2021
4.0.2 10,028 8/5/2021
4.0.1 16,425 7/28/2021
4.0.0 21,444 7/26/2021
3.8.0 398,616 7/3/2020
3.7.1 120,956 2/13/2020
3.7.0 12,785 2/13/2020
3.6.0 36,504 1/27/2020
3.5.0 24,028 12/31/2019
3.4.0 155,471 7/29/2019
3.3.0 82,186 4/23/2019
3.2.0 47,660 4/8/2019
3.1.2 26,213 3/13/2019
3.1.1 8,981 3/12/2019
3.1.0 29,867 3/12/2019
3.0.0 41,980 11/28/2018
3.0.0-rc3 7,982 10/25/2018
3.0.0-rc2 5,848 10/24/2018
3.0.0-rc1 11,298 7/25/2018
2.5.1 108,463 6/5/2018
2.5.0 8,605 6/4/2018
2.4.0 55,130 3/11/2018
2.3.1 10,287 3/7/2018
2.3.0 8,581 3/6/2018
2.2.3 21,147 12/16/2017
2.2.2 7,237 12/16/2017
2.2.0 12,479 12/8/2017
2.1.0 13,958 10/23/2017
2.0.1 8,610 10/4/2017
2.0.0 10,480 9/26/2017
2.0.0-rc7 6,342 8/17/2017
2.0.0-rc6-2 5,783 7/29/2017
2.0.0-rc6.1 1,120 7/26/2017
2.0.0-rc5 4,714 6/19/2017
2.0.0-rc4 6,010 6/6/2017
2.0.0-rc3 5,525 4/11/2017
2.0.0-rc2-fix 5,059 4/6/2017
2.0.0-rc2 2,507 4/5/2017
1.0.6 8,584 2/3/2017
1.0.5 3,109 1/31/2017
1.0.4 7,056 12/10/2016
1.0.3 4,476 11/28/2016
1.0.2 4,652 11/21/2016
1.0.1 4,925 10/31/2016
1.0.0 15,845 9/14/2016
1.0.0-rc6 3,814 9/12/2016
1.0.0-rc5 6,989 8/1/2016
1.0.0-rc4 4,276 7/29/2016
1.0.0-rc1 3,370 3/30/2016