What are .Net Core Worker Services?

0
1897

.Net Core 3 has introduced a new feature which is called as “Worker Services”. Worker services is a new project template that is considered to provide an initial point to write long-running background services in .NET Core. These background services execute the IHostedService interface; therefore, they are called “hosted services”. The peculiar fact about this is that dependency injection is available natively, which was not the case with a Windows Service created with Top Shelf. They can be delivered as a Windows service and also Linux daemons. A .Net core worker service generally performs a periodic background task, for instance sending out notification e-mails. Many modern cloud applications run at least some of them to make sure smooth sailing. These worker services solidify the position of the .NET ecosystem as a background development stack. .Net core background services help the client websites and .Net Core Web Development companies in reducing burden in a significant way.

Steps to create a new worker service application

Prerequisite for creating a new Worker Service in ASP .NET Core 3.0 includes

Some experience and knowledge with .Net Core is essentially required-

  • Visual Studio 2019 Community
  • Docker Desktop. It is optional
  • .NET Core 3.0 SDK. It is included with Visual Studio.
  • An account on SendGrid or a similar e-mail provider

Steps to follow:

First of all, open the Visual Studio. Now let’s create an ASP.Net Core project with the following steps:

Visual Studio 2019

(i)  Launch Visual Studio

  • Now click create a new Project.
  • In the Create new project window, choose Worker Service from the given list of templates.

Create new project

(ii)  Project template selection

  • Now click next.
  • In this Configure your new Project window that displays, specify or set the project name and location for the new project.

Configure your new Project

(iii)  create new project

  • Then select Create.
  • The window Create New ASP.Net Core Web Application will be displayed on the screen.
  • Now select .Net Core as the runtime and ASP.Net Core 3.0 from the dropdown menu at the top.
  • Select worker services as the project template to make a new worker service application.

new worker service

(iv)  worker service template selection

  • Make sure that the checkbox Enable Docker Support is unchecked because the user will not be using this feature here.
  • In the next step ensure that authentication is set as “No Authentication” because you won’t be using authentication either.
  • Click Create.

Some key benefits of .Net Core Worker Services

Dependency Injection

The .Net Worker Service template configures a default Dependency injection container which is ready for the developers to use. This is a great advantage compared to the generic Console template.

Configuration

For .Net Core Worker Services, the same configuration provider setup for ASP.NET Core are copied here. It eventually provides the developer with a stable and familiar environment for storing configuration-related information such as Logging. Likewise, logging providers have been configured to match the default setup for ASP.Net Core, providing developers with the following providers:

  • Console
  • Debug
  • EventSource
  • EventLog (Applicable only when running on Windows)

Worker Startup Class

The Worker.cs file is the place where the bulk of developer code exists. Three overridable procedures from the base class BackgroundService let developer tie into the lifecycle of their application:

  • ExecuteAsync – This is an abstract method used as the main entry point for the user application. If this method exists, then an application shuts down.
  • StartAsync – This is a virtual method when overridden, is called when the service is starting and can be used for a one-time setup of resources.
  • StopAsync – This is a virtual method which is called at the time the application is shutting down and is an excellent place to release resources.

Wrapping up 

The new worker service template in .NET Core 3 creates a hosting environment that is well-suited for console applications, containerized applications, cross-platform background services, and microservices. While these advantages can be configured independently of the template, the .Net Core Worker Service template provides us with a consistent start-up environment between ASP.NET Core and Console applications.