Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

builder.Services.AddHostedService<TimerBackgroundService>(); has no effect #41860

Closed
1 task done
nssidhu opened this issue May 26, 2022 · 7 comments
Closed
1 task done
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions

Comments

@nssidhu
Copy link

nssidhu commented May 26, 2022

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

In a Blazor MAUI Hybrid Project.
I was trying to run a service in the background using the following command

builder.Services.AddSingleton<TimerBackgroundService>();
builder.Services.AddHostedService(sp => sp.GetRequiredService<TimerBackgroundService>());

This has no effect whatsoever, even the constructor of TimerBackgroundService does not run

All the examples i saw were for server side scenario, not sure if this is supported in Blazor wasm or Blazor Hybrid Porjects.

If it is supported, why is it not executing .

Here is the actual background service

using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SchedulingMobile.Services
{
    public class TimerBackgroundService : BackgroundService
    {
        public TimerBackgroundService()
        {
            Console.WriteLine("Instance Created");
        }
        public static event Func<DateTime, Task> UpdateEvent;
        protected override async Task ExecuteAsync(CancellationToken stoppingToken)
        {
            while(!stoppingToken.IsCancellationRequested)
            {
                await Task.Delay(TimeSpan.FromSeconds(10)); //Every 1 minute
                await UpdateEvent(DateTime.Now);
            }
        }

        public override Task StartAsync(CancellationToken cancellationToken)
        {
            return base.StartAsync(cancellationToken);
        }
    }
}

Expected Behavior

Was expecting the TimerBackground service code to execute.

Steps To Reproduce

Add the
builder.Services.AddSingleton();
builder.Services.AddHostedService(sp => sp.GetRequiredService());

in Blazor Wasm or BlazorHyrbid Project

Exceptions (if any)

None

.NET Version

6.0.300

Anything else?

No response

@adityamandaleeka
Copy link
Member

This seems like an issue with MauiAppBuilder. @Redth can you please help route this?

@halter73
Copy link
Member

halter73 commented Jun 1, 2022

@eerhardt I wonder if MauiAppBuilder can start using HostApplicationBuilder with DisableDefaults so we're not automatically trying to load appsetttings.json or anything. Is there any reason we couldn't or shouldn't? Updating WebApplicationBuilder was pretty painless but I had the benefit of designing HostApplicationBuilder with that goal in mind.

@eerhardt
Copy link
Member

eerhardt commented Jun 1, 2022

If it is supported, why is it not executing .

It isn't supported. See reasoning here:

dotnet/maui#4393

Specifically (4), which I copied here:

Extensions.Hosting's main concept is its "Hosted Services" or BackgroundService. While this is definitely a useful concept in a client application, the abstraction isn't correct for client apps. Hosted Services get "Start" and "Stop". But what if the app is getting deactivated? Can the service tell the difference between that and the app being closed? Should all the Hosted Services get resumed when the app is resumed? These are all scenarios that would need to work and the Hosted Service abstraction isn't designed for these scenarios. Maui apps that need background service functionality need to use something that is designed for client apps.

@eerhardt
Copy link
Member

eerhardt commented Jun 1, 2022

There are other ways to enable this scenario, though. One example is https://github.com/shinyorg/shiny. I've never personally used it, but my understanding is that it works well.

@nssidhu
Copy link
Author

nssidhu commented Jun 1, 2022

Shiny looks promising, but version specific to MAUI is in alpha release, the stable ones are for Xamarin platform.

@nssidhu nssidhu closed this as completed Jun 1, 2022
@aritchie
Copy link

aritchie commented Jun 3, 2022

You don't want a hosted service. ASP.NET will wait for all hosted services to start - android & ios apps cannot. What you really want is an IMauiInitializeService or yes, Shiny also has IShinyStartupTask which is similar. Both of these concepts can be found here: https://github.com/shinyorg/shiny/tree/master/src/Shiny.Hosting.Maui

@nssidhu
Copy link
Author

nssidhu commented Jun 3, 2022

@aritchie
when do you think shiny version 3 will be released ?

@dotnet dotnet locked as resolved and limited conversation to collaborators Jul 3, 2022
@amcasey amcasey added area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions and removed area-runtime labels Aug 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-networking Includes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractions
Projects
None yet
Development

No branches or pull requests

7 participants