Azure DevOps for .NET Core 3.1 Worker Services
In this article I deep dive into deploying .NET Core Worker Services to Windows Services on a VM and Linux or Windows App Services in Azure using Azure DevOps.
My love for .NET Core increased tremendously when .NET Core 3.0 was released and I found out about the new Worker Service project template. Even more so when I read up on how easy it is to deploy as a Windows Service, a Linux daemon, an Azure App Service, etc.
So our team at Apstory decided to migrate all our customers Azure Cloud Services (classic) to this awesome new background processing micro-service framework in .NET core 3.1. Refactoring was a breeze as all of our micro-services were written with .NET Standard 2.0. We were able to easily debug and run them locally and ensure that everything was working accordingly. We even managed to get Application Insights telemetry and the Log4Net appender running without a hitch!
Deploying as a Windows Service on an Azure VM
We have a lot of experience writing Windows Services and getting the worker services to deploy to our QA VM hosted in Azure using Azure DevOps was pretty easy:
First we had to add the Microsoft.Extentions.Hosting.WindowsServices nuget package to the project for Windows and Microsoft.Extentions.Hosting.Systemd for Linux:
We also had to add the below lines of code to Program.cs:
Manually Install Windows Service
Our service is now ready to run on a Windows VM in the Azure cloud. We installed it manually on our QA VM using sc.exe. First we published the project to a local folder and copied all the artifacts across to our QA VM and then we ran the following command to install it:
sc create [Service name] binPath=[Path to exe]
Configure Azure DevOps Build Pipeline
We added 4 tasks:
4. Publish Artifact
Configure Azure DevOps Azure VM Release Pipeline
We added 4 tasks to the QA VM release pipeline:
- Stop Service
3. Copy Files
4. Start Service
And that’s it for the Azure VM Windows Service!
Deploying to a Linux Azure App Service
The release pipeline for the Linux Azure App Service was even easier to release, we added 2 tasks:
- File Transform
2. Azure App Service Deploy
For a Linux App Service Plan you need to add the Startup command!
Below is screenshot of the release pipeline stages:
Below is a screenshot of the Linux App Service hosted in the Azure cloud:
Deploying to a Windows Azure App Service
It’s pretty much the same as the Linux App Service configuration but there is a slight difference on the build pipeline for the Worker Service that is super important:
In the above image the dotnet publish needs to publish the output to a specific path in order for it to startup and run continuously in the Azure App Service:
— configuration release — output $(Build.ArtifactStagingDirectory)\publish\app_data\jobs\continuous\[Service Name]
And that’s that! The Worker Services run extremely well on the QA VM and Production Azure App Services. As mentioned above it’s also really easy to debug and we get full visibility through App Insights telemetry in our QA and Production environments.
In our next article we’ll take you through setting up and configuring App Insights.