There are
many great tools
available to create Ethereum Smart Contracts. It can be hard to choose between them. In this article, you will learn a simple workflow for developing Solidity smart contracts and calling their functions from C#. This workflow is well suited to .NET developers because it minimises the amount of new tools you need to know about. By using the excellent
Nethereum .NET
library you can continue to use the Visual Studio set of tools you are already familiar with.

Imagine your goal is to take the contract called
SimpleStorage.sol
from the Solidity documentation and call its functions from a C# project. Your preference is to use Visual Studio where possible. This article is based on my own experiences working on the Nethereum project to integrate Ethereum with SAP Sales and Distribution business processes.

Update Nov 2019: This workflow is well suited to situations where smart contracts are changing often (perhaps because you are developing them). When smart contracts are changing less often (perhaps because smart contracts are developed by another team) I have found I prefer the workflow detailed in section Alternative Workflow with VSCode at the end of this article. This is because like being able to explicitly control when regeneration happens. Both workflows use the same Nethereum code generation..

Workflow Overview

There are many possible workflows to achieve your goal, and as new versions of tools and plugins are released other options will appear. At the time of writing this workflow was found to be simple and quick:

The diagram above shows these steps:

Write Solidity smart contracts and compile them in Visual Studio Code. The output of the compilation process are some files representing the ABI and bytecode for the contracts.

Use the Nethereum Autogen code generator to automatically build C# API classes that provide access to the smart contracts.

Use Visual Studio to write C# to call methods in the generated C# API classes.
In this article the term
function
refers to a Solidity function and
method
refers to a C# method.

Initial Setup

Create Project

In a command prompt, you will create a new .NET core console project that you’ll use to hold all your files:

Save the file as
SimpleStorage.sol
in the root of the SimpleStorage folder.
The contract is from the
Solidity documentation
and you can see it is a very simple contract, just
set()
and
get()
functions. Now you are ready to begin the main developer workflow. The steps below correspond to the numbers on the diagram above.

Main Developer Workflow

Step 1 — Compile Smart Contract in Visual Studio Code

In Visual Studio Code, press Shift-Ctrl-P and choose “Solidity: Compile Current Solidity Contract” or press F5. You should see some new files appearing in the
SimpleStorage\bin
folder, most importantly
SimpleStorage.abi
and
SimpleStorage.bin
.

Step 2 — Rebuild the C# Project in Visual Studio

Open the solution
DevWorkflowExample.sln
in Visual Studio (not Visual Studio Code).
Right-click on the SimpleStorage project and choose “Rebuild”. The act of rebuilding the project triggers the
Nethereum.Autogen.ContractApi
package to build the C# API classes to let you interact with the
SimpleStorage.sol
contract. You should see a collection of new files added to the project in a folder called
SimpleStorage
like this:

Notice the C# method naming is different for the
set()
and
get()
function calls. This is because
set()
changes a value on the blockchain, so it costs Ether, so it has be called using an Ethereum transaction, and will return a receipt. The
get()
function doesn't change any values on the blockchain, so it is a simple call and is free (no transaction and no receipt).

Step 3 — Call Smart Contract functions from C# in Visual Studio

Now you can call functions from your smart contract in the .NET core console program, by making calls to the generated C# classes mentioned in the previous section. For example, paste the code below into Program.cs, replacing everything that is currently there.

Build the project.
The workflow is done! You can now make further edits to the smart contract in Visual Studio Code, compile it there, and simply rebuild the project in Visual Studio to be able to make C# calls to the amended Solidity functions.

Program Execution

Of course, you’d like to check that the program runs successfully. For the project to run, it needs to speak to a blockchain and here you do need a new tool. A good option during development is to run a local blockchain as described here:
https://github.com/Nethereum/TestChains#geth
.
With a local blockchain running, run the
SimpleStorage
console project from Visual Studio. You should get output like below:

Where to go from here

The next step in your development process would probably be to add some tests for your Solidity contract. Does this mean you absolutely have to go off and learn Truffle or some other tooling? The answer is no, you don’t. There is an
example here
of using XUnit test fixtures to launch a local chain before running tests to deploy contracts and call functions.
Note, you don’t have to use the generated
SimpleStorageService
class to call your smart contract's functions. At the very least, though, it is instructive to see how the calls work in the generated code.

Alternative Workflow with VSCode

Update Nov 2019: As mentioned, the workflow detailed above I have found useful when the smart contracts are changing often and you want the C# classes to reflect these changes often. This suited the case where I was developing smart contracts and the C# at the same time.
In cases where the smart contracts are stable (e.g. you have been sent ABI and bytecode by another development team) I have found I prefer to explicitly control when regeneration happens. This can be done by using VSCode not just to write the smart contracts but also to generate the necessary C# code. The workflow is well explained over on the Nethereum documentation site.

Article Author

0 Comments

Related Articles

Smart contracts integration with Nethereum

The purpose of this article is to help .NET developers leverage Nethereum, An open source .NET integration library for blockchain. You can execute this tutorial as a workbook, or download a simplified sample here The purpose of this sample is the following: Understanding how to create contract deployment, function and event definitions to interact with a smart contracts Creating an account object using a private key, this will allow to sign transactions offline. Deploying a smart contract (the s

Signing messages with Nethereum

The purpose of this article is to help .NET developers leverage Nethereum, An open source .NET integration library for blockchain. This document also exists as a Workbook , find more about workbooks installation requirements here Nethereum provides methods to sign messages in an Ethereum compatible format. The following is a quick guide to signing a string with Nethereum and verifying a signature using various methods. Ethereum signing basics In the Ethereum context, signing a message allows us