Browse DevX
Sign up for e-mail newsletters from DevX


Resuscitate Your .NET Programs with the Application Recovery and Restart API

Please your users by making your applications recover intelligently and restart smoothly after failures with this .NET-savvy integration wrapper for the Application Recovery and Restart API.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

icrosoft has gone to great lengths to help you make your software applications more appealing to users. You can create rich user experiences with Windows Presentation Foundation, smooth web applications with ASP.NET AJAX, data friendly applications with LINQ, and much more. But when it comes to appeal, nothing beats an application that is always available and doesn't suffer from data loss or corruption. Microsoft's Application Recovery and Restart (ARR) API makes it easier than ever to add this level of appeal to your application. ARR gives you an opportunity to recover data and return to a consistent state when your application terminates or hangs, and it will optionally restart your application.

Application recovery is the process of saving and recovering the state of your application when it terminates unexpectedly or hangs. Application restart is the process of launching an application after it has terminated unexpectedly. The restart functionality in Application Recovery and Restart is independent of the recovery functionality, but the two complement each other well. You may recover an application without automatically restarting it, and you may automatically restart an application without recovering its data.

Microsoft released ARR with Windows Vista, but it's particularly relevant in Windows Server 2008, because server applications typically require higher reliability and stability, and servers rarely have a human present to address problems when they arise.

To explore ARR, it's easiest to start with an application destined to fail. You'll need Visual Studio 2005 or 2008 running on Windows Vista or Windows Server 2008 to build and run the sample "DevX.ServerApplication" application described in this article. If you haven't yet upgraded to one of these operating systems, but you have an MSDN subscription, you can use Virtual PC 2007 to create a virtual machine test lab.

Listing 1 shows how DevX.ServerApplication exercises the Application Recovery and Restart API.

Figure 1. DevX.ServerApplication in Action: The sample application exercises the Application Recovery and Restart API.
DevX.ServerApplication is a .NET console application (see Figure 1). Microsoft didn't expose Application Recovery and Restart in the .NET Framework, so the first step to using it with .NET is writing a class to expose the relevant kernel32.dll functions. It turns out that Application Recovery and Restart is one of the more difficult APIs to marshal properly, so you can save valuable time by adding the ApplicationRecoveryRestart class to your application:

using System; using System.Runtime.InteropServices; using System.Text; namespace DevX.ServerApplication { public static class ApplicationRecoveryRestart { public delegate Int32 ApplicationRecoveryCallbackDelegate( RecoveryInformation parameter); [DllImport("kernel32.dll")] public static extern void ApplicationRecoveryFinished( Boolean success); [DllImport("kernel32.dll")] public static extern Int32 ApplicationRecoveryInProgress( out Boolean canceled); [DllImport("kernel32.dll")] public static extern Int32 GetApplicationRecoveryCallback( IntPtr process, out ApplicationRecoveryCallbackDelegate recoveryCallback, out RecoveryInformation parameter, out UInt32 pingInterval, out UInt32 flags); [DllImport("kernel32.dll")] public static extern Int32 GetApplicationRestartSettings( IntPtr process, StringBuilder commandLine, ref UInt32 size, out UInt32 flags); [DllImport("kernel32.dll")] public static extern Int32 RegisterApplicationRecoveryCallback( ApplicationRecoveryCallbackDelegate recoveryCallback, RecoveryInformation parameter, UInt32 pingInterval, UInt32 flags); [DllImport("kernel32.dll")] public static extern Int32 RegisterApplicationRestart( String commandLineArgs, UInt32 flags); [DllImport("kernel32.dll")] public static extern Int32 UnregisterApplicationRecoveryCallback(); [DllImport("kernel32.dll")] public static extern Int32 UnregisterApplicationRestart(); } }

With the Application Recovery and Restart API exposed to .NET, you're ready to write server applications capable of recovering from failures and restarting.

Thanks for your registration, follow us on our social networks to keep up-to-date