====== .NET Development ====== .NET brings a professional .NET runtime to Linux-based PCs like the ComfilePi. The [[https://dotnet.microsoft.com/apps/aspnet/web-apps/blazor|Blazor and Web Assembly]] technologies can be used to build GUI applications for the ComfilePi's modern web browser. Blazor allows both client and server to be programmed almost entirely in C#. The [[https://docs.microsoft.com/en-us/dotnet/api/system.device.gpio|System.Devices.Gpio]] namespace provides just about everything one would need to do IO on the ComfilePi. Blazor also includes support for real-time web applications that can be viewed and interacted with from multiple local or remote clients. The following video demonstrates a project running on the ComfilePi, controlling a [[:cpio:cp-io22-a4-2:index|CP-IO22-A4-2]] IO board (mounted on the rear of the ComfilePi). The application, running on the ComfilePi to the right can be viewed and interacted with from the ComfilePi itself, a development PC, or even a smartphone, in real-time. {{ :comfilepi:dotnet_core_blazor.mp4?720x405 }}
sudo apt update
sudo apt install cpi-dotnet-sdk-9.0
==.NET 8 SDK==
Prior to .NET 10, Microsoft had not published ARM64 packages for the .NET SDK, so we filled that gap in our Bookworm OS by creating our own .NET SDK installation packages.
sudo apt update
sudo apt install cpi-dotnet-sdk-8.0
=== Bullseye ===
ComfilePi OS images dated 2022-07-26 or later have the .NET SDK already installed. Verify by running the command ''dotnet --version'' in a terminal. If you need a later version than that which is installed, use the manually installation process below.
=== Any OS ===
Any version of the .NET SDK can be installed manually using the following commands.
DOTNET_VERSION=10.0 # Choose your version
wget https://builds.dotnet.microsoft.com/dotnet/scripts/v1/dotnet-install.sh
chmod +x dotnet-install.sh
sudo ./dotnet-install.sh --channel $DOTNET_VERSION --install-dir /opt/Microsoft/dotnet
sudo ln -s /opt/Microsoft/dotnet/dotnet /usr/local/bin/dotnet
# Create a configuration file telling the system where to find the .NET runtime
sudo mkdir /etc/dotnet
sudo bash -c 'echo /opt/Microsoft/dotnet/ >> /etc/dotnet/install_location'
Test the installation by running dotnet --version in a terminal.
=== Workaround for .NET ARM Linux Garbage Collection Bug (.NET 8 & 9 Only) ===
**Important** - Due to [[https://github.com/dotnet/runtime/issues/95257|a bug in the .NET runtime]], it may be necessary to restrict the .NET garbage collector's memory limit on devices with small amounts of memory (e.g. a Raspberry Pi) or .NET applications may abort prematurely. To set the garbage collector's memory limit, create or open the file ///etc/environment//, and add the following line to the file.
DOTNET_GCHeapHardLimitPercent=32
Then reboot.
For more information about this setting, see [[https://learn.microsoft.com/en-us/dotnet/core/runtime-config/garbage-collector#heap-limit-percent|Microsoft's official documentation]].
===== Install the .NET SDK on a Development PC =====
It is likely much more convenient to author code on a development PC. To do that there are a few options:
* [[https://visualstudio.microsoft.com/downloads/|Download Visual Studio]] 2022 or later and install it on a development PC. When installing, be sure to select the options for .NET development. It will install both the Visual Studio IDE and the .NET SDK.
* [[https://dotnet.microsoft.com/download/dotnet/|Download the .NET SDK]] and install install it on development PC. You can then use the ''dotnet'' command in a terminal window to create, compile, and publish projects
* [[https://visualstudio.microsoft.com/downloads/|Download Visual Studio Code]] as a companion IDE for the ''dotnet'' command line tools available after downloading and installing the .NET SDK.
===== Create in a .NET Project =====
.NET projects can be created using the ''dotnet'' command line interface (a.k.a. the .Net CLI) or using the Visual Studio project creation templates.
==== Using Visual Studio ====
=== Creating and Building a Project ===
To create a .NET Server-size Blazor application using Visual Studio, create a new //Blazor App// project.
| {{:comfilepi:blazor_app.png?450|}} | {{:comfilepi:blazor_server.png?450|}} |
Build the project just as you would any other Visual Studio project.
==== Using the .NET CLI ====
=== Creating and Building a Project ===
To create a .NET Server-side Blazor application like that demonstrated in the video above, run ''dotnet new blazorserver'' in a terminal window. To build the application run ''dotnet build'' from within the project folder.
====== Publishing, Deploying, and Running a .NET Application ======
To get a .NET application from your development environment to a ComfilePi, you need to perform the following steps in order:
- Publish the application for the ComfilePi's CPU architecture and operating system.
- Deploy the resulting binaries to a ComfilePi panel PC.
- Execute the binaries on the ComfilePi panel PC.
Those steps can all be done using using Visual Studio, the dotnet CLI, and/or Visual Studio Code.
* [[.:dotnet:deploy:visual_studio:index|Using Visual Studio]]
See also [[https://learn.microsoft.com/en-us/dotnet/iot/deployment|Deploy .NET apps on ARM single-board computers]]
===== Debugging a Project =====
To deploy to, launch and debug .NET applications on the ComfilePi from within Visual Studio, use our [[:comfilepi:dotnet_core_development:remote_debugger:index|Remote .NET Debugger Visual Studio Extension]]
See also [[https://docs.microsoft.com/en-us/dotnet/iot/debugging|Debug .NET apps on the Raspberry Pi]].