Before you can start developing chaincode (a.k.a. Smart Contract) for Hyperledger Fabric with .NET Core, you need to install some prerequisites described in this blog post.
Please note, that .NET Core is not yet officially supported by Hyperledger Fabric.
Since we're going to develop a new chaincode, which will involve re-compiling and re-running the chaincode over and over again, we're using Hyperledger Fabric's Chaincode Development Mode. This mode allows the user/developer to start the chaincode. Otherwise the chaincode will be started and maintained by the peer.
To get the Chaincode Development Mode up & running, we need to get the Hyperledger Fabric Samples. Please make sure to follow the steps described on the samples page to install them. Additionally, you need to install .NET Core SDK. For an IDE you can either use JetBrains Rider or Visual Studio Code.
After grabbing the Hyperledger samples, there's a folder
chaincode-docker-devmode. Find the file
docker-compose-simple.yml and open it in a text editor. Within the file, find the ports for peer and add the mapping
7052:7052. This opens the port for GRPC requests outside of the container environment. The current
fabric-ccenv (the chaincode execution environment for Hyperledger) does not allow the execution of .NET Core, since the runtime is not pre-installed in the docker container. For development, our chaincode will run on the host machine making requests into the docker containers. After adding the ports, peer will look like this:
peer: container_name: peer image: hyperledger/fabric-peer environment: - CORE_PEER_ID=peer - CORE_PEER_ADDRESS=peer:7051 - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer:7051 - CORE_PEER_LOCALMSPID=DEFAULT - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock - CORE_LOGGING_LEVEL=DEBUG - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp volumes: - /var/run/:/host/var/run/ - ./msp:/etc/hyperledger/msp working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer command: peer node start --peer-chaincodedev=true -o orderer:7050 ports: - 7051:7051 - 7052:7052 - 7053:7053 depends_on: - orderer
Side note: If you want to deploy your chaincode within a container, an example
Dockerfileis available here. It installs .NET Core within the
fabric-ccenvto allow the execution of our .NET Core chaincode within the container. For the rest of this blog post, this is not needed, so you can skip this step safely.
After that, we're ready to start the development environment. Head over the
chaincode-docker-devmode folder within a terminal and start it via
docker-compose -f docker-compose-simple.yaml up. After some seconds the development environment will be up and running.
Open a second terminal and execute
docker exec -it cli bash. This will start a bash within the CLI of the Hyperledger Fabric environment we need to install and instantiate our chaincode later on.
For now, we've prepared the environment to get started with chaincode development. Either take a look here for developing your own or take a look at the Asset Holding or FabCar samples.
If you want to clean up your development environment, close all docker terminals and execute
docker-compose -f docker-compose-simple.yaml down within
Runtime environment variables
If you have a chaincode ready to go, please make sure to set the following environment variables so it'll connect to your development environment:
chaincodeis just a placeholder for the name of your chaincode. So for example it could be
fabcar:0whereas the number indicates the version)