A quick introduction for creating a Nuget package locally and using it directly with a .NET Core app.

Situation

I’ve been working on a Blazor Wasm app that presents the GitHub dotnet/iot Device Sample apps as a menu for individual selection and deployment as projects to a target such as a Raspberry Pi. The client side of the Wasm app runs as PWA app getting the selected sample app project files from the server and then deposits them on the target
The problem was or is that some samples require version 1.1.0 of the SystemDevice.GPIO and Iot.Device.Bindings.
Version 1.0.0 is the current Stable release available from Nuget.org, although there is are some Prerelease versions of 1.1.0 available from there. The problem is that some of the Device Sample Apps do not run with any of those packages.
So as part of the Blazor Wasm app, the packages have been built using a forked version of the current dotnet/iot repository and made available through the app.

About the djaus2/iot fork of dotnet/iot

This was done so as to modify all of the Device Sample projects (in iot/src/device/XXX/Samples folders) could be upgraded to
.NET Core 3.1 All of the src/Devices/XXX and IoT.Device.Bindings remain as .NET Core V2.1 for backward compatibility.
System.Device.GPIO and other .NET Standard projects remain as V2.0 also for backward compatibility.

Note my global.json file, sdk is 3.1 not 5.0.100-preview.6.20310.4 so can build to V3.1 packages

{
  "tools": {
    "dotnet": "5.0.100-preview.6.20310.4",
    "runtimes": {
      "dotnet/x64": [
        "3.1.5"
      ]
    }
  },
  "sdk": {
    "version": "3.1" <---
  },
  "msbuild-sdks": {
    "Microsoft.DotNet.Arcade.Sdk": "5.0.0-beta.20330.3",
    "Microsoft.DotNet.Helix.Sdk": "5.0.0-beta.20330.3"
  }
}

Nuget 101

In this activity, the CpuTemperature sample app is in a folder on the RPI that has Raspian installed. dotnet V3.1 has been installed on the device. CpuTemperature was chosen as its the dotnet/iot HelloWorld sample because it requires no hardware. It denotes the current CPU temperature on the RPi (Note this sample does not work on IoT-Core).

Create a package:

From within the djaus2/iot repository on my dev machine:

  • Build a Release package from within iot\src\System.Device.GPIO: dotnet pack -c Release
  • Build it for Raspian: dotnet pack -c Release --runtime linux-arm
    • It is placed in iot\artifacts\packages\Release\Shipping\ as System.Device.Gpio.1.1.0-dev.nupkg
    • Similarly building IoT.Device.Bindings creates Iot.Device.Bindings.1.1.0-dev.nupkg in same folder.

On the target:

All of the following commands are run from a command prompt within the target project folder. Using System.Device.GPIO as an example. Assuming the project files are there:

  • List the packages currently subscribed to by the project: dotnet list package eg:
pi@raspberrypi:/temp/Sample $ dotnet list package
Project 'CpuTemperature.Samples' has the following package references
   [netcoreapp3.1]: 
   Top-level Package          Requested   Resolved
   > Iot.Device.Bindings      1.0.0       1.0.0   
   > System.Device.Gpio       1.0.0       1.0.0   
  • Add a package to a project: dotnet add package System.Device.GPIO
  • Add a specific version: dotnet add package System.Device.GPIO -v 1.1.0-prerelease.20153.1
  • To update a package: Just repeat the add package command

As noted, needed to use the custom built packages (as built above):

  • Need to copy the Shipping folder from the dev machine (via FileShare or FTP) to the target (the RPi)
    • I put it in /temp and renamed it Nuget so the path is now /temp/Nuget
  • List the source/s of Nuget packages searched with package add: dotnet nuget list source eg:
pi@raspberrypi:/temp/Sample $ dotnet nuget list source
Registered Sources:
  1.  nuget.org [Enabled]
      https://api.nuget.org/v3/index.json
  • Add the new local nuget folder to the sources: dotnet nuget add source /temp/nuget Now:
pi@raspberrypi:/temp/Sample $ dotnet nuget list source
Registered Sources:
  1.  nuget.org [Enabled]
      https://api.nuget.org/v3/index.json
  2.  Package source 1 [Enabled]
      /temp/nuget
  • Now can add the custom built packages:
dotnet add package system.device.gpio -v 1.1.0-dev
dotnet add package iot.device.bindings -v 1.1.0-dev
  • The last command when run:
pi@raspberrypi:/temp/Sample $ dotnet add package iot.device.bindings -v 1.1.0-dev
  Determining projects to restore...
  Writing /tmp/tmpzOJok6.tmp
info : Adding PackageReference for package 'iot.device.bindings' into project '/temp/Sample/CpuTemperature.Samples.csproj'.
info : Restoring packages for /temp/Sample/CpuTemperature.Samples.csproj...
info : Package 'iot.device.bindings' is compatible with all the specified frameworks in project '/temp/Sample/CpuTemperature.Samples.csproj'.
info : PackageReference for package 'iot.device.bindings' version '1.1.0-dev' updated in file '/temp/Sample/CpuTemperature.Samples.csproj'.
info : Committing restore...
info : Assets file has not changed. Skipping assets file writing. Path: /temp/Sample/obj/project.assets.json
log  : Restored /temp/Sample/CpuTemperature.Samples.csproj (in 1.78 sec).
  • And the project file now is:
<!-- CpuTemperature.Samples.csproj-->
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="iot.device.bindings" Version="1.1.0-dev" />
    <PackageReference Include="System.Device.Gpio" Version="1.1.0-dev" />
  </ItemGroup>

</Project>
  • And the running app:
pi@raspberrypi:/temp/Sample $ dotnet run
CPU Temperature: 49.92599868774414 C
CPU Temperature: 48.3120002746582 C
CPU Temperature: 47.236000061035156 C
CPU Temperature: 46.15999984741211 C
CPU Temperature: 46.15999984741211 C

 TopicSubtopic
  Next: > Blazor
<  Prev:   Blazor
   
 This Category Links 
Category:Coding Index:Coding
  Next: > Blazor
<  Prev:   PowerShell