Using Azure Pipelines to Generate a .NET Package from a Solidity Contract's ABI

Itay Podhajcer
Microsoft Azure
Published in
3 min readFeb 10, 2020

Most commonly, Azure DevOps pipelines are used to build, publish and/or deploy software artifacts. But, as it is possible to execute arbitrary tools available thorough the various package managers (npm, NuGet, pip and more), you can also use such tools to generate software artifacts from “thin air”, sort of speak. This means that a DevOps engineer can deliver a Nuget package for .NET developers to consume in their applications, without writing an actual class library, pretty cool!

In this article we will be using one of Nethereum’s tools inside a multi-stage Azure Pipelines YAML script to:

  1. Build a solidity contract.
  2. Create a .NET class library project.
  3. Generate C# classes using the compiled contract’s ABI.
  4. Build and pack the .NET class library into a NuGet package
  5. Push the NuGet package to a feed

We won’t be covering the first step in this article, as a complete example of creating an Azure pipeline to build a Solidity contract was discussed in one of my previous articles:

Source Code Repository

The repository containing the complete azure pipelines YAML script and a simple solidity contract (based on the above previous article) can be found here:

The Pipeline Script

We will start by creating a stage for the steps from the previous article (the steps for building the Solidity contract) and call it build_contract. Next, we will create a new stage for generating and publishing the NuGet package and call it generate_dotnet_nuget.

  • The first thing we will add to our stage is a dependency on the previous one, meaning, we only want the second stage to execute when the first one completed successfully:
  • We will be using a deployment job with a runOnce strategy:
  • Now we will added the steps for our stage starting by ensuring we have netcore2.1 available on the build agent:

The above step is required, as at the writing of these article:

  1. Nethereum command line tool failed if not ran with netcore2.1.
  2. The was a problem with nuget tool install, where the tools’ folder is not present in the PATH variable and the above step fixes that.
  • Now we can install the tool we will use the generate the C# files, Nethereum.Generator.Console:
  • Create a new C# class library:

We don’t need to keep a project in source control as we can always re-generate the project and build it with a new set of generated C# files.

  • Add the package Nethereum.Web3 which will be required by the generated C# code:
  • Clear the initial C# files generated with the class library as we don’t need them:
  • Extract the compiled Solidity contract from the previous stage:
  • Generate the C# files using the Nethereum tool we installed in a previous step:
  • Build and pack the project into a NuGet package containing the compiled library:
  • And finally, push the package to a NuGet feed (in this case I’m using an internal Azure DevOps feed called internal-nuget):

The complete YAML script should look similar to this:

We can now run our pipeline, and when it completes, we should have a new NuGet package published to our feed.

Conclusion

Some aspects of this example were simplified to not over complicate the script. Some of the changes that might make sense in a production pipeline might include improvements such as receiving the package version from an external variable, building multiple contracts and generating a single package for all those contracts in a single multi-stage pipeline and maybe even run tests on the compiled contracts.

--

--

Itay Podhajcer
Microsoft Azure

Tech expert with 20+ years’ experience as CTO, Chief Architect, and Consultant. 3x Microsoft MVP award winner. Passionate blogger and open-source contributor