Ethereum
准备好在生产环境中调用了吗?
免费套餐涵盖个人项目。按量付费,无需绑卡即可扩展。
Ethereum
免费套餐涵盖个人项目。按量付费,无需绑卡即可扩展。
Nethereum is the de-facto way to reach Ethereum from .NET. It targets .NET Standard 2.0 and up, so the same code talks to chain 1 from a console tool, an ASP.NET Core API, a Blazor front end, a Xamarin app, or a Unity3D game. One dotnet add package Nethereum.Web3 pulls in the JSON-RPC client, ABI encoder, account handling, and the wei/ether conversion helpers you need to query a balance or sign a transaction against https://ethereum.therpc.io/YOUR_API_KEY.
Because Nethereum sits on .NET Standard 2.0+, a single dependency covers Unity3D, Xamarin, ASP.NET Core, and Blazor without per-platform forks. Install it from NuGet with dotnet add package Nethereum.Web3 and you are ready to point at Ethereum mainnet.
Pass the endpoint URL straight into new Web3(url) and every call routes over JSON-RPC to Ethereum. Each network method returns a Task — GetBalance.SendRequestAsync returns a BigInteger in wei, which Web3.Convert.FromWei turns into ETH. Always await these. Blocking on .Result or .Wait() inside an ASP.NET request can deadlock the synchronization context, so keep the call chain async all the way down.
eth_subscribe on wss://ethereum.therpc.io/YOUR_API_KEYHand the ABI string and a deployed address to _web3.Eth.GetContract(abi, contractAddress) and Nethereum gives you a Contract you can drive by function name. GetFunction("balanceOf").CallAsync<BigInteger>(...) runs an eth_call against the latest block — a read that costs no gas and returns immediately. State-changing calls go through SendTransactionAsync, which submits a signed transaction and hands back the hash you can track on etherscan.io. This is the pattern you use for an ERC-20 transfer or an Aave deposit alike.
Register EthereumService once in Program.cs (or Startup) — usually as a singleton, since the Web3 client is cheap to share and reuses its HTTP connection to the endpoint. Controllers then receive it through constructor injection, as the example below shows. Wrap each await in a try/catch: a malformed address or a transient RPC hiccup surfaces as an exception, and returning BadRequest(ex.Message) keeps a single bad request from bubbling up as a 500.
To watch a contract's logs, describe the event as a DTO and pass it to GetEvent<TEventDTO>(contractAddress). CreateFilterInput() builds the topic filter Ethereum expects, and polling GetFilterChanges returns decoded events — a Transfer from an ERC-20 token, for instance — as strongly typed objects rather than raw hex. For a live push instead of polling, run the same filter over the WebSocket endpoint and subscribe to logs.
In a Unity game, build the Web3 instance once in Awake() or Start() pointing at the Ethereum endpoint, then keep it for the object's lifetime. The catch is MonoBehaviour: Unity only lets you mark Start() as async void, and async void swallows exceptions if you forget the try/catch. Keep the heavy lifting in async Task helpers and call them from Start() or a coroutine, so an unhandled RPC error logs through Debug.LogError instead of silently killing the frame.