Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

My ASP.NET Core Razor website doesn't open in a browser when I add a BackgroundService

I have created ASP.NET Core Web App (Razor Pages). I start it without any problems and it opens in a browser but when I add a BackgroundService:

public class BackgroundJob : BackgroundService
{
    public BackgroundJob()
    {
    }

    protected async override Task ExecuteAsync(CancellationToken stoppingToken)
    {
        while (!stoppingToken.IsCancellationRequested)
        {

        }
    }
}

    // In Program.cs:
    builder.Services.AddHostedService<BackgroundJob>();

then my website doesn’t want to open in a browser when I run the application (no errors), why?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You didn’t post your complete ExecuteAsync code but I guess you are hitting this issue:

https://github.com/dotnet/runtime/issues/36063

Adding await Task.Yield() to the top of the ExecuteAsync() probably would fix it:

protected async override Task ExecuteAsync(CancellationToken stoppingToken)
{
    await Task.Yield();

    while (!stoppingToken.IsCancellationRequested)
    {

    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading