Skip to content

Running in CentOS (Linux)

adriancs edited this page Jan 27, 2022 · 8 revisions

The idea is to build the project in .NET Core.

We can test this out with a simple Console App.

07

Starts by creating a .net core Console linux project:

01

Check the project properties, ensure the project is built with .NET Core framework:

02

Install Nuget Package of MySqlBackup.NET (with MySqlConnector):

03

A simple sample code to test out:

static void Main(string[] args)
{
    try
    {
        Console.WriteLine("Enter the MySQL Connection String: ");
        Console.WriteLine();

        string constr = Console.ReadLine();

        Console.WriteLine();
        Console.WriteLine();

        Console.WriteLine(("Begin MySQL backup process..."));

        string folder = Environment.CurrentDirectory;
        string file = Path.Combine(folder, "backup.sql");

        using (MySqlConnection conn = new MySqlConnection(constr))
        {
            using (MySqlCommand cmd = new MySqlCommand())
            {
                using (MySqlBackup mb = new MySqlBackup(cmd))
                {
                    conn.Open();
                    cmd.Connection = conn;

                    mb.ExportToFile(file);

                    conn.Close();
                }
            }
        }

        Console.WriteLine("MySQL backup file saved at: " + file);
    }
    catch (Exception ex)
    {
        Console.WriteLine();
        Console.WriteLine("Error: " + ex.Message);
    }

    Console.WriteLine();
    Console.WriteLine("Press any key to exit...");

    Console.ReadKey();
}

Next, is to publish the project for .NET Core (Linux):

08

04

In above example, the Linux compiled binaries are located at:

\bin\Release\netcoreapp3.1\publish\linux-x64

Copy the DLL in above folder and paste it to your CentOS

At the CentOS, make sure that you have installed Dotnet runtime by following command:

sudo dnf install dotnet-sdk-<version>

For example:

sudo dnf install dotnet-sdk-3.1
sudo dnf install dotnet-sdk-5.0

Assume that you have copied your .NET Core DLL files at the following folder:

/home/<username>/testapp

Then you can test out your app with following command:

cd /home/admin/testapp
dotnet ConsoleApp.dll

Here is the sample source code for your referrence:

ConsoleApp2.zip

Run .NET Core App without installing dotnet runtime

.NET Core App can also be run without installing dotnet runtime, which is by compiling the project into single "self-contained" binary.

Here is the screenshot for publishing the "self-contained" binary:

05

Here is the example of how it looks like after publishing, there is only 1 single file:

06

Copy the compiled binary to CentOS

At the terminal, browse to the folder that contains the self-contained binary, for example:

cd /home/admin/testapp

Next, is to allow execution permission to the file:

sudo chmod 777 ConsoleApp2

Run the app:

./ConsoleApp2

For more information on publishing "self-contained" binary, read more at:

https://docs.microsoft.com/en-us/dotnet/core/deploying/single-file