Owin Shutdown

Need to put some shutdown code in your Owin service? Here’s the non obvious way using CancellationToken.

using System;
using System.Threading;
using System.Threading.Tasks;
using Owin;
using Microsoft.Owin;
using Microsoft.Owin.Host.SystemWeb;
[assembly: OwinStartup(typeof(MyNameSpace.Startup))]
namespace MyNameSpace
public class Startup
public void Configuration(IAppBuilder app)
// Startup code here
var context = new OwinContext(app.Properties);
var token = context.Get<CancellationToken>("host.OnAppDisposing");
if (token != CancellationToken.None)
token.Register(() =>
// Add your shutdown code here

Originally published at www.nexusconcepts.com.

Like what you read? Give Damian a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.