User Tools

Site Tools

한국어

comfilepi:dotnet_core_development

.NET 5 Development

.NET 5 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 5 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 5 SDK on the ComfilePi

Download the .NET 5 SDK from https://dotnet.microsoft.com/download/dotnet/5.0 and install it on the ComfilePi. The ComfiliePi is a Linux ARM32 device.

# Download the .NET 5 SDK archive
wget {URL of .NET 5 SDK *.tar.gz file}
 
# Create the installation folder
sudo mkdir -p /opt/Microsoft/dotnet 
 
# Extract the archive to `/opt/Microsoft/dotnet` folder
sudo tar -xvf {filename}.tar.gz -C /opt/Microsoft/dotnet
 
# Create a symbolic link to the `dotnet` executable file
sudo ln -s /opt/Microsoft/dotnet/dotnet /usr/bin/dotnet
 
# Create a configuration file telling the system where to find the .NET 5 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 window.

Install the .NET 5 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 2019 or later and install it on a development PC. When installing, be sure to select the options for .Net 5 development. It will install both the Visual Studio IDE and the .Net 5 SDK.

* Download the .NET 5 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 5 SDK.

Create, Build, and Publish a .Net 5 Application for the ComfilePi

.Net 5 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 5 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 5 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 5 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 5 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 5 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 5 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 5 application from a Windows development PC.

$publish_dir = "{Project Folder}\bin\Release\net5.0\publish\"
$ip = "{IP Address of the ComfilePi}"
$dest_dir = "~/{Project Name}"
$user = "pi"
$password = "raspberry"
 
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.

comfilepi/dotnet_core_development.txt · Last modified: 2020/11/13 13:12 by COMFILE Technology