Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

IApplicationLifetime ApplicationStopping does not trigger if ConfigureServices method exists #151

Closed
davidfowl opened this issue Feb 4, 2015 · 10 comments

Comments

@davidfowl
Copy link
Member

I spent a good few hours trying to track this down on mono and it seems like it happens on windows as well.

using System;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Framework.DependencyInjection;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {

    }

    public void Configure(IApplicationBuilder app, IApplicationLifetime lifetime)
    {
        lifetime.ApplicationStopping.Register(OnStuff);
    }

    public void OnStuff()
    {
        Console.WriteLine("Goodbye!");
    }
}

The above code will not print out goodbye.

@davidfowl davidfowl added this to the 1.0.0-rc1 milestone Feb 4, 2015
@davidfowl
Copy link
Member Author

/cc @Praburaj @Tratcher

@Praburaj
Copy link
Contributor

Praburaj commented Feb 4, 2015

Related: aspnet/HttpSysServer#71

@davidfowl
Copy link
Member Author

Is it? It happens when use kestrel. Seems like a bug in hosting

@Tragetaschen
Copy link
Contributor

On Mono, Kestrel throws an Exception during its Dispose (aspnet/KestrelHttpServer#49). This means the cleanup code in HostingEngine.Start's Disposable does not invoke applicationLifetime.SignalStopped()

@davidfowl
Copy link
Member Author

I'm not sure that's even related, when you remove ConfigureServices, the text "Goodbye!" gets printed

@davidfowl
Copy link
Member Author

Here's my workaround:

using System;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Hosting;
using Microsoft.Framework.DependencyInjection;

namespace WebApplication52
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services, IApplicationLifetime lifetime)
        {
            services.AddInstance(lifetime);
        }

        public void Configure(IApplicationBuilder app, IApplicationLifetime lifetime)
        {
            lifetime.ApplicationStopping.Register(OnStuff);
        }

        public void OnStuff()
        {
            Console.WriteLine("Goodbye!");
        }
    }
}

@Praburaj
Copy link
Contributor

Praburaj commented Feb 4, 2015

okay. Actually these are all problems related to app shutdown .

@davidfowl
Copy link
Member Author

This is specifically related to the fact that something is going bonkers when services are applied. I don't think this has to do with app shutdown per se as everything is functioning. It feels like its triggering the event on some bogus ApplicationLifetime service, rather than the one I'm using. Which is why the above code seems to work.

@Praburaj Praburaj self-assigned this Mar 4, 2015
@Praburaj
Copy link
Contributor

Praburaj commented Mar 4, 2015

Looks like the case. I can see a difference in the ApplicationStopping cancellation token that I receive in ConfigureServices vs Configure. If ConfigureServices is present then hosting additionally does a services.BuildServiceProvider(). I'm investigating this more.

@Praburaj
Copy link
Contributor

Praburaj commented Mar 5, 2015

Fixed 718d923

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants