Click here to Skip to main content
11,492,385 members (62,015 online)
Click here to Skip to main content

Using Updater Block

, 12 Apr 2005 172.9K 1.3K 109
Rate this:
Please Sign up or sign in to vote.
A vanilla implementaion showing how Updater Block 2.0 can be included in a Windows Forms application.

Introduction

I had been trying to include updater block in my application. The quick starts that came along with the application block had some real neat examples, but it took quite some time to figure out what files must be placed where, what settings needed to be made and how, to get a sample of my own working. So, I thought I would post the sample implementation that I generated, along with a step by step guide so that others who will be trying this point onwards need not struggle much.

Note: A good tutorial for updater block version 1.0 can be found here.

The example below tries to show the usage for the version 2.0 of the Updater block.

Prerequisites

  • The Enterprise library and the Updater application block must be downloaded and installed. The enterprise library can be downloaded from here.
  • The updater block can be downloaded from here.

System Requirements

  • Supported Operating Systems: Windows XP.
  • Windows XP Professional.
  • Internet Information Server version 5.0 or later.
  • Microsoft .NET FrameWork version 1.1 SDK.
  • Microsoft Visual Studio 2003.
  • Background Intelligent Transfer Service (BITS) (for the out-of-the-box downloader to function correctly).

Steps

  1. Create a simple Windows form application .
    • Open VS .NET editor and select File -> New Project -> Windows Application.
    • Change the name to SampleApplication.
    • Change the name of the default form generated to SampleForm.cs
    • Add a label on the form SampleForm and set its text to “Version 1.0.0.0”
    • Add a button to the form and change its text to “Exit”. Add the event handler to exit the application - Application.Exit();.
    • Add a listbox called eventList.
    • Open the AssemblyInfo.cs and change the version of the application to 1.0.0.0: [assembly: AssemblyVersion("1.0.0.0")]
    • Change the line in Main () body to Application.Run(new SampleForm());.
    • Add app.config to the application: Add -> New Item -> Application Configuration File.
    • Build the application.
  2. Add the updater application block to the application.
    • The three assemblies that have to be referenced are present under the folder C:\Program Files\Microsoft Patterns & Practices\Updater Application Block 2.0\src\cs (if the default settings were chosen during the installation of the updater block).
    • The three DLLs can be found in the bin/debug folders of the ActivationProcessors, Downloaders and Updater projects present under the common folder mentioned above.
    • Add refernce to the three DLLs to the created sample application:
      1. Microsoft.ApplicationBlocks.Updater.ActivationProcessors.dll
      2. Microsoft.ApplicationBlocks.Updater.Downloaders.dll
      3. Microsoft.ApplicationBlocks.Updater.dll
    • Add the code, present at the end of the document to the application that forms the body for handling the download (from the #region Auto-Update Stuff To #endregion).

      Note: The downloadable source has this content, but what the heck.

    • Add the following code to the constructor of the SampleForm Class to start off the updater manager:
      InitializeAutoUpdater ();
  3. Update the App.Config file using Enterprise library Configuration Tool.
    • Open the tool from Start -> All Programs -> Microsoft Patterns and Practices -> Updater Application Block V2 -> Enterprise Library Configuration Tool.
    • In the Configuration tool console, select File -> Open Application and browse to the App.Config file, select and click Open.
    • A new item “Application” appears under the root “Enterprise Library Configuration”.
    • Rightclick Application and select New –> Updater Application Block Configuration.
    • Configuration, Cryptography and Updater Application Blocks are added.
    • Update the properties of the application block:
      1. ApplicationID: SampleApplication.
      2. BasePath: C:\Temp\UpdaterTest\UAB.
      3. ManifestUri: http://localhost/ApplicationBlockServer/ServerManifest.xml.

        Note: The ManifestUri will point to a URL that has to be created later.

    • Rightclick Updater Application Block and select New -> Downloaders.
    • Rightclick Downloaders and select New -> BITSDownloader.
    • Save.
  4. Deploy the Client
    • Build the SampleApplication project.
    • Copy the output of the project from the bin/Debug folder to the folder C:\Temp\UpdaterTest. The files to be copied are:
      1. All the DLLs including the following:
        1. Microsoft.ApplicationBlocks.Updater.ActivationProcessors.dll
        2. Microsoft.ApplicationBlocks.Updater.dll
        3. Microsoft.ApplicationBlocks.Updater.Downloaders.dll
      2. The application exe: SampleApplication.exe
      3. The config file: SampleApplication.exe.config
    • Copy the files updaterconfiguration.config and securitycryptographyconfiguration.config present in the root folder of the project to the folder UpdaterTest.
  5. Change the project to create the server application.

    The server application will be a newer version of the application deployed at c:\temp\ UpdaterTest.

    • Open the SampleApplication solution in VS.NET editor.
    • Open SampleForm in designer mode and make noticeable changes on the form. Change the label text to “Version 1.0.0.1” and the form text to “Sample Form New Version”.
    • Open AssemblyInfo.cs and change the assembly version to 1.0.0.1: [assembly: AssemblyVersion("1.0.0.1")].
    • Build the application.
  6. Deploy the Server Application.

    The new version of the application will be deployed at the server, from where it can be downloaded.

    • Create a folder called ApplicationBlockServer under C:\inetpub\wwwroot.
    • Open IIS manager (Start -> Run -> Inetmgr).
    • In the IIS manager, navigate to Default Web Site.
    • Rightclick on Default Web Site and select “New -> Virtual Directory”.
    • Create a virtual directory with alias ApplicationBlockServer and the folder pointing to ApplicationBlockServer folder created above.
    • Create a new folder called 1.0.0.1 under the folder c:\inetpub\wwwroot\ ApplicationBlockServer.
    • Copy the file SampleApplication.exe built in the previous step to the newly created folder 1.0.0.1.
  7. Build the Manifest file for the server
    • Open the Manifest Editor tool that was also installed along with the updater block (Start -> All Programs -> Microsoft Patterns and Practices -> Updater Application Block V2 -> Manifest Editor Tool.
    • Under Manifest Properties Tab, click the Generate button and generate the manifest id. Check the Mandatory check box.
    • Enter the description “Sample Application Version 1.0.0.1”.
    • In the application properties tab, enter the application id as “SampleApplication” and location as “.”
    • The entry point is “SampleApplication.exe”.
    • The File URI is http://localhost/ApplicationBlockServer/1.0.0.1.
    • The source folder is “C:\Inetpub\wwwroot\ApplicationBlockServer\1.0.0.1”.
    • Click the “Add” button and select SampleApplication.exe from the 1.0.0.1 folder. The file must appear in the Files list.
    • In the activation process tab, enter the process name as AppDeploy and processor type as “Application Deploy” and press “Add”.
    • Enter process name as “WFE” and processor type as “Wait For Exit” and press Add. A new window prompts to add a process name. Press Cancel.
    • Click the Validate button to validate the generated manifest.
    • Click the Save button to Save the manifest at “C:\Inetpub\wwwroot\ApplicationBlockServer” as “ServerManifest.xml”.
  8. Run the application at “C:\Temp\UpdaterTest”.

    The application, after some time, will pops up a dialog box telling that a newer version is available and do you want to download it. Click yes. The newer version will be downloaded which will be displayed in the list box eventList. After downloading, it will prompt you to restart the application. Close the application and start it again. The version 1.0.0.1 will open up.

    Addendum:

    • Once the update of the application happens, a folder called UAB gets created in the UpdaterTest folder. This folder gives the information about the latest version present.
    • From what I could understand, the manifest ID is maintained at the server and if an update occurs for the client, the manifest ID is placed in the UAB folder. If the manifest ID of the server is different from that of the client (or if the client does not have the UAB folder, meaning no updates have yet taken place), the updater block swings into action.
    • To deploy a newer version of the application on the server, create a new folder 1.0.0.2 and dump all the files at that location (the naming convention is just a convention for convenience, not a rule) and generate a new server manifest file with a new Manifest ID and replace the old manifest file.
    • I have not checked the auto update feature, i.e. the client can be scheduled to ping the server at regular intervals. But this must be straight forward as setting some property somewhere.
    • The code below has a thread to ping the server every 10 seconds. That part can be removed.

Hey, this was just a vanilla implementation.

Auto Update Code to be pasted in the application

#region Auto-Update Stuff
 
    private Thread pollThread = null;
    private int manifestDownloaded = 0;
            
    private void CheckAndUpdate()
    {
       try
       {
          // Get the updater manager
          ApplicationUpdaterManager updater = 
              ApplicationUpdaterManager.GetUpdater();
                        
          // Subscribe for various events
          updater.DownloadStarted +=
              new DownloadStartedEventHandler(updater_DownloadStarted);
          updater.DownloadProgress += 
              new DownloadProgressEventHandler(updater_DownloadProgress);
          updater.DownloadCompleted += 
              new DownloadCompletedEventHandler(updater_DownloadCompleted);
          updater.DownloadError += 
              new DownloadErrorEventHandler(updater_DownloadError);
          updater.ActivationInitializing += 
              new ActivationInitializingEventHandler(updater_ActivationInitializing);
          updater.ActivationStarted += 
              new ActivationStartedEventHandler(updater_ActivationStarted);
          updater.ActivationInitializationAborted += 
              new ActivationInitializationAbortedEventHandler(
              updater_ActivationInitializationAborted);
          updater.ActivationError += 
              new ActivationErrorEventHandler(updater_ActivationError);
          updater.ActivationCompleted +=
              new ActivationCompletedEventHandler(updater_ActivationCompleted);
 
          // Loop till the updates are available
 
           Manifest[] manifests = null;
           while(true)
           {
              manifests = updater.CheckForUpdates();
              if(manifests.Length > 0)
              {
                 // Prompt user if he wants to apply the updates
                 if( MessageBox.Show(this,
                    "Update for Auto Inproc Application is available,"+
                    " do you want to apply the update?",
                    "Update",MessageBoxButtons.YesNo)== DialogResult.Yes)
                 {
                    foreach(Manifest m in manifests)
                    {
                       m.Apply = true;
                    }
                    // update the application as per manifest details.
                    updater.Download( manifests, TimeSpan.MaxValue );
                    if(manifestDownloaded == manifests.Length)
                    {
                       updater.Activate( manifests );
                       manifestDownloaded = 0;
                    }
                    break;
                 }
                 else
                 {
                    Thread.Sleep(10000);
                 }
              }
              else
              {
                 Thread.Sleep(10000);
              }
           }
        }
        catch(ThreadAbortException)
        {
           // Do nothing if the thread is being aborted, 
           //as we are explicitly doing it
        }
        catch(Exception ex)
        {
           MessageBox.Show(this,ex.Message,"Error",
                MessageBoxButtons.OK,MessageBoxIcon.Error);
        }
     }
 
     private void exitButton_Click(object sender, System.EventArgs e)
     {
        Application.Exit();
     }
 
     private void updater_DownloadStarted(object sender, 
         DownloadStartedEventArgs e)
     {
        UpdateList("DownloadStarted for manifest: " + e.Manifest.ManifestId);
     }
 
     private void updater_DownloadProgress(object sender, 
                                       DownloadProgressEventArgs e)
     {
        UpdateList("DownloadProgress for manifest: "+ e.Manifest.ManifestId +
          "- Files: "+e.FilesTransferred+"/"+e.FilesTotal+
          " - Bytes: "+e.BytesTransferred+"/"+e.BytesTotal);
     }
 
     private void updater_DownloadCompleted(object sender, 
                                   ManifestEventArgs e)
     {
        UpdateList("DownloadCompleted for manifest: " + 
                                 e.Manifest.ManifestId);
        manifestDownloaded++;
     }
 
     private void updater_DownloadError(object sender, 
                           ManifestErrorEventArgs e)
     {
        UpdateList("DownloadError for manifest: " + 
                       e.Manifest.ManifestId +"\n"+e.Exception.Message);
     }
 
     private void updater_ActivationInitializing(object sender, 
                              ManifestEventArgs e)
     {
        UpdateList("ActivationInitializing for manifest: " + 
                                      e.Manifest.ManifestId);
     }
 
     private void updater_ActivationStarted(object sender, ManifestEventArgs e)
     {
        UpdateList("ActivationStarted for manifest: " + e.Manifest.ManifestId);
     }
 
     private void updater_ActivationInitializationAborted(object sender,
                                            ManifestEventArgs e)
     {
        UpdateList("ActivationInitializationAborted for manifest: " + 
                                      e.Manifest.ManifestId);
        MessageBox.Show(this,
           "The Application needs to restart for applying the updates," +
           " please restart the application.",
           "Auto Inproc Updates",MessageBoxButtons.OK,
           MessageBoxIcon.Information);
     }
 
     private void updater_ActivationError(object sender, 
                                       ManifestErrorEventArgs e)
     {
        UpdateList("ActivationError for manifest: " + e.Manifest.ManifestId +
            "\n"+e.Exception.Message);
     }
 
     private void updater_ActivationCompleted(object sender, 
                             ActivationCompleteEventArgs e)
     {
        UpdateList("ActivationCompleted for manifest: " + e.Manifest.ManifestId);
     }
 
     private void UpdateList(string displayString)
     {
        eventList.Items.Add(displayString);
        eventList.Update();
     }
 
    private void InitializeAutoUpdater ()
    {
       // Seperate thread is spun to keep polling for updates
       ThreadStart checkUpdateThreadStart = new ThreadStart(CheckAndUpdate);
       pollThread = new Thread(checkUpdateThreadStart);
       pollThread.Start();     
    }
#endregion

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Praveen Nayak
Web Developer
United Kingdom United Kingdom
No Biography provided

Comments and Discussions

 
QuestionError when runing WaitForApplicationExitProcessor Pin
vijay4you21-Sep-14 2:32
membervijay4you21-Sep-14 2:32 
QuestionType : Microsoft.ApplicationBlocks.Updater.Downloaders.BitsDownloadErrorException, Microsoft.ApplicationBlocks.Updater.Downloaders, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null Message : The server did not return the file size. The URL might Pin
vijay4you10-Sep-14 1:47
membervijay4you10-Sep-14 1:47 
Generalcheck and there are not updates Pin
alhambra-eidos18-Oct-10 9:05
memberalhambra-eidos18-Oct-10 9:05 
QuestionCan I use it n Win2003 and IIS 6.0 ?? Pin
alhambra-eidos15-Oct-10 5:11
memberalhambra-eidos15-Oct-10 5:11 
Generalvalue does not fall in the expected range Pin
conraba30-Apr-10 14:46
memberconraba30-Apr-10 14:46 
GeneralRe: value does not fall in the expected range Pin
conraba3-May-10 7:28
memberconraba3-May-10 7:28 
GeneralMy vote of 1 Pin
Ankur\m/8-Apr-10 21:59
memberAnkur\m/8-Apr-10 21:59 
QuestionDoes UAB works with VB.NET 3.5 and How?? Pin
risingarun30-Nov-09 1:09
memberrisingarun30-Nov-09 1:09 
GeneralThanks Praveen Pin
risingarun27-Nov-09 19:57
memberrisingarun27-Nov-09 19:57 
GeneralThank you Pin
tehtb14-Oct-08 17:42
membertehtb14-Oct-08 17:42 
GeneralExe is not updating with new version. Pin
dheerajv17-Apr-08 5:33
memberdheerajv17-Apr-08 5:33 
GeneralRe: Exe is not updating with new version. Pin
tehtb14-Oct-08 17:41
membertehtb14-Oct-08 17:41 
GeneralVisual Studio 2005 Pin
mania_ar17-Mar-08 0:27
membermania_ar17-Mar-08 0:27 
QuestionActivationInitializationAborted what is causing this? Pin
Guerven29-Jan-08 17:23
memberGuerven29-Jan-08 17:23 
AnswerRe: ActivationInitializationAborted what is causing this? Pin
tehtb14-Oct-08 17:38
membertehtb14-Oct-08 17:38 
QuestionError when runing WaitForApplicationExitProcessor Pin
v_pele9-Jul-07 6:49
memberv_pele9-Jul-07 6:49 
QuestionRollback? Pin
Elric-Wang6-Nov-06 18:41
memberElric-Wang6-Nov-06 18:41 
GeneralAuthentification Pin
Carillonbe17-Oct-06 4:24
memberCarillonbe17-Oct-06 4:24 
GeneralAccording above steps.Occurs some error. Hlep me Pin
lixun29-Jun-06 0:49
memberlixun29-Jun-06 0:49 
Generalneed some more help urgent Pin
gurrumurthi22-May-06 4:55
membergurrumurthi22-May-06 4:55 
Generalwhich is the location in the client where the files Pin
pushpads15-Mar-06 1:32
memberpushpads15-Mar-06 1:32 
GeneralRe: which is the location in the client where the files Pin
cadessi17-Mar-06 10:16
membercadessi17-Mar-06 10:16 
GeneralStrange error message Pin
MarcusNaderus4-Jan-06 4:35
memberMarcusNaderus4-Jan-06 4:35 
GeneralRe: Strange error message Pin
mcreteau15-Mar-06 0:37
membermcreteau15-Mar-06 0:37 
GeneralRe: Strange error message Pin
mmbn23-Apr-06 11:15
membermmbn23-Apr-06 11:15 
GeneralRe: Strange error message (Solution) [modified] Pin
rgonzal18-Nov-06 21:59
memberrgonzal18-Nov-06 21:59 
GeneralRe: Strange error message (Solution) Pin
alhambra-eidos9-Oct-10 0:56
memberalhambra-eidos9-Oct-10 0:56 
GeneralStrange message Pin
MarcusNaderus4-Jan-06 4:34
memberMarcusNaderus4-Jan-06 4:34 
GeneralRe: Strange message Pin
rgonzal18-Nov-06 19:31
memberrgonzal18-Nov-06 19:31 
Generalsample won't update Pin
gerry9930-Nov-05 10:14
membergerry9930-Nov-05 10:14 
QuestionWould the .NET 2.0 ApplicationDeployment Class be a substitute for this architecture? Pin
GRChauvin21-Nov-05 12:26
memberGRChauvin21-Nov-05 12:26 
AnswerRe: Would the .NET 2.0 ApplicationDeployment Class be a substitute for this architecture? Pin
JIANG, Sheng[MVP]19-Nov-06 7:03
memberJIANG, Sheng[MVP]19-Nov-06 7:03 
QuestionHave you tried the "Using File Hash Comparisons for Partial Updates" ? Pin
Kent Liu19-Nov-05 20:13
memberKent Liu19-Nov-05 20:13 
AnswerRe: Have you tried the "Using File Hash Comparisons for Partial Updates" ? Pin
l3chvck25-Jan-06 23:40
memberl3chvck25-Jan-06 23:40 
GeneralApp.config Updater AB Vs App.Config Enterprise Library Pin
davidalcaraz4-Nov-05 7:01
memberdavidalcaraz4-Nov-05 7:01 
QuestionHow can i add a progress bar to my updating process?!? Pin
Beziel24-Oct-05 0:21
memberBeziel24-Oct-05 0:21 
QuestionWhy manifest poit to URL? Pin
davidalcaraz29-Sep-05 10:01
memberdavidalcaraz29-Sep-05 10:01 
GeneralSpecified Cast is not Valid Pin
NitinP224-Sep-05 3:35
memberNitinP224-Sep-05 3:35 
GeneralRe: Specified Cast is not Valid Pin
huahuang27-Nov-05 23:28
memberhuahuang27-Nov-05 23:28 
GeneralRe: Specified Cast is not Valid Pin
CherishTheWind19-Nov-06 21:17
memberCherishTheWind19-Nov-06 21:17 
GeneralRe: Specified Cast is not Valid Pin
vip_navneet22-Apr-08 2:10
membervip_navneet22-Apr-08 2:10 
GeneralError: Specified Cast is not Valid Pin
Azmal19-Sep-05 0:57
memberAzmal19-Sep-05 0:57 
GeneralRequest Registry Access not allowed Pin
Half Past Dead15-Sep-05 14:39
memberHalf Past Dead15-Sep-05 14:39 
GeneralRequested Registry Access Not Allowed Pin
Trying to get this to work15-Sep-05 6:48
sussTrying to get this to work15-Sep-05 6:48 
QuestionWhy it wants to reboot the system? Pin
Ricardo Mendes9-Sep-05 9:34
memberRicardo Mendes9-Sep-05 9:34 
AnswerRe: Why it wants to reboot the system? Pin
davidalcaraz7-Nov-05 10:37
memberdavidalcaraz7-Nov-05 10:37 
Generalcannot add Updater Block in Enterprise Library Configuration Tool Pin
yuewah7-Sep-05 7:01
memberyuewah7-Sep-05 7:01 
GeneralRe: cannot add Updater Block in Enterprise Library Configuration Tool Pin
davidalcaraz7-Nov-05 13:33
memberdavidalcaraz7-Nov-05 13:33 
GeneralProxy server settings Pin
Anonymous3-Sep-05 2:39
sussAnonymous3-Sep-05 2:39 
QuestionCan tell me how to aviod download files via HTTP directly? Pin
Chia-Seng29-Aug-05 0:46
memberChia-Seng29-Aug-05 0:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150520.1 | Last Updated 13 Apr 2005
Article Copyright 2005 by Praveen Nayak
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid