RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Automatically Upgrade Your .NET Applications On-the-Fly : Page 3

Use the class library described in this article to add automatic upgrading to your .NET application.

Introducing the AutoUpgrade Class
The AutoUpgrade class provides a framework for adding automatic updates to your rich client application, with just a few lines of code. The AutoUpgrade class was written to provide automatic upgrade support for applications where Web deployment is unsuitable.

To use the code, you reference the AutoUpgrade class library (AutoUpgrade.Lib.dll) from your application, and use its properties and methods to control automatic upgrades. You also need the AutoUpgrade stub program (AutoUpgrade.exe), which runs the automatic upgrade. The stub is required because .NET locks assemblies when they are in use. The StartUpgradeStub method of the AutoUpgrade class invokes the stub automatically from within the IsUpgradeAvailable method when the blnStartUpgrade argument is True.

How It works
The AutoUpgrade class works by comparing the file versions or last modified dates of files on your local file system with data from an XML-based "manifest" file downloaded from a Web server. You can generate the manifest file automatically using the AutoUpgrade class. That creates a list of all of the files in your application, their version numbers (or last modified dates), instructions on how to check for updated versions, and actions to take when a new version of the file is available.

You use the AutoUpgrade framework (encapsulated in AutoUpgrade.Lib.dll) to create the manifest, perform the file version and date comparisons to determine the need for an upgrade, and to perform the upgrade.

Adding Auto-upgrade to Your Application
Here's the procedure to add AutoUpgrade capability to your application.

1. Add a reference to AutoUpgrade.Lib.dll. In the Visual Studio solution explorer, right-click references, Add reference then click browse and locate AutoUpgrade.Lib.dll.

2. Create a virtual directory. Create a virtual directory on your Web server from which your application will get the new versions of your application's files. (You will need to change the code sample in step 3 to refer to the virtual directory you are creating). If your application already uses Web services or Active Server Pages, you can use the same virtual directory you use for your application server.

3. Add code to your application to check for new versions of files. Your application needs to create an instance of the AutoUpgrade class and pass in information on the location of the manifest file, call IsUpgradeAvailable and terminate and pass control to the AutoUpgrade stub executable if required. For example:
   Sub Main
      Dim upgAuto As AutoUpgrade()
      ' In a real application, the path would be 
      ' read from a configuration file or the registry
      upgAuto = AutoUpgrade.Create _
     ' The 'True' argument to the IsUpgradeAvailable 
     ' function tells AutoUpgrade to automatically 
     ' start the upgrade stub if an upgrade is available
     If not upgAuto.IsUpgradeAvailable(True) Then
       ' Start the application
       With new MyApp.MyForm
         ' Start a message loop. We need this because 
         ' we're starting from a sub Main()
       End With
       ' Terminate the application. 
     End If
   End Sub
4. Compile and deploy your application in a test environment, then update components and re-compile. To test the automatic upgrade, you will need to deploy an "old" version of your application to a test environment, and then recompile your application to update the assembly versions.

Author's Note: If you're using automatic version numbering in Visual Basic .NET, you will have to close Visual Studio and re-load your project to get the version number to update (the C# compiler updates version numbers on each compilation).

5. Copy an image of your client application to the virtual directory. Make sure the directory structure on the Web server matches that of a deployed application. Create a "client" subdirectory in your virtual directory and copy your files there.

6. Copy AutoUpgrade.exe and AutoUpgrade.Lib.dll to the "client" subdirectory. When the AutoUpgrade framework detects an upgrade, the AutoUpgrade class saves the updated manifest file and executes AutoUpgrade.exe, which automatically downloads the updated files from your source location to the upgrade cache directory, then replaces your application files You should place the files AutoUpgrade.exe and AutoUpgrade.Lib.dll in your "client" subdirectory.

7. Create a manifest file for your "new" version. Initially you can create a manifest file automatically by navigating to your virtual directory path from the command prompt and executing the command:
   AutoUpgrade [path] [target]
In the preceding method call, [path] is the path to search for files, and [target] is the file name where you want to save the manifest file. For example (from your virtual directory's root directory):

   client\AutoUpgrade client mymanifest.xml
This syntax invokes the GenerateManifest() method, which writes entries for all files that default to the copyFile action. You can set additional options by editing the manifest file in an XML editor or notepad.

After completing these steps, run your application from the location you specified in step 4. AutoUpgrade will check the entries in the manifest file against your current installation, and will launch an automatic upgrade.

For "live" installations, your server setup could automatically:

  • Create the virtual directory creation using ADSI (refer to the .NET framework documentation on the System.DirectoryServices namespace).
  • Install an "image" of your client application in the virtual directory.
  • Automatically generate a manifest file, or install one you prepare as part of your build process.
The Installer Class
.NET applications can inherit the System.Configuration.Install.Installer class to perform custom actions at installation time (in a Visual Studio deployment project, you can create custom actions to run instances of the Installer class at install time).

AutoUpgrade calls all instances of the Installer class marked with the RunInstaller(true) attribute by using the AssemblyInstaller object in the System.Configuration.Install namespace.

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