Running a DotNET Core Application as a Service on CentOS a Step by Step Guide

When you have a .NET Core application that you want to run continuously on a CentOS server, running it as a service is a good way to ensure it stays up and running, even if the server is rebooted. A service is a background process that can be started automatically when the system boots up and is managed by a system process called Systemd.

To create a service for your .NET Core application, you will need to create a service unit file that defines the service and its behavior. The service unit file is a plain text file that specifies the commands to run and any required configuration options. In this post, we will walk through the steps to create a service unit file to run a .NET Core application as a service on CentOS.

Here are the steps to create a Systemd service unit file:

Step 1. Open a text editor and create a new file for the service unit file. For example, you can use the nano editor to create a file called mydotnetapp.service:

1sudo nano /etc/systemd/system/dotnetapp.service

Step 2. In the file, add the following content:

 1[Unit]
 2Description=My .NET Core App service
 3After=network.target
 4
 5[Service]
 6Type=simple
 7User=root
 8WorkingDirectory=/path/to/my/dotnet/app/code
 9ExecStart=/usr/bin/dotnet App.dll --urls "http://0.0.0.0:5000"
10
11[Install]
12WantedBy=multi-user.target
  • The [Unit] section describes the service unit and specifies the dependencies.
  • The [Service] section describes the service and specifies the command to run. Note that we're using the full path to the dotnet executable, and specifying the path to the .dll file for our .NET Core app, along with the --urls option to specify the URL for the application.
  • The [Install] section specifies the target that the service should be enabled for.

Step 3. Save and close the file.

Step 4. Reload the Systemd daemon to load the new service unit file:

1sudo systemctl daemon-reload

Step 5. Start the service:

1sudo systemctl start dotnetapp

Step 6. Check the status of the service:

1sudo systemctl status dotnetapp

Step 7. Enable the service to start at boot:

1sudo systemctl enable dotnetapp

Congratulations! Your .NET Core application is now running as a service on your CentOS system with the specified URL. With this setup, your application will start automatically when the system boots up, and you can manage it using the systemctl command.

In summary, running a .NET Core application as a service on CentOS is a simple and effective way to ensure that your application runs continuously on your server. By creating a Systemd service unit file, you can define the service's behavior, specify any required configuration options, and start, stop, or restart the service as needed.

I :heart: AWS! :smile: Enjoy