Interfacing .NET and Ethereum Blockchain Smart Contracts with Nethereum
--
.NET is the venerable framework that indie and enterprise developers alike have come to love. The ability to choose a variety of languages as well as deploy to a variety of platforms ranging from mobile to servers makes .NET a great choice for all kinds of applications. While .NET does a lot, it doesn’t do everything. For instance, one cannot write client side code to run in a browser in .NET (unless you use Silverlight…) nor is there currently a language supported by .NET that enables developers to write smart contracts.
The language of choice for smart contracts though is Solidity. Solidity is a purpose-built language that assumes many things about the environment it is running it, which pretty much marries it to blockchain technologies. It’s these baked in assumptions that conversely preclude other languages though.
Smart-contracts by themselves though are only half the story. For apps to be complete, smart contracts need something to call them by way of RPC’s. This is where Web3 clients come in. A Web3 client is simply the client side interface that enables client apps to interface with smart contracts running on Ethereum, so as long as one exists for a given language, then it can interface with Ethereum.
For .NET, really the only game in town for Ethereum is a project called Nethereum. (Nethereum is a portmanteau of .NET and Ethereum). This library attempts to replicate the same functionality provided by Web3.js available for JavaScript apps like those that run in a browser and with NodeJS
In this tutorial, we’ll deploy a smart contract to Ganache and then create a simple .NET app using .NET core to interface with the smart contract.
Create and Deploy a Smart Contract
- Download and install Ganache. Ganache is a personal blockchain useful for doing Ethereum development.
- In a terminal, command prompt, or Powershell session, install Truffle. Truffle is a framework and set of utilities that helps facilitate Solidity development of smart contracts. NPM is need to complete these commands.
npm install -g truffle
3. Create a folder and run truffle init
in the folder.
truffle init
4. Truffle will create a few new folders: contract
, test
, and migration
. In the contract folder, create a new file called…