company_banner

Demystifying the new .NET Core 3 Worker Service

    Premier Developer Consultant Randy Patterson discusses the benefits of using the new Worker Service project template introduced in .NET Core 3.

    .NET Core 3 introduced a new project template called Worker Service. This template is designed to give you a starting point for cross-platform services. As an alternate use case, it sets up a very nice environment for general console applications that is perfect for containers and microservices.



    Some of the benefits of using this template include the following areas.



    Dependency Injection


    The Worker Service template configures a default Dependency injection container, ready for us to use. This is a huge benefit compared to the generic Console template.

    Adding Services involves updating the ConfigureServices method in the Program.cs file:

    Host.CreateDefaultBuilder(args)
       .ConfigureServices((hostContext, services) =>
       {
          services.AddTransient<ICustomerService,CustomerService>();
          services.AddHostedService<Worker>();
       });
    

    Configuration


    The same configuration providers setup for ASP.NET Core are duplicated here for Worker Services. This gives us a powerful and familiar environment for storing configuration information:

    1. appsettings.json
    2. appsettings.{Environment}.json
    3. User Secrets (for development only)
    4. Environment Variables
    5. Command Line arguments

    For additional information on each of the providers please see my previous article posted here.

    Logging


    Likewise, logging providers have been configured to match the default setup for ASP.Net Core, giving you the following providers:

    1. Console
    2. Debug
    3. EventSource
    4. EventLog (only when running on Windows)

    You can modify the logging providers by adding a ConfigureLogging method to the Host object in Program.cs:

    Host.CreateDefaultBuilder(args)
       .ConfigureServices((hostContext, services) =>
       {
          services.AddHostedService<Worker>();
       })
       .ConfigureLogging(logging =>
       {
          logging.ClearProviders();
          logging.AddConsole();
       });
    

    For additional information on logging, please see the documentation for ASP.NET Core.

    Worker Startup Class


    Finally, the Worker.cs file is where the bulk of your code will exist. There are 3 overridable methods from the base class BackgroundService that let you tie into the lifecycle of your application:

    ExecuteAsync – an abstract method used as the main entry point for your application. If this method exits, then your application shuts down.

    StartAsync – A virtual method that, when overridden, is called when the service is starting, and can be used for one-time setup of resources.

    StopAsync – A virtual method that is called when the application is shutting down, and is a good place to release resources and dispose objects.

    Summary


    The new worker service template in .NET Core 3 creates a hosting environment that is well-suited for console applications, microservices, containerized applications, and cross-platform background services. While these benefits can be configured independently of the template, the Worker Service template gives us a consistent startup environment between ASP.NET Core and Console applications.
    Microsoft
    188.21
    Microsoft — мировой лидер в области ПО и ИТ-услуг
    Share post

    Similar posts

    Comments 2

      0
      is perfect for containers and microservices

      Could you show a code to enable WebApi?
      services.AddControllers();

      is that enough?

      Only users with full accounts can post comments. Log in, please.