Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 29, 2021 08:00 pm GMT

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 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=notifyWorkingDirectory=/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-reloadsudo 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.


Original Link: https://dev.to/uthmanrahimi/deploy-net-core-worker-service-on-linux-1mjc

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To