Deploying .NET Core Background Service On Windows

Configure your .NET Core project

 
When trying to deploy on a Windows Service, make sure you already configure your Program.cs accordingly. On the Program.cs add this config,
 
Image by Author
 

Configure your Windows Server

 
Downloading the required packages
 
Install the required packages before configuration.
Downloading the dotnet SDK
 
Deployments only need runtime, but if you want to have a more flexible environment that allows you to build and publish dll files rather than only running it you would want to install the latest .NET Core SDK, here.
 
Install the Azure Artifacts Credential Provider
 
If you use private NuGet packages you might want to install Azure Artifacts Credentials. You can find the latest release here and download the .zip package.
 
Image by Author
 
After downloading the zip archive, copy the plugins folder to %USERPROFILE%/.nuget.
 
Image by Author
 
Set your environment variables, open User Variable, and create a new entry.
 
Image by Author
 
Set the variable name to NUGET_PLUGIN_PATHS and because in this case, we are going to be using the azure credential provider, so set the value to
 
%USERPROFILE%\.nuget\plugins\netcore\CredentialProvider.Microsoft\CredentialProvider.Microsoft.dll
 
Image by Author
Then finish the setup by clicking OK on the Environment Variables dialog. Install the Azure Artifacts Credential Provider, full instructions can be seen here.
 

Publish your .NET Core application

 
Pull the project using Git
 
Get into the directory where you want to put the application.
 
Image by Author
 
Get inside the newly formed folder and run the dotnet publish command. Do dotnet publish -r win-x64 -c Release to make a windows installer on publishing.
 
Image by Author
 
If an authorization prompt to https://microsoft.com/devicelogin pops up, login using the Microsoft account and enter the code shown in the window.
 
Otherwise, it should show a successful build like so,
 
Image by Author
 
In this case, let’s assume D:\Projects\api.test\bin\Release\netcoreapp3.1\publish\zoom-service.exe is the physical path.
 

Run your .NET Core Service

 
Create the Service
 
First, create a Windows Service using the sc create command and enter the service name, in this case, would be ZoomService, and add the physical path to the BinPath.
 
An example command would be: sc create ZoomService BinPath=D:\Projects\api.test\bin\Release\netcoreapp3.1\publish\zoom-service.exe
 
Image by Author
 
Start the Service
 
After creating the Windows Service start it by using the sc start command. An example command would be: sc start ZoomService.
 
Image by Author
 
Make the Service auto initialize on Startup
 
Go to the Task Manager, and open the services tab, and right-click on your newly created service
 
Image by Author
 
Then click the Open Services option.
 
Image by Author
 
Right-click on the newly created service and choose Properties.
 
Image by Author
 
Change the Startup type from Manual to Automatic.
 
Finally, click Apply and then click OK.
 

Summary

 
In this tutorial, you have learned how to serve a .NET Core application Windows service. To recap what we have done:
  • We have configured our .NET Core project as a Windows service.
  • We installed the required dotnet SDKs and the corresponding Azure authentication providers.
  • We have deployed the project as a Windows service.
You now have an open playbook to use when you want to deploy .NET Core applications as a service. Hope this tutorial was of help and have a nice day!


Similar Articles