. .NET is an ancient framework loved by both indie and enterprise developers. The ability to choose from a variety of languages and deploy to a wide variety of platforms, including a wide range of capabilities from mobile to server, makes . .NET becomes a great choice for a variety of applications. Although. .NET does a lot, but it can also be said that it does nothing. For example, you can't use . .NET (unless using Silverlight...... ), nor does it exist. .NET supported languages, which enables developers to write smart contracts.
The development language of smart contracts is solidity. Solidity is a purpose-specific language that assumes a lot of things about the environment in which it operates, and it is very closely integrated with blockchain technology. It is these hypothetical features that exclude other languages.
The smart contract itself is only half the story. For applications, smart contracts need to call them via RPC. This is where Web3 clients come in. A Web3 client is simply a client application that enables the client application to interface with smart contracts running on Ethereum, so as long as a given language exists, then it can interface with Ethereum.
For . The most important thing for .NET is a project called Nethereum. (Nethereum is a portal for .NET and Ethereum at the address:https://nethereum.com/)。 This library attempts to replicate the same functionality provided by Web3.js, similar to JavaScript applications running in browsers and NodeJS.
In this tutorial, we'll deploy a smart contract to Ganache and then use . NET core creates a simple . .NET applications to interface with smart contracts.
Create and publish a smart contract
1. Download and install Ganache, a very useful private blockchain for Ethereum development.
The hyperlink login is visible.
2. In a terminal, command prompt, or Powershell session, install Truffle. Truffle is a framework and set of utility tools that help facilitate solidity development of smart contracts. Use NPM to complete the command below.
3. Create a folder and run truffle init under this folder
4. Truffle will create some new folders contract, test, and migration. Create a new file Vote.sol in the contracts folder.
5. Paste the following code into the newly created Vote.sol and save the file. This smart contract only records the number of votes cast by 2 candidates. The contract uses the message sender (i.e., the account address) as the voter. It only allows 1 vote per account.
6. Create a new file called 2_vote.js in the migrations folder.
7. Copy the following code into the 2_vote.js and save it.
8. Open truffle.js in the root directory of the folder you created and paste the code below, then save the file. This configuration of Truffle uses Ganache.
9. Launch Ganache in the Start menu.
10. Deploy smart contracts with Truffle. The ganache network is defined in truffle.js file.
11. Pay attention to the output. You'll see an output similar to the one below. This is the address of the voting contract. Copy and paste the hex string for later use.
Create a . .NET applications call smart contracts
1. In a new directory, create a new console application using the dotnet command.
2. Install the Nethereum package.
3. Edit Program.cs file. Replace the default code with the code below. This will allow the console to prompt account addresses and user votes. Save the file.
4. On program.cs, paste the address we just recorded to the address of the deployment contact and save the file.
5. Compile the application.
6. Run the app.
7. You'll see the app prompt you to type. Enter the address and vote. You can copy the account address from the Ganache GUI under the Accounts tab.
8. Run the app again and you will see that the vote has increased.
For Nethereum, it's easy to integrate smart contracts with any . .NET applications. Because Nethereum is based on .NET, it can be used for .NET core applications, . .NET standard, Xamarin, and various Windows applications. Using Nethereum, the mighty Ethereum and . NET is now at your disposal!
|