TemplateStudio icon indicating copy to clipboard operation
TemplateStudio copied to clipboard

IHostApplicationLifetime never invoke shutdown callbacks in WinUI templates

Open katdti opened this issue 3 years ago • 1 comments
trafficstars

Describe the bug

Callbacks on the IHostApplicationLifetime interface is never invoked when the App is closed.

To Reproduce

  1. Create a service that uses IHostApplicationLifetime
public class DummyService
{
    public DummyService(IHostApplicationLifetime hostApplicationLifetime)
    {
        hostApplicationLifetime.ApplicationStopping.Register(() =>
        {
            // This code is never called
        });
    }
}
  1. Configure service in app.xaml.cs
        .ConfigureServices((context, services) =>
        {
            services.AddSingleton<DummyService>();
        }
  1. Run the App and close it
  2. Result: The registered callback is never called and any background services added with services.AddHostedService<MyHostedService>(); is never started.

Additional context

No response

Applies to the following platforms:

  • [X] WinUI
  • [ ] WPF
  • [ ] UWP

About your setup

  • Visual Studio Version: 17.2.5
  • Template Studio Wizard Version: 5.1
  • Windows Build: 19044

katdti avatar Jul 05 '22 11:07 katdti

I believe these events are only raised if you use Start/Stop/Run on the Host to control the application lifecycle, which the templates do not.

mikebattista avatar Jul 05 '22 15:07 mikebattista