Using our NuGet package with .Net

Using our NuGet package with .Net

This week, we're taking advantage of the release of a fix on our C# library (more specifically, the library's Nuget package) to take a look at the use of the NuGet package with .Net Core.






For a long time now, we've been publishing our C# programming library as a Nuget package (Yoctopuce.YoctoLib), in addition to the GitHub repository and our website. This package enables our programming API to be used with .Net Framework or .Net Core projects.

This week, we had to fix an error in this package that appeared in .Net Core projects when using the "dotnet publish" command.

After fixing the problem in our NuGet package, we realized that we'd never really talked about how to use this command. So we're going to create a small command-line application that simply displays the list of the Yoctopuce modules connected to USB ports, and prepare it for deployment using this command.

Developing

The first step is to create the .Net Core project with the following command:

dotnet new console


This command creates a new .Net Core project for a terminal application in the current directory. Next, we need to add our .Net programming library, using the following command:

dotnet add package Yoctopuce.YoctoLib


All that remains is to write the application in C#. Note: If you've never used our modules or library, this is the example code included in the documentation of all modules.

string errmsg = "";
if (YAPI.RegisterHub("usb", ref errmsg) != YAPI.SUCCESS)
{
    Console.WriteLine("RegisterHub error: " + errmsg);
    Environment.Exit(0);
}

Console.WriteLine("Device list");
YModule m = YModule.FirstModule();
while (m != null)
{
    string serialNumber = m.get_serialNumber();
    string productName = m.get_productName();
    Console.WriteLine(serialNumber + " (" + productName + ")");
    m = m.nextModule();
}

YAPI.FreeAPI();


You can then use the "dotnet run" command to compile and run the application.

C:\tmp\dotnet_test>dotnet run Device list LIGHTMK3-17EFC2 (Yocto-Light-V3)


This command compiles the application source code and generates binary files in the output directory (by default "bin/Debug") using the version of .Net installed on the machine.

The application now works on our machine, but we still need to publish it so that it runs on a computer that doesn't have .Net installed (or another version).

Publishing

To run, the .Net application needs the code we've compiled, the Yoctopuce library, and the .NET runtime.

The publishing step must generate a version of the application that contains all these elements, so that it can be easily installed on any machine. The "dotnet publish" command does just that.

There are, however, a few options to be passed to it in order to obtain a truly portable application.

By default, the "publish" command does not include the .Net runtime. To obtain a completely independent and portable application, you need to use the --self-contained option. This option includes .Net runtime in the output directory.

The -o option is used to specify an output directory.

For example, the following command packages the application and all its dependencies in the ready_to_deploy subdirectory:

dotnet publish --self-contained -o ready_to_deploy


Once this command has been executed, all you need to do is copy the ready_to_deploy directory to any Windows machine to be able to use the application.

It is no longer necessary to launch the application with dotnet. To run the application, simply launch the executable in the directory:

C:\ready_to_deploy>demo_dotnet.exe Device list LIGHTMK3-17EFC2 (Yocto-Light-V3)


Conclusion

To sum up, the dotnet publish command lets you create self-contained, portable applications, ready to be deployed on any machine without the need to install .NET.

Add a comment No comment yet Back to blog












Yoctopuce, get your stuff connected.