Building .NET applications using the CLI
Visual Studio allows you to develop, test, and compile .NET Framework applications inside a single IDE.
However, if you want to set up automated builds for a Continuous Integration pipeline you’ll need to learn how to do so via the command line.
In this post, we’ll set up a build server to compile .NET Framework applications using Cake.
When is this useful?
Creating a continuous integration pipeline will allow you to automatically:
- Compile your application
- Run tests
- Create binaries / installers
- Deploy your application
Is this just for Windows?
If you’re writing a .NET Core application and want to build on Linux, the process is a little different. For simplicity’s sake, we’ll only cover Windows in this post.
Prerequisites
- Windows Server 2012 R2 or higher
Installing Build Tools
- Download Build Tools for Visual Studio 2019
- Click continue
- In the Workloads section, choose the types of applications you will be building.
- I chose Web Development Build Tools and .NET Core Build Tools
- In the Individual Components section, choose the targeting pack and SDK for any of the .NET Framework versions your applications use.
- Check NuGet targets and build tasks
- Click install
- Go take a break! This will take ~5-10 minutes.
- Click restart when it’s all done
Using Cake to build an application
Cake is a tool that allows you to write C# in order to build, test, and copy files. We’ll be using it to build a sample project provided by Cake.
Download the sample project zip and extract it.
Here are the two files that are used to perform the build:
- build.ps1
- Installs cake if it is not found on your computer
- build.cake
- C# script that tells cake how to build your application
In the build.cake file, there is a collection of Tasks that are executed in order by using the IsDependentOn() call.
Running the build script
- Open a powershell terminal
- Navigate to the folder you extracted the sample project zip to
- Run
.\build.ps1
You might get an error like this:
PS C:\Users\Administrator\Downloads\example-master> .\build.ps1
.\build.ps1 : File C:\Users\Administrator\Downloads\example-master\build.ps1 cannot be loaded. The file
C:\Users\Administrator\Downloads\example-master\build.ps1 is not digitally signed. You cannot run this script on the
current system. For more information about running scripts and setting execution policy, see about_Execution_Policies
at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ .\build.ps1
+ ~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
If Windows realizes you downloaded the file from the internet it make block execution. You can get around this by telling Windows that the files did not come from the internet using a powershell command.
If you received the error, follow these steps:
- Run
Get-ChildItem .\ | Unblock-File
- Close the Powershell Window
- Reopen the Powershell Window and navigate to the folder
- Run
.\build.ps1
Test Run Summary
Overall result: Passed
Test Count: 2, Passed: 2, Failed: 0, Inconclusive: 0, Skipped: 0
Start time: 2018-04-22 23:57:57Z
End time: 2018-04-22 23:57:57Z
Duration: 0.113 seconds
========================================
Default
========================================
Task Duration
--------------------------------------------------
Clean 00:00:00.0121812
Restore-NuGet-Packages 00:00:02.5454776
Build 00:00:02.8819069
Run-Unit-Tests 00:00:01.0125675
--------------------------------------------------
Total: 00:00:06.4554858
If you saw the above result, congratulations! You compiled a C# application and ran a set of unit tests via the command line without installing Visual Studio.
Try it with your own solution
You can take the build.cake file and build.ps1 file and copy it into your own .NET application’s folder.
Then you can edit these lines in the build.ps1 file to point to your own project:
var buildDir = Directory("./src/Example/bin") + Directory(configuration);
NuGetRestore("./src/Example.sln");
MSBuild("./src/Example.sln", settings =>
For your initial try, I would also comment out or remove the Run-Unit-Tests task and remove the .IsDependentOn("Run-Unit-Tests");
section.
Did you have any luck? If you ran into issues, check out the Cake website for more information.