It’s still a bit too early to use the .Net Bazel rules in a real enterprise project, but it’s at least possible to start doing some small POCs. In this article I will show how I built a .Net Core RabbitMQ queue producer/consumer using a Bazel setup.
For some background on Bazel and its benefits I recommend checking out my previous article first.
The Bazel rules can be downloaded from the Bazelbuild org on Github. In this experiment I am primarily using the core_binary rule, which is the rule designed to create .Net Core binaries like console applications. See the example below:
In this sample rule I am building the console app that will act as the producer of my RabbitMQ messages. The consumer is identical since it’s just another console application.
In addition to telling Bazel to build Program.cs, I am also declaring dependencies on the RabbitMQ nuget package and .Net’s mscore lib. The nuget package is declared separately using the nuget_package rule as seen below:
In my sample I am only showing the .Net Core parts of the Nuget package rule, but it can be configured to support other .Net environments as well.
The nuget integration is perhaps the most painful part with the current Bazel rules. Since packages tend to come as a large dependency graph of recursive dependencies, it’s impractical to hand code the nuget_package rules. To help with this there is a tool in the rules repo to generate WORKSPACE rules for all necessary nuget packages. Ideally this tool should be integrated as a Visual Studio plugin to make the integration more seamless.
In my experiment I used regular Visual Studio on my Mac to do the coding, but triggered builds on the command line using IBazel, which is a Bazel file watcher. This enables me to automatically restart the application on every file save, which makes for a good dev experience.
In order to benefit from intelisense inside Visual Studio I ended up installing the nuget packages in Visual Studio as well. This is kind of redundant since Bazel already downloads them, but it makes for a better dev experience in the editor since you get intelisense through Visual Studio. Hopefully there will be better Visual Studio integration eventually.
I have uploaded the code to a Github repo in case you are interested in trying it out yourself.
For reference I have included the RabbitMQ Producer/Consumer code below: