.NET on Raspberry Pi


Introduction

The .NET editions of our toolkits support modern cross-platform .NET (.NET 5 and later, including the current long-term support releases), so the same components can be used on Linux, including on ARM devices such as the Raspberry Pi. This entry shows how to use the HTTP component from IPWorks in a console application running on a Raspberry Pi with the 64-bit version of Raspberry Pi OS.

Contents

Installing .NET on the Raspberry Pi

The .NET SDK and runtime are available for both 64-bit (ARM64) and 32-bit (ARM32) Linux, so applications can either be built directly on the Raspberry Pi, or built on another machine and copied to it.

To install .NET on the Raspberry Pi, run Microsoft's installation script, and then add .NET to the path:

curl -sSL https://dot.net/v1/dotnet-install.sh | bash /dev/stdin --channel LTS echo 'export DOTNET_ROOT=$HOME/.dotnet' >> ~/.bashrc echo 'export PATH=$PATH:$HOME/.dotnet' >> ~/.bashrc source ~/.bashrc dotnet --version

If the application will be published as a self-contained application (described below), installing .NET on the Raspberry Pi is not required. For more information, please see Microsoft's Deploy .NET apps to ARM single-board computers article.

Creating the Project

Create a new console application, and add the IPWorks NuGet package to it. These commands can be run either on the Raspberry Pi or on a development machine.

dotnet new console -n DotNetPiTest cd DotNetPiTest dotnet add package nsoftware.IPWorks

Code

Replace the contents of the generated Program.cs file with the following code. The program downloads a file using the HTTP component, and prints messages as the download progresses.

When deploying to a machine that does not have the toolkit installed (such as a Raspberry Pi), the component's RuntimeLicense property must be set to a valid runtime license. For information about how to obtain this value, please see our .NET Licensing and Deployment Guide entry.

using nsoftware.IPWorks; HTTP http = new HTTP(); http.RuntimeLicense = "RUNTIME_LICENSE"; // Replace with the runtime license. int lastPercent = -1; http.OnConnected += (s, e) => Console.WriteLine($"Connected: {e.StatusCode} {e.Description}"); http.OnHeader += (s, e) => Console.WriteLine($"Header: {e.Field}: {e.Value}"); http.OnTransfer += (s, e) => { // Print the progress every 5 percent. if (e.PercentDone % 5 == 0 && e.PercentDone != lastPercent) { Console.WriteLine($"Transferred: {e.PercentDone}% ({e.BytesTransferred} bytes)"); lastPercent = e.PercentDone; } }; http.OnEndTransfer += (s, e) => Console.WriteLine("Transfer complete."); http.OnDisconnected += (s, e) => Console.WriteLine($"Disconnected: {e.StatusCode} {e.Description}"); http.LocalFile = Path.Combine(AppContext.BaseDirectory, "download.html"); http.Get("https://www.nsoftware.com/"); Console.WriteLine("Saved to " + http.LocalFile);

Publishing and Running the Application

To run the application directly on the Raspberry Pi (if it was created there), run the following command from the project folder:

dotnet run

To build the application on a development machine instead, publish it for the Raspberry Pi's architecture, and then copy the published folder to the Raspberry Pi (e.g., using scp). For the 64-bit version of Raspberry Pi OS, use the linux-arm64 runtime identifier; for the 32-bit version, use linux-arm.

dotnet publish -c Release --runtime linux-arm64 --self-contained

The --self-contained option includes the .NET runtime with the application, so .NET does not need to be installed on the Raspberry Pi. After copying the published folder to the Raspberry Pi, make the application executable and run it:

chmod +x DotNetPiTest ./DotNetPiTest

If the application is published without --self-contained, .NET must be installed on the Raspberry Pi, and the application is run using dotnet DotNetPiTest.dll instead.

We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.