Deploy .NET Core worker Service on Linux

In the previous post, we learned how to deploy an ASP.NET Core Application on Linux and configure it, in this post we are going to learn about deploying a Worker Service on a Linux machine.

What is Worker Service?

Before talking about our m…


This content originally appeared on DEV Community and was authored by Uthman Rahimi

In the previous post, we learned how to deploy an ASP.NET Core Application on Linux and configure it, in this post we are going to learn about deploying a Worker Service on a Linux machine.

What is Worker Service?

Before talking about our main topic, for those of you who do not know what is a worker service, let's learn about what is worker service.
Worker Service is a built-in feature in .NET Core for creating background services. One example of using Worker Service is running periodical schedules like sending newsletter emails for clients every morning. To learn more about worker service, refer to this link.

We assume that you have created a Worker Service and now you want to deploy it on a Linux machine. First of all, as you learned in the previous article, we need to create a new service file, so use the below command to create a service file:

sudo nano /etc/systemd/system/appbackground.service

and edit its content with the following content to it:

[Unit]
Description=Your description 

[Service]
Type=notify
WorkingDirectory=/home/centos/Desktop/services/

ExecStart=/usr/bin/dotnet /home/centos/Desktop/services/myapp.WorkerServic$


Environment=ASPNETCORE_ENVIRONMENT=Production
[Install]
WantedBy=multi-user.target

Then press ctrl+x to save its content and run the following commands:

sudo systemctl daemon-reload
sudo systemctl start appbackground.service

If you get an error after running sudo systemctl start appbackground.service you will need to add a small change to your worker service project.

Install Microsoft.Extensions.Hosting.Systemd by nuget:

dotnet add package Microsoft.Extensions.Hosting.Systemd

Then change CreateHostBuilder in program.cs like below:

 public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseSystemd() // this method must be added
                .ConfigureServices((hostContext, services) =>
                {
                    services.AddHostedService<appWorker>();
                });
    }

After applying this change, get a new publish of your project and put it in the path on the Linux then run start its service.


This content originally appeared on DEV Community and was authored by Uthman Rahimi


Print Share Comment Cite Upload Translate Updates
APA

Uthman Rahimi | Sciencx (2021-12-29T20:00:09+00:00) Deploy .NET Core worker Service on Linux. Retrieved from https://www.scien.cx/2021/12/29/deploy-net-core-worker-service-on-linux/

MLA
" » Deploy .NET Core worker Service on Linux." Uthman Rahimi | Sciencx - Wednesday December 29, 2021, https://www.scien.cx/2021/12/29/deploy-net-core-worker-service-on-linux/
HARVARD
Uthman Rahimi | Sciencx Wednesday December 29, 2021 » Deploy .NET Core worker Service on Linux., viewed ,<https://www.scien.cx/2021/12/29/deploy-net-core-worker-service-on-linux/>
VANCOUVER
Uthman Rahimi | Sciencx - » Deploy .NET Core worker Service on Linux. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/29/deploy-net-core-worker-service-on-linux/
CHICAGO
" » Deploy .NET Core worker Service on Linux." Uthman Rahimi | Sciencx - Accessed . https://www.scien.cx/2021/12/29/deploy-net-core-worker-service-on-linux/
IEEE
" » Deploy .NET Core worker Service on Linux." Uthman Rahimi | Sciencx [Online]. Available: https://www.scien.cx/2021/12/29/deploy-net-core-worker-service-on-linux/. [Accessed: ]
rf:citation
» Deploy .NET Core worker Service on Linux | Uthman Rahimi | Sciencx | https://www.scien.cx/2021/12/29/deploy-net-core-worker-service-on-linux/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.