Need to create sample worker service in .Net 6.0

Sachi 221 Reputation points
2021-12-19T05:14:54.253+00:00

Can anyone help me to create a sample worker service in .Net 6.0 ? If any sample is there please provide me

Developer technologies C#
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2021-12-19T08:57:51.63+00:00

    Did you see the doc/sample at : Worker Services in .NET ?


  2. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2021-12-19T14:38:26.707+00:00

    See the following GitHub repository and article which is not .NET 6 specific as Worker Service projects have been around since .NET Core 3.0. And if you have a Pluralsight account see Building ASP.NET Core Hosted Services and .NET Core Worker Services which 202 people gave a five star rating.


  3. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-12-19T17:31:00.533+00:00

    I commented the sample code

    namespace App.WorkerService
    {
        public class Worker : BackgroundService
        {
            private readonly ILogger<Worker> _logger;
    
            public Worker(ILogger<Worker> logger)
            {
                _logger = logger;
            }
    
            // this is call to run your service code
            protected override async Task ExecuteAsync(CancellationToken stoppingToken)
            {
                // loop until asked to quit
                while (!stoppingToken.IsCancellationRequested)
                {
                    _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
    
                    // place you code here
                    // typically the first step is a call (poll)  for work
    
                    // add a delay for loop if required 
                    await Task.Delay(1000, stoppingToken);
                }
            }
        }
    }
    

  4. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-12-20T15:48:10.683+00:00

    You should show the code with the error. The docs are pretty clear

    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/?view=aspnetcore-6.0#log-event-id


  5. Sachi 221 Reputation points
    2021-12-22T15:59:28.517+00:00

    Hi Team,
    Can anyone tell me what is dockerfile option while creating worker service in .net 6.0 and also how to use it and why we need to use it ?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.