![]() |
VOOZH | about |
dotnet add package Nethereum.JsonRpc.Client --version 6.1.0
NuGet\Install-Package Nethereum.JsonRpc.Client -Version 6.1.0
<PackageReference Include="Nethereum.JsonRpc.Client" Version="6.1.0" />
<PackageVersion Include="Nethereum.JsonRpc.Client" Version="6.1.0" />Directory.Packages.props
<PackageReference Include="Nethereum.JsonRpc.Client" />Project file
paket add Nethereum.JsonRpc.Client --version 6.1.0
#r "nuget: Nethereum.JsonRpc.Client, 6.1.0"
#:package Nethereum.JsonRpc.Client@6.1.0
#addin nuget:?package=Nethereum.JsonRpc.Client&version=6.1.0Install as a Cake Addin
#tool nuget:?package=Nethereum.JsonRpc.Client&version=6.1.0Install as a Cake Tool
Core JSON-RPC abstraction layer for Ethereum node communication.
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:
Use Cases:
dotnet add package Nethereum.JsonRpc.Client
Note: This is typically used as a dependency by concrete client implementations. Most users will use:
Nethereum:
External:
JSON Serialization (Peer Dependencies):
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}");
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);
}
Base interface with common properties:
public interface IBaseClient
{
RequestInterceptor OverridingRequestInterceptor { get; set; }
TimeSpan ConnectionTimeout { get; set; }
}
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)}");
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}");
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>()}");
}
}
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
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}");
}
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}");
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}");
}
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);
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}");
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);
}
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; }
}
Represents a JSON-RPC error:
public class RpcError
{
public int Code { get; set; }
public string Message { get; set; }
public object Data { get; set; }
}
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 |
| Exception | Description |
|---|---|
| RpcResponseException | Standard JSON-RPC error response |
| RpcClientTimeoutException | Request exceeded ConnectionTimeout |
| RpcClientUnknownException | Network or other unexpected errors |
AcceptPartiallySuccessful = trueUse RequestInterceptor for:
IClient implementations should be thread-safe after initialization| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 is compatible. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 is compatible. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 is compatible. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 is compatible. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net451 net451 is compatible. net452 net452 was computed. net46 net46 was computed. net461 net461 is compatible. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
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. |
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 |
|---|---|---|
| 6.1.0 | 50,235 | 3/25/2026 |
| 6.0.4 | 10,659 | 3/18/2026 |
| 6.0.3 | 2,101 | 3/18/2026 |
| 6.0.1 | 3,304 | 3/17/2026 |
| 6.0.0 | 4,252 | 3/16/2026 |
| 5.8.0 | 83,316 | 1/6/2026 |
| 5.0.0 | 411,471 | 5/28/2025 |
| 4.29.0 | 312,321 | 2/10/2025 |
| 4.28.0 | 88,392 | 1/7/2025 |
| 4.27.1 | 14,726 | 12/24/2024 |
| 4.27.0 | 7,766 | 12/24/2024 |
| 4.26.0 | 110,774 | 10/1/2024 |
| 4.25.0 | 34,855 | 9/19/2024 |
| 4.21.4 | 129,693 | 8/9/2024 |
| 4.21.3 | 13,929 | 7/22/2024 |
| 4.21.2 | 76,367 | 6/26/2024 |
| 4.21.1 | 4,133 | 6/26/2024 |
| 4.21.0 | 20,254 | 6/18/2024 |
| 4.20.0 | 374,311 | 3/28/2024 |
| 4.19.0 | 109,466 | 2/16/2024 |