User Tools

Site Tools

한국어

comfilepi:dotnet_core_development

.NET Development

.NET 8 brings a professional .NET runtime to Linux-based PCs like the ComfilePi. The 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 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 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.

A .NET real-time web application running on the ComfilePi

Download the source Code for the application demonstrated in the video above.

See also the .NET IoT Libraries API Reference.

Install the .NET SDK on the ComfilePi

NOTE: 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.

To install the .NET SDK run the following script in a terminal on the ComfilePi.

wget https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh
chmod +x dotnet-install.sh
sudo ./dotnet-install.sh --channel 8.0 --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.

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:

* 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.

* 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

* Download Visual Studio Code as a companion IDE for the dotnet command line tools available after downloading and installing the .NET SDK.

Create, Build, and Publish a .NET Application for the ComfilePi

.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.

Build the project just as you would any other Visual Studio project.

Publish a Project for the ComfilePi

To publish a .NET project using Visual Studio, right-click the project in Visual Studio's Solution Explorer and choose the Publish option. In the Advanced Settings choose the linux-arm runtime. If the .NET SDK or runtime is already installed on the ComfilePi, choose the Framework Dependent development mode.

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.

Publish a Project for the ComfilePi

To publish a .NET project for the ComfilePi, run dotnet publish -r linux-arm –self-contained false.

If you want to publish a self-contained set of binaries so the .NET SDK or runtime does not need to be installed on the ComfilePi, run dotnet publish -r linux-arm –self-contained true. Be aware that a self-contained application will cause a very large number of files to be generated and copied to the ComfilePi which will likely make iterative development very unproductive.

Deploying a Project to the ComfilePi

The result can be copied to the ComfilePi using normal file copy procedures, but it may be more efficient to automate it. The following is a sample powershell script to both publish and deploy a .NET application from a Windows development PC.

$publish_dir = "{Project Folder}\bin\Release\net7.0\publish\"
$ip = "{IP Address of the ComfilePi}"
$dest_dir = "~/{Project Name}"
$user = "pi"
 
Write-Host "Publishing"
Remove-Item -Recurse -Force ${publish_dir}
dotnet publish -r linux-arm -c Release -o ${publish_dir} --self-contained false
 
Write-Host "Deploying the application"
scp -rp ${publish_dir} pi@${ip}:${dest_dir}

See also Deploy .NET apps to the Raspberry Pi.

Running a Deployed Project on the ComfilePi

After a project has been deployed to the ComfilePi it can be executed with the command dotnet {executable}.

For ASP.Net applications, the project can be run by changing to the project's directory and running the command dotnet {project name}.dll. By default the project will be bound to http://localhost:5000, but that will only make the project available to the ComfilePi's browser. To make the project available to external clients, run the command dotnet {project name}.dll --urls http://0.0.0.0:5000

The application can then be accessed either from the ComfilePi's web browser or from any other device with a modern web browser.

The ComfilePi's browser can run the application in kiosk mode using a command like chromium-browser --noeerrdialogs --disable-infobars --kiosk http://localhost:5000

Type Ctrl-C to shutdown the application.

Debugging a Project

The project can be debugged in Visual Studio using Debug–>Attach to Process… from Visual Studio's menu bar.

See also Debug .NET apps on the Raspberry Pi.

To automate deployment and debugging in Visual Studio, consider the Visual Studio VSRemoteDebugger Extension.

comfilepi/dotnet_core_development.txt · Last modified: 2023/11/22 12:25 by COMFILE Technology