aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

.NET Core Worker not call ExecuteTask

Open carloscds opened this issue 7 years ago • 11 comments

Describe the bug

I have create a worker using "dotnet new worker" but using code bellow no events are written to EventLog:

program.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.EventLog;

namespace CoreWindowsService
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .UseServiceBaseLifetime()
                .ConfigureLogging(loggerFactory => loggerFactory
                    .AddEventLog(new EventLogSettings() {SourceName = "CoreWindowsService"}))
                .ConfigureServices(services =>
                {
                    services.AddHostedService<Worker>();
                });
    }
}

worker.cs

using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace CoreWindowsService
{
    public class Worker : BackgroundService
    {
        private readonly ILogger<Worker> _logger;

        public Worker(ILogger<Worker> logger)
        {
            _logger = logger;
        }

        public override async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation($"Iniciado: {DateTime.Now}");
            await Task.CompletedTask;
        }

        public override async Task StopAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation($"Parado: {DateTime.Now}");
            await Task.CompletedTask;
        }

        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while (!stoppingToken.IsCancellationRequested)
            {
                _logger.LogInformation($"Executando: {DateTime.Now}");
                await Task.Delay(1000, stoppingToken);
            }
        }
    }
}

Attaching debug session to installed service, nothing happen!

carloscds avatar Mar 31 '19 02:03 carloscds

I have discover that problem is StartAsync() and StopAsync() methods. This happen because I used override on methods, change to:

public async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation($"Iniciado: {DateTime.Now}");
            await Task.CompletedTask;
        }

        public async Task StopAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation($"Parado: {DateTime.Now}");
            await Task.CompletedTask;
        }

Works fine!

But I receive an alert about "override"!

carloscds avatar Mar 31 '19 02:03 carloscds

Yes this is because you need to call the base in StartAsync and StopAsync (which I agree is a bit unclear). Changing this wild be a breaking change at this point so I’m not sure how much we’d be willing to do here

davidfowl avatar Mar 31 '19 07:03 davidfowl

@davidfowl Maybe you can change sample at blog post including Start and Stop calls!

carloscds avatar Mar 31 '19 22:03 carloscds

@davidfowl Now it's working:

 public override async Task StartAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation($"Iniciado: {DateTime.Now}");
            await base.StartAsync(cancellationToken);
        }

        public override async Task StopAsync(CancellationToken cancellationToken)
        {
            _logger.LogInformation($"Parado: {DateTime.Now}");
            await base.StopAsync(cancellationToken);
        }

carloscds avatar Mar 31 '19 23:03 carloscds

@carloscds - should this issue be closed? Or do you have any further questions?

Eilon avatar Apr 01 '19 18:04 Eilon

Do you mean my blog posts about running as a Windows Service? I am happy with that.

@Eilon I think we add the doc label to this to make sure we have a note in any official docs about this scenario. I'll call it out in an up-coming blog post to help anyone else who gets caught by it.

glennc avatar Apr 01 '19 18:04 glennc

@Eilon Yes, you can close. @glennc Yes, I read you blog post. Very usefull, maybe you can add Start/Stop in you sample!

carloscds avatar Apr 02 '19 17:04 carloscds

Oh I guess we'll keep open because this is a doc issue now.

Eilon avatar Apr 02 '19 18:04 Eilon

@anurse can you assign?

Pilchie avatar Sep 26 '19 18:09 Pilchie

Sure, I assign it to myself :). https://github.com/aspnet/AspNetCore.Docs/pull/14642

analogrelay avatar Sep 26 '19 18:09 analogrelay

@anurse looks like the docs article is done. Should this be closed?

pranavkm avatar Oct 17 '19 13:10 pranavkm