Ok The answer was in the question:
Yes, I can use the WebJobsShutdownWatcher class because it has a function Registerthat is called when the cancel marker is canceled, in other words, when the webjob stops.
static void Main()
{
var cancellationToken = new WebJobsShutdownWatcher().Token;
cancellationToken.Register(() =>
{
Console.Out.WriteLine("Do whatever you want before the webjob is stopped...");
});
var host = new JobHost();
host.RunAndBlock();
}
EDIT (based on Matthew's comment):
If you use Triggered functions, you can add a parameter CancellationTokento your function signatures. Runtime cancels this token when the host automatically shuts down, which allows your function to receive a notification.
public static void QueueFunction(
[QueueTrigger("QueueName")] string message,
TextWriter log,
CancellationToken cancellationToken)
{
...
if(cancellationToken.IsCancellationRequested) return;
...
}
source
share