HTTP-service (dotnet process) does not shut down on desktop-app exit/quit #435

Closed
opened 2026-01-29 16:39:35 +00:00 by claunia · 2 comments
Owner

Originally created by @hlubovac on GitHub (Dec 19, 2019).

Originally assigned to: @GregorBiswanger on GitHub.

I noticed this on Mac. Whether I close or quit the desktop window, the HTTP service does not shut down.

Does the mechanism that I'm looking for exist and doesn't reliably function, or is it not there at all? Am I missing a part of configuration that relates? I don't believe that I have anything within my dotnet app that might be non-standard or in a way to preventing the backend console to die.

Thank you.
Hari

Originally created by @hlubovac on GitHub (Dec 19, 2019). Originally assigned to: @GregorBiswanger on GitHub. I noticed this on Mac. Whether I close or quit the desktop window, the HTTP service does not shut down. Does the mechanism that I'm looking for exist and doesn't reliably function, or is it not there at all? Am I missing a part of configuration that relates? I don't believe that I have anything within my dotnet app that might be non-standard or in a way to preventing the backend console to die. Thank you. Hari
claunia added the bug label 2026-01-29 16:39:35 +00:00
Author
Owner

@kk-code-lab commented on GitHub (Dec 22, 2019):

I've had the same problem on linux net core v3.1 and solved it by using OnClosed event from BrowserWindow object.

I injected IHostApplicationLifetime object in the Configure method and used StopApplication() when browser window called OnClosed event.

IHostApplicationLifetime is new in net core 3.0, before it was called IApplicationLifetime (ms doc)

For example:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();
    app.UseRouting();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{controller=Home}/{action=Index}/{id?}");
    });

    Task.Run(() => BootstrapElectron(lifetime));
}

private async Task BootstrapElectron(IHostApplicationLifetime lifetime)
{
    var browserWindow = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions()
    {
        Show = false
    });

    browserWindow.OnReadyToShow += () =>
    {
        browserWindow.Show();
    };

    browserWindow.OnClosed += lifetime.StopApplication;
}
@kk-code-lab commented on GitHub (Dec 22, 2019): I've had the same problem on linux net core v3.1 and solved it by using `OnClosed` event from `BrowserWindow` object. I injected `IHostApplicationLifetime` object in the `Configure` method and used `StopApplication()` when browser window called `OnClosed` event. `IHostApplicationLifetime` is new in net core 3.0, before it was called `IApplicationLifetime` [(ms doc)](https://docs.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#migrate-startupconfigure) For example: ```csharp public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IHostApplicationLifetime lifetime) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); Task.Run(() => BootstrapElectron(lifetime)); } private async Task BootstrapElectron(IHostApplicationLifetime lifetime) { var browserWindow = await Electron.WindowManager.CreateWindowAsync(new BrowserWindowOptions() { Show = false }); browserWindow.OnReadyToShow += () => { browserWindow.Show(); }; browserWindow.OnClosed += lifetime.StopApplication; } ```
Author
Owner

@hlubovac commented on GitHub (Dec 22, 2019):

Thank you very much. That works flawlessly.

Previously, I had HTTP services remaining active in the background, and growing in number over time (as I would keep re-launching the app) - so, that wouldn't have looked good long-term.

Happy holidays!

@hlubovac commented on GitHub (Dec 22, 2019): Thank you very much. That works flawlessly. Previously, I had HTTP services remaining active in the background, and growing in number over time (as I would keep re-launching the app) - so, that wouldn't have looked good long-term. Happy holidays!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#435