A .NET Console app project can be easily modified to be a library that packages up its functionality so that it can be published on Nuget. It is then a simple matter to create a Console app, on any .NET platform, that uses the package and use the functionality there.

Nuget provides a way of publishing cross platform .NET libraries. By converting an existing .NET Console app into a library, you can package up its functionality and publish it on Nuget. All it then needs to run as a Console app on a target platform (with .NET installed), is a very simple console app with a reference to the package (dotnet add package) and a one line call from the host app to Main() in the package. All of this can be enacted on a command line on the target. Simple! .

Deployimg and app on a remote target

The development approach for deploying an app to a remote device such as a Raspberry Pi, is to build the app of the desktop and deploy it or publish it from there. Alternatively you can copy the project files or clone it onto the target and build it there. The approach presented here, once the package is available on Nuget, only requires a few lines of code and command line commands.

Steps

Convert Console app to Package

  1. In the original Console app
    1. In the project’s .csproj project file remove the line:
      <OutputType>Exe</OutputType>
    2. In Program.cs (or where Main() is:
      Change class Program to public static class Program
  2. Test that it builds OK
  3. Test that the library can be hosted in a Console app:
    1. Create a new Console app in the same solution and reference the library in it as a Project reference.
    2. Modify the 7th line of the console app as below where LIBRARY_NAME is the project name of the library.
    3. Test run the new Console app.
  4. Build a RELEASE version, Target Any CPU, of the library and publish it to Nuget
    1. Get the Package Name and version from Nuget.org. Look for the original project name.
namespace ConsoleApp
{
  class Program
  {
    static void Main(string[] args)
    {
      LIBRARY_NAME.Program.Main(args);
    }
  }
}

Use the Package

On a target such as a RPi with .NET 5 installed:

In a new folder:

  1. Create a .NET Console app:
    Run dotnet new console
  2. Add the library as a Nuget package
    Run dotnet add package PACKAGE_NAME --version 1.x.y
    Where PACKAGE_NAME is as it says and 1.x.y is the package version.
  3. Repeat step 3.c as above
  4. Build the app:
    Run dotnet build
  5. Run and test the app:
    dotnet run

Note that you can append any parameters eg. dotnet run 12 The parameters passed to args in the host console app are passed directly to Main() in the library.

Comment

Whilst it may appear that there are many steps here, its is far simpler that it may seems first time up.

An Examples

djaus2/DNETCoreGPIO is a console app to be run on a RPi that can run in a variety of modes to flash an LED, exercise sensors such as the DHT22, BME280, control a DC motor through a H-Bridge and turn a relay off an on. It makes use of the GitHub dotnet/iot packages System.Devices.GPIO and Iot.Device.Bindings and its IO functionality is based upon the samples in that repository. The app was modified as above into a shell console app with the functionality in a library that was deployed to Nuget here. The djaus2/DNETCoreGPIO GitHUb repository has two console apps. One that refers to library as a local project reference and a second one that uses the Nuget package. This app, in its original un-partitioned form was cloned and built on the RPi. The Nuget version can be easily generated as above, builds and run Ok ona RPi.

Further

Encapsulating the functionality of a console app into a Nuget package also means that the functionality can be used in a GUI environment. This was done with a Blazor app where emails are sent to members of a sporting club. The text is submitted as Markup, with the parsing into HTML performed by the Nuget package Meltdown. This is meant to be a simpler Markup than Markdown.

Conclusion

This provides a simple mechanism for publishing .NET Console apps, which are typically tools, in a cross platform manner, using Nuget. Yes, this approach isn’t suitable for developing or extending an existing console app, but if ready to use, it makes a simple way to implement the app of a range of devices.


Footnote

The DNETCoreGPIO app takes a single integer parameter which determines its functional mode. See djaus2/DNETCoreGPIO. Originally it was menu driven and once running it an ran until exit was chosen. Via the menu, for example, the motor could be enabled, stopped, driven forwards or reverse. Additional options were introduced so that rather than a menu, it can run run once to set , the motor for example, in a particular state. In run once mode it can also read a sensor and write the result as a pronounceable string to a file. These run once modes were introduced so that devices could be controlled by a Google Nest devices making use of TRIGGERcmd. You can say, for example, “Hey Google, run temp on Pi”, and the Nest will enunciate the environment as read by a DHT22 sensor on the RPi named Pi.
See my blog posts on this.


 TopicSubtopic
<  Prev:   Google Home Windows Bridge
   
 This Category Links 
Category:Coding Index:Coding
  Next: > Nuget Packages 101
<  Prev:   .NET Core