BNB Smart Chain

BNB Smart Chain

C#

Nethereum is the established .NET gateway to BNB Smart Chain. By building against .NET Standard 2.0, it slots into nearly every corner of the ecosystem — Unity3D games, Xamarin mobile apps, ASP.NET Core backends, and Blazor front-ends can all read BNB balances and call BEP-20 contracts on chain 56 through the same API. Pull it into a project with dotnet add package Nethereum.Web3.

That .NET Standard 2.0 baseline is what gives Nethereum its reach across Unity3D, Xamarin, ASP.NET Core, and Blazor without per-platform forks. Installation never varies: dotnet add package Nethereum.Web3 pulls it from NuGet, and from there the connection code to BSC is identical regardless of which host you target.

Nethereum

A single constructor call connects you: new Web3("https://bsc.therpc.io/YOUR_API_KEY") hands you a client bound to BNB Smart Chain. Every I/O operation it exposes returns a Task, so always await your balance reads and transaction sends on chain 56. Resist the temptation to call .Result or .Wait() to make them synchronous: on a context with a synchronization context, like ASP.NET or Unity's main thread, that pattern deadlocks.

using Nethereum.Web3;
using Nethereum.Web3.Accounts;
using Nethereum.Util;
using Nethereum.Hex.HexTypes;
public class EthereumService
{
private readonly Web3 _web3;
public EthereumService(string url)
{
_web3 = new Web3(url);
}
public async Task<decimal> GetBalanceAsync(string address)
{
var balance = await _web3.Eth.GetBalance.SendRequestAsync(address);
return Web3.Convert.FromWei(balance.Value);
}
public async Task<string> SendTransactionAsync(
string privateKey,
string toAddress,
decimal etherAmount)
{
var account = new Account(privateKey);
var web3 = new Web3(account, _web3.Client.Url);
var transaction = await web3.Eth.GetEtherTransferService()
.TransferEtherAsync(toAddress, etherAmount);
return transaction;
}
}
  • GitHub: https://github.com/Nethereum/Nethereum
  • Docs: https://docs.nethereum.com
  • Full BNB Smart Chain integration across the .NET stack
  • Smart-contract deployment and interaction for BEP-20 and beyond
  • HD wallet generation and key management
  • Targets Unity3D and Xamarin alongside server runtimes
  • Transports for IPC, RPC, and WebSocket connections to chain 56

Smart Contract Integration

Obtain a contract handle by passing the ABI and deployed address to _web3.Eth.GetContract(abi, contractAddress) — for instance to wrap a BEP-20 token on chain 56. From that handle you grab individual functions with GetFunction, then choose the call shape by intent: CallAsync<T> runs a read-only view and decodes the result into your chosen type, while SendTransactionAsync broadcasts a signed state-changing call to BNB Smart Chain.

public class SmartContractService
{
private readonly Web3 _web3;
private readonly Contract _contract;
public SmartContractService(string url, string contractAddress, string abi)
{
_web3 = new Web3(url);
_contract = _web3.Eth.GetContract(abi, contractAddress);
}
public async Task<T> CallFunctionAsync<T>(string functionName, params object[] parameters)
{
var function = _contract.GetFunction(functionName);
return await function.CallAsync<T>(parameters);
}
public async Task<string> ExecuteFunctionAsync(
string functionName,
string fromAddress,
params object[] parameters)
{
var function = _contract.GetFunction(functionName);
return await function.SendTransactionAsync(fromAddress, parameters);
}
}

ASP.NET Integration

In ASP.NET Core, register your EthereumService wrapper with the dependency-injection container in Program.cs (or Startup) so controllers receive it through constructor injection rather than newing up a Web3 per request. Inside each action, guard the awaited BSC call with a try/catch: return the data on success, and on a failure talking to chain 56 translate the exception into a BadRequest with a clear message instead of letting it bubble up as a 500.

[ApiController]
[Route("api/[controller]")]
public class EthereumController : ControllerBase
{
private readonly EthereumService _ethereumService;
public EthereumController(EthereumService ethereumService)
{
_ethereumService = ethereumService;
}
[HttpGet("balance/{address}")]
public async Task<ActionResult<decimal>> GetBalance(string address)
{
try
{
var balance = await _ethereumService.GetBalanceAsync(address);
return Ok(balance);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
}

Event Handling

To track contract activity on BNB Smart Chain, model the log as a strongly typed event DTO and fetch its handler with GetEvent<TEventDTO>. Build a CreateFilterInput to scope which logs you want — by contract address and block range — and Nethereum decodes each matching entry into your DTO. That turns raw BEP-20 Transfer logs on chain 56 into ordinary C# objects you can query and store.

public class EventMonitorService
{
private readonly Web3 _web3;
public async Task MonitorEventsAsync(string contractAddress, string eventName)
{
var filterAll = _web3.Eth.GetEvent<YourEventDTO>(contractAddress)
.CreateFilterInput();
var subscription = _web3.Eth.GetEvent<YourEventDTO>(contractAddress)
.GetFilterChanges(filterAll);
subscription.Subscribe(evt =>
{
Console.WriteLine($"New event: {evt.Event}");
});
}
}

Unity3D Integration

In a Unity3D project, create your Web3 instance once during Awake() or Start() with the BSC endpoint and hold it on the MonoBehaviour, rather than rebuilding it each frame. Be deliberate about async: async void event handlers swallow exceptions and are hard to cancel when a scene unloads, so prefer Task-returning helper methods and await them from a controlled entry point — invoked out of Start() or marshalled through a coroutine — so chain-56 calls never stall the game loop.

public class EthereumUnityManager : MonoBehaviour
{
private Web3 _web3;
private async void Start()
{
_web3 = new Web3("https://bsc.therpc.io/YOUR_API_KEY");
await InitializeWalletAsync();
}
private async Task InitializeWalletAsync()
{
try
{
var balance = await _web3.Eth.GetBalance.SendRequestAsync("YOUR_ADDRESS");
Debug.Log($"Wallet Balance: {Web3.Convert.FromWei(balance.Value)} BNB");
}
catch (Exception ex)
{
Debug.LogError($"Failed to initialize wallet: {ex.Message}");
}
}
}

Ready to call this in production?

Free tier covers personal projects. Pay-as-you-go scales without a card.