{"action":"created","issue":{"url":"https://api.github.com/repos/aspnet/Hosting/issues/871","repository_url":"https://api.github.com/repos/aspnet/Hosting","labels_url":"https://api.github.com/repos/aspnet/Hosting/issues/871/labels{/name}","comments_url":"https://api.github.com/repos/aspnet/Hosting/issues/871/comments","events_url":"https://api.github.com/repos/aspnet/Hosting/issues/871/events","html_url":"https://github.com/aspnet/Hosting/issues/871","id":186199536,"number":871,"title":"Add methods to IApplicationLifetime for firing all events","user":{"login":"davidfowl","id":95136,"avatar_url":"https://avatars.githubusercontent.com/u/95136?v=3","gravatar_id":"","url":"https://api.github.com/users/davidfowl","html_url":"https://github.com/davidfowl","followers_url":"https://api.github.com/users/davidfowl/followers","following_url":"https://api.github.com/users/davidfowl/following{/other_user}","gists_url":"https://api.github.com/users/davidfowl/gists{/gist_id}","starred_url":"https://api.github.com/users/davidfowl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidfowl/subscriptions","organizations_url":"https://api.github.com/users/davidfowl/orgs","repos_url":"https://api.github.com/users/davidfowl/repos","events_url":"https://api.github.com/users/davidfowl/events{/privacy}","received_events_url":"https://api.github.com/users/davidfowl/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2016-10-31T04:12:37Z","updated_at":"2016-10-31T09:32:57Z","closed_at":null,"body":"Today [IApplicationLifetime](https://github.com/aspnet/Hosting/blob/0f1eac5a98c14e63c8528a3e7ab11c61552ba6a6/src/Microsoft.AspNetCore.Hosting.Abstractions/IApplicationLifetime.cs) has 3 events, ApplicationStarted, ApplicationStopping and ApplicationStopped. It also has a way to Stop the application (StopApplication). In order to make this service self contained and replaceable, we should have a way to fire the individual events as well as handle them. That way it would be possible for hosting to use a user provided implementation of `IApplicationLifetime` to trigger the hosting shutdown sequence. See https://github.com/aspnet/Hosting/issues/812#issuecomment-256917236\r\n\r\n```C#\r\npublic interface IApplicationLifetime\r\n{\r\n    CancellationToken ApplicationStarted { get; }\r\n\r\n    CancellationToken ApplicationStopping { get; }\r\n\r\n    CancellationToken ApplicationStopped { get; }\r\n\r\n    void FireApplicationStarted();\r\n\r\n    void FireApplicationStopping();\r\n\r\n    void FireApplicationStopped();\r\n\r\n    void StopApplication();\r\n}\r\n```\r\n\r\nNot sure about the names of things, could be Fire/Trigger or any other verb. This would be 2.0 since it's a breaking change"},"comment":{"url":"https://api.github.com/repos/aspnet/Hosting/issues/comments/257249327","html_url":"https://github.com/aspnet/Hosting/issues/871#issuecomment-257249327","issue_url":"https://api.github.com/repos/aspnet/Hosting/issues/871","id":257249327,"user":{"login":"davidfowl","id":95136,"avatar_url":"https://avatars.githubusercontent.com/u/95136?v=3","gravatar_id":"","url":"https://api.github.com/users/davidfowl","html_url":"https://github.com/davidfowl","followers_url":"https://api.github.com/users/davidfowl/followers","following_url":"https://api.github.com/users/davidfowl/following{/other_user}","gists_url":"https://api.github.com/users/davidfowl/gists{/gist_id}","starred_url":"https://api.github.com/users/davidfowl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davidfowl/subscriptions","organizations_url":"https://api.github.com/users/davidfowl/orgs","repos_url":"https://api.github.com/users/davidfowl/repos","events_url":"https://api.github.com/users/davidfowl/events{/privacy}","received_events_url":"https://api.github.com/users/davidfowl/received_events","type":"User","site_admin":false},"created_at":"2016-10-31T09:32:57Z","updated_at":"2016-10-31T09:32:57Z","body":"Alternatively, we might want to change to a model where you have to implement something like an `IApplicationLifetimeEvents` to get notified of events instead of us exposing cancellation tokens:\r\n\r\n```C#\r\npublic interface IApplicationLifetimeEvents\r\n{\r\n    void OnApplicationStarted();\r\n    void OnApplicationStopping();\r\n    void OnApplicationStopped();\r\n}\r\n```\r\n\r\nWe would need some place to put the StopApplication as it wouldn't be overridable in this model. We could leave it on `IApplicationLifetime`.\r\n\r\n```C#\r\npublic class IApplicationLifetime\r\n{\r\n    void StopApplication();\r\n}\r\n```\r\n\r\nUse code would implement `IApplicationLifetimeEvents` and handle the methods:\r\n\r\n```C#\r\npublic class MyHandler : IApplicationLifetimeEvents\r\n{\r\n    public void OnApplicationStarted()\r\n    {\r\n    }\r\n\r\n    public void OnApplicationStopping()\r\n    {\r\n    }\r\n\r\n    public void OnApplicationStopped()\r\n    {\r\n    }\r\n}\r\n```\r\n\r\nHosting would resolve an `IEnumerable<IApplicationLifetimeEvents>` and invoke the callbacks during the shutdown sequence.\r\n\r\nYou could even implement the interface on your Startup class...\r\n\r\n```C#\r\npublic class Startup : IApplicationLifetimeEvents\r\n{\r\n   public void ConfigureServices(IServiceCollection services) \r\n   { \r\n        // Add the startup class as an event handler\r\n        services.AddSingleton<IApplicationLifetimeEvents>(this);\r\n   }\r\n\r\n   public void Configure(IApplicationBuilder app) \r\n   { \r\n   }\r\n\r\n   // Handle events here\r\n   public void OnApplicationStarted()\r\n   {\r\n   }\r\n\r\n    public void OnApplicationStopping()\r\n    {\r\n    }\r\n\r\n    public void OnApplicationStopped()\r\n    {\r\n    }\r\n}\r\n```\r\n\r\n/cc @Tratcher @pakrym "}}
{"actor":{"display_login":"davidfowl"}}