skip to content
Stylized letter A in teal with pink sparkle accents Khalid Abuhakmeh

Looking At ASP.NET Core's IApplicationLifetime

/ 3 min read

There is a lot to learn with the new ASP.NET Core framework, and while a lot of what I know from the previous version of ASP.NET carries over, there are exciting new features that I can’t help but explore. IApplicationLifetime is one of those interfaces that begs for further inspection.

What Is IApplicationLifetime?

IApplicationLifetime is a new interface in ASP.NET Core under the Microsoft.AspNetCore.Hosting namespace. The interface is designed to give a developer the opportunity to gracefully startup but mostly shutdown the application. It is comprised of three properties and one method respectively: ApplicationStarted, ApplicationStopping, ApplicationStopped, and StopApplication().

public interface IApplicationLifetime
{
CancellationToken ApplicationStarted { get; }
CancellationToken ApplicationStopping { get; }
CancellationToken ApplicationStopped { get; }
void StopApplication();
}

The IApplicationLifetime is already part of the services collection in ASP.NET Core, so you just need to request it as a dependency.

IApplicationLifetime CancellationTokens

The three properties are CancellationTokens meant to define events with the application’s lifetime. As a developer, I can register Action methods to execute when the application is invoking these events. A great example comes from the Serilog library.

public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerfactory,
IApplicationLifetime appLifetime)
{
loggerfactory.AddSerilog();
// Ensure any buffered events are sent at shutdown
appLifetime.ApplicationStopped.Register(Log.CloseAndFlush);
// ... more stuff
}

The example shows the registering of an Action with the IApplicationLifetime.ApplicationStopped event and flushing all remaining log messages to a more permanent location.

The comments on all the CancellationTokens read as follows:

  • ApplicationStarted: Triggered when the application host has fully started and is about to wait for a graceful shutdown.
  • ApplicationStopping: Triggered when the application host is performing a graceful shutdown. Requests may still be in flight. Shutdown will block until this event completes.
  • ApplicationStopped: Triggered when the application host is performing a graceful shutdown. All requests should be complete at this point. Shutdown will block until this event completes.

IApplicationLifetime StopApplication

The method StopApplication shuts down the application when called. The comment reads.

Requests termination the current application.

Personally, I can’t imagine ever needing to call this method, but I could imagine it being useful for managing a cluster of applications that need to tell each other to turn off. That said, it may make sense to do that at a system call level, rather than an application level.

Conclusion

IApplicationLifetime is a simple yet powerful interface for managing the lifetime of my application. It is one more point of extensibility that allows me to manage resources, gracefully shutdown, and make sure I am a good developer citizen. If you are using IApplicationLifetime in a current project, I’d love to hear about it.

Related Posts