JoshNaro
JoshNaro

Reputation: 2097

How to detect a shutdown and run code before shutdown execution in MVC .NET

I have a web server cluster (windows 2008) that collects usage data from clients. The servers all batch the data and send after a certain time or amount of data. The problem is we are using AWS auto-scaling, and the machine can be shut down at any time. I would like to detect a shutdown event and send the usage data to the database before the application is killed. Does anyone know if this is possible?

Upvotes: 2

Views: 2529

Answers (3)

Tony O'Hagan
Tony O'Hagan

Reputation: 22672

WebActivator is NuGet package designed to clean up application Start and Shutdown registrations.

This will register your MyStaticClass.Start() and MyStaticClass.Shutdown() methods:

[assembly: WebActivator.PostApplicationStartMethod(typeof(MyStaticClass), "Start")]
[assembly: WebActivator.ApplicationShutdownMethod(typeof(MyStaticClass), "Shutdown")]

Upvotes: 0

Toan Nguyen
Toan Nguyen

Reputation: 11581

You can subscribe to this event and perform your logic before the application is shutdown.

Application.Current.Exit +=DoSomething;

Upvotes: 0

T.S.
T.S.

Reputation: 19330

I would say, you can perform these tasks (in code/ yourself) when your server goes back up.

Otherwise look here http://social.technet.microsoft.com/Forums/windowsserver/en-US/bd8ea190-9bf4-4915-8ed9-96ee5d6f336a/when-are-windows-server-20032008-shutdown-scripts-run-in-the-shutdown-process

Upvotes: 1

Related Questions