![]() |
VOOZH | about |
dotnet add package Nethereum.JsonRpc.RpcClient --version 6.1.0
NuGet\Install-Package Nethereum.JsonRpc.RpcClient -Version 6.1.0
<PackageReference Include="Nethereum.JsonRpc.RpcClient" Version="6.1.0" />
<PackageVersion Include="Nethereum.JsonRpc.RpcClient" Version="6.1.0" />Directory.Packages.props
<PackageReference Include="Nethereum.JsonRpc.RpcClient" />Project file
paket add Nethereum.JsonRpc.RpcClient --version 6.1.0
#r "nuget: Nethereum.JsonRpc.RpcClient, 6.1.0"
#:package Nethereum.JsonRpc.RpcClient@6.1.0
#addin nuget:?package=Nethereum.JsonRpc.RpcClient&version=6.1.0Install as a Cake Addin
#tool nuget:?package=Nethereum.JsonRpc.RpcClient&version=6.1.0Install as a Cake Tool
Production-ready HTTP/HTTPS JSON-RPC client for Ethereum node communication.
Nethereum.JsonRpc.RpcClient provides the standard HTTP/HTTPS transport implementation for communicating with Ethereum nodes via JSON-RPC. This is the most commonly used RPC client in Nethereum, offering robust connection management, automatic retries, authentication support, and production-tested reliability.
Key Features:
Use Cases:
dotnet add package Nethereum.JsonRpc.RpcClient
This is typically the default RPC client used by Nethereum.Web3:
dotnet add package Nethereum.Web3
Nethereum:
Framework:
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth;
// Connect to local node
var client = new RpcClient(new Uri("http://localhost:8545"));
// Use with RPC services
var ethBlockNumber = new EthBlockNumber(client);
var blockNumber = await ethBlockNumber.SendRequestAsync();
Console.WriteLine($"Current block: {blockNumber.Value}");
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth;
using Nethereum.Hex.HexTypes;
// Local Geth/Erigon/Besu node
var client = new RpcClient(new Uri("http://localhost:8545"));
// Infura
var infuraClient = new RpcClient(
new Uri("https://mainnet.infura.io/v3/YOUR_PROJECT_ID")
);
// Alchemy
var alchemyClient = new RpcClient(
new Uri("https://eth-mainnet.g.alchemy.com/v2/YOUR_API_KEY")
);
// Use with any RPC service
var ethChainId = new EthChainId(client);
HexBigInteger chainId = await ethChainId.SendRequestAsync();
Console.WriteLine($"Chain ID: {chainId.Value}");
// Output: Chain ID: 1 (mainnet)
using Nethereum.JsonRpc.Client;
using System.Net.Http.Headers;
using System.Text;
// Option 1: URL-based authentication (simplest)
var client = new RpcClient(
new Uri("http://username:password@localhost:8545")
);
// Option 2: Explicit AuthenticationHeaderValue
var credentials = Convert.ToBase64String(
Encoding.UTF8.GetBytes("admin:secretpassword")
);
var authHeader = new AuthenticationHeaderValue("Basic", credentials);
var authenticatedClient = new RpcClient(
new Uri("http://localhost:8545"),
authHeaderValue: authHeader
);
// Use authenticated client
var ethAccounts = new EthAccounts(authenticatedClient);
var accounts = await ethAccounts.SendRequestAsync();
Console.WriteLine($"Accounts: {string.Join(", ", accounts)}");
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth;
var client = new RpcClient(new Uri("http://localhost:8545"));
// Default timeout is 120 seconds (2 minutes)
Console.WriteLine($"Default timeout: {client.ConnectionTimeout.TotalSeconds}s");
// Set custom timeout for slow networks
client.ConnectionTimeout = TimeSpan.FromSeconds(30);
try
{
var ethGasPrice = new EthGasPrice(client);
var gasPrice = await ethGasPrice.SendRequestAsync();
Console.WriteLine($"Gas price: {gasPrice.Value} wei");
}
catch (RpcClientTimeoutException ex)
{
Console.WriteLine($"Request timed out after 30 seconds: {ex.Message}");
}
using Nethereum.JsonRpc.Client;
using System.Net.Http;
// Create custom HttpClient with specific settings
var httpClient = new HttpClient(new SocketsHttpHandler
{
PooledConnectionLifetime = TimeSpan.FromMinutes(15),
PooledConnectionIdleTimeout = TimeSpan.FromMinutes(10),
MaxConnectionsPerServer = 50,
EnableMultipleHttp2Connections = true
})
{
Timeout = TimeSpan.FromSeconds(60)
};
// Create RpcClient with custom HttpClient
var client = new RpcClient(
new Uri("http://localhost:8545"),
httpClient: httpClient
);
var ethBlockNumber = new EthBlockNumber(client);
var blockNumber = await ethBlockNumber.SendRequestAsync();
Console.WriteLine($"Block: {blockNumber.Value}");
using Nethereum.JsonRpc.Client;
using System.Net;
using System.Net.Http;
// Configure proxy
var handler = new HttpClientHandler
{
Proxy = new WebProxy("http://proxy.example.com:8080")
{
Credentials = new NetworkCredential("proxyuser", "proxypass")
},
UseProxy = true,
MaxConnectionsPerServer = 20
};
// Create client with proxy handler
var client = new RpcClient(
new Uri("http://localhost:8545"),
httpClientHandler: handler
);
var ethChainId = new EthChainId(client);
var chainId = await ethChainId.SendRequestAsync();
Console.WriteLine($"Chain ID (via proxy): {chainId.Value}");
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth.DTOs;
using System.Threading.Tasks;
var client = new RpcClient(new Uri("http://localhost:8545"));
// RpcClient is thread-safe - can handle concurrent requests
var tasks = new List<Task>
{
Task.Run(async () =>
{
var ethBlockNumber = new EthBlockNumber(client);
var block = await ethBlockNumber.SendRequestAsync();
Console.WriteLine($"Task 1 - Block: {block.Value}");
}),
Task.Run(async () =>
{
var ethGasPrice = new EthGasPrice(client);
var gasPrice = await ethGasPrice.SendRequestAsync();
Console.WriteLine($"Task 2 - Gas Price: {gasPrice.Value}");
}),
Task.Run(async () =>
{
var ethChainId = new EthChainId(client);
var chainId = await ethChainId.SendRequestAsync();
Console.WriteLine($"Task 3 - Chain ID: {chainId.Value}");
})
};
await Task.WhenAll(tasks);
Console.WriteLine("All requests completed successfully");
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth;
using Polly;
var client = new RpcClient(new Uri("http://localhost:8545"));
client.ConnectionTimeout = TimeSpan.FromSeconds(10);
// Define retry policy with Polly
var retryPolicy = Policy
.Handle<RpcClientTimeoutException>()
.Or<RpcClientUnknownException>()
.WaitAndRetryAsync(
retryCount: 3,
sleepDurationProvider: attempt => TimeSpan.FromSeconds(Math.Pow(2, attempt)),
onRetry: (exception, timeSpan, retryCount, context) =>
{
Console.WriteLine($"Retry {retryCount} after {timeSpan.TotalSeconds}s due to: {exception.Message}");
}
);
try
{
var blockNumber = await retryPolicy.ExecuteAsync(async () =>
{
var ethBlockNumber = new EthBlockNumber(client);
return await ethBlockNumber.SendRequestAsync();
});
Console.WriteLine($"Success! Block: {blockNumber.Value}");
}
catch (RpcResponseException ex)
{
Console.WriteLine($"RPC Error {ex.RpcError.Code}: {ex.RpcError.Message}");
}
catch (RpcClientTimeoutException ex)
{
Console.WriteLine($"Timeout after retries: {ex.Message}");
}
catch (RpcClientUnknownException ex)
{
Console.WriteLine($"Network error: {ex.Message}");
}
using Nethereum.JsonRpc.Client;
using Nethereum.RPC.Eth;
public class LoadBalancedRpcClient
{
private readonly List<RpcClient> _clients;
private int _currentIndex = 0;
private readonly object _lock = new object();
public LoadBalancedRpcClient(params string[] nodeUrls)
{
_clients = nodeUrls.Select(url => new RpcClient(new Uri(url))).ToList();
}
public RpcClient GetNextClient()
{
lock (_lock)
{
var client = _clients[_currentIndex];
_currentIndex = (_currentIndex + 1) % _clients.Count;
return client;
}
}
}
// Usage
var loadBalancer = new LoadBalancedRpcClient(
"http://node1.example.com:8545",
"http://node2.example.com:8545",
"http://node3.example.com:8545"
);
// Round-robin requests
for (int i = 0; i < 10; i++)
{
var client = loadBalancer.GetNextClient();
var ethBlockNumber = new EthBlockNumber(client);
var block = await ethBlockNumber.SendRequestAsync();
Console.WriteLine($"Request {i + 1} - Block: {block.Value}");
}
using Nethereum.Web3;
using Nethereum.JsonRpc.Client;
// Option 1: Web3 creates RpcClient internally (simplest)
var web3 = new Web3("https://mainnet.infura.io/v3/YOUR_PROJECT_ID");
// Option 2: Create custom RpcClient first
var client = new RpcClient(new Uri("http://localhost:8545"));
client.ConnectionTimeout = TimeSpan.FromSeconds(60);
var web3WithCustomClient = new Web3(client);
// Use Web3 normally
var balance = await web3WithCustomClient.Eth.GetBalance.SendRequestAsync("0x742d35Cc6634C0532925a3b844Bc9e7595f0bEb");
Console.WriteLine($"Balance: {Web3.Convert.FromWei(balance)} ETH");
var blockNumber = await web3WithCustomClient.Eth.Blocks.GetBlockNumber.SendRequestAsync();
Console.WriteLine($"Block: {blockNumber.Value}");
// Basic constructor
public RpcClient(Uri baseUrl,
AuthenticationHeaderValue authHeaderValue = null,
JsonSerializerSettings jsonSerializerSettings = null,
HttpClientHandler httpClientHandler = null,
ILogger log = null)
// With custom HttpClient
public RpcClient(Uri baseUrl,
HttpClient httpClient,
AuthenticationHeaderValue authHeaderValue = null,
JsonSerializerSettings jsonSerializerSettings = null,
ILogger log = null)
public static int MaximumConnectionsPerServer { get; set; } = 20;
public TimeSpan ConnectionTimeout { get; set; } // Default: 120 seconds
public RequestInterceptor OverridingRequestInterceptor { get; set; }
Task<T> SendRequestAsync<T>(RpcRequest request, string route = null);
Task<T> SendRequestAsync<T>(string method, string route = null, params object[] paramList);
Task<RpcRequestResponseBatch> SendBatchRequestAsync(RpcRequestResponseBatch batch);
Task<RpcResponseMessage> SendAsync(RpcRequestMessage request, string route = null);
HttpClient Rotation:
MaximumConnectionsPerServer)Best Practices:
| Operation | Latency | Notes |
|---|---|---|
| Local node | 1-10ms | Localhost Geth/Erigon |
| Cloud provider | 50-200ms | Infura, Alchemy, QuickNode |
| Slow network | 200-500ms | High latency regions |
Optimization Tips:
EnableMultipleHttp2ConnectionsMaxConnectionsPerServer for high throughput| Exception | Cause | Retry? |
|---|---|---|
| RpcClientTimeoutException | Request exceeded ConnectionTimeout | Yes (with backoff) |
| RpcClientUnknownException | Network/HTTP errors | Yes (transient) |
| RpcResponseException | JSON-RPC error from node | Depends on error code |
| HttpRequestException | DNS, connection failures | Yes (with backoff) |
Supports HTTP Basic Authentication:
http://user:pass@localhost:8545AuthenticationHeaderValue("Basic", base64Credentials)Uses Newtonsoft.Json with default settings optimized for Ethereum:
0x hex prefixesFor System.Text.Json, use Nethereum.JsonRpc.SystemTextJsonRpcClient instead.
Supports Microsoft.Extensions.Logging:
var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole());
var logger = loggerFactory.CreateLogger<RpcClient>();
var client = new RpcClient(
new Uri("http://localhost:8545"),
log: logger
);
Logs include:
| Provider | URL Format | Notes |
|---|---|---|
| Infura | https://mainnet.infura.io/v3/PROJECT_ID |
Free tier available |
| Alchemy | https://eth-mainnet.g.alchemy.com/v2/API_KEY |
Enhanced APIs |
| QuickNode | https://your-endpoint.quiknode.pro/token/ |
Global network |
| Local Geth | http://localhost:8545 |
Full node |
| Local Erigon | http://localhost:8545 |
Archive node |
| 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.RpcClient:
| Package | Downloads |
|---|---|
|
Nethereum.Web3
Nethereum.Web3 Ethereum Web3 Class Library to interact via RPC with an Ethereum client, for example geth. Including contract interaction, deployment, transaction, encoding / decoding and event filters |
|
|
Nethereum.BlockchainProcessing
Nethereum.BlockchainProcessing Ethereum blockchain processing allowing to crawl Blocks, Transactions, TransactionReceipts and Logs (Event) for storage and / or using custom handlers like queuing , search, etc |
|
|
Nethereum.Web3Lite
Nethereum.Web3Lite Ethereum Web3 Class Library (light browser version, with no reference to signing crypto libraries) to interact via RPC with an Ethereum client, for example geth. Including contract interaction, deployment, transaction, encoding / decoding and event filters |
|
|
Aoite.Neth
The aoite ethereum module. |
|
|
WalletConnect.NEthereum
An NEthereum extension to access the WalletConnect protocol through a Web3 Provider. A lightweight C# implementation of the WalletConnect protocol that can be used to connect to external wallets or connect a wallet to an external Dapp |
Showing the top 4 popular GitHub repositories that depend on Nethereum.JsonRpc.RpcClient:
| Repository | Stars |
|---|---|
|
yc-l/yc.boilerplate
YC. Boilerplate is a set of loose coupling, flexible combination, complete functions, convenient development, and reduces the workload of development.
|
|
|
unoplatform/Uno.Samples
A collection of code samples for the Uno Platform
|
|
|
JayArrowz/PancakeTokenSniper
BSC BNB Pancake token sniper, buy, take profit and rug check
|
|
|
biheBlockChain/MyLinkToken
开源链克口袋,玩客币钱包
|
| Version | Downloads | Last Updated |
|---|---|---|
| 6.1.0 | 48,127 | 3/25/2026 |
| 6.0.4 | 10,085 | 3/18/2026 |
| 6.0.3 | 1,558 | 3/18/2026 |
| 6.0.1 | 2,469 | 3/17/2026 |
| 6.0.0 | 3,676 | 3/16/2026 |
| 5.8.0 | 78,889 | 1/6/2026 |
| 5.0.0 | 408,428 | 5/28/2025 |
| 4.29.0 | 296,788 | 2/10/2025 |
| 4.28.0 | 76,760 | 1/7/2025 |
| 4.27.1 | 13,759 | 12/24/2024 |
| 4.27.0 | 7,257 | 12/24/2024 |
| 4.26.0 | 102,960 | 10/1/2024 |
| 4.25.0 | 33,258 | 9/19/2024 |
| 4.21.4 | 127,684 | 8/9/2024 |
| 4.21.3 | 11,377 | 7/22/2024 |
| 4.21.2 | 75,604 | 6/26/2024 |
| 4.21.1 | 3,568 | 6/26/2024 |
| 4.21.0 | 17,285 | 6/18/2024 |
| 4.20.0 | 371,054 | 3/28/2024 |
| 4.19.0 | 90,638 | 2/16/2024 |