On-Demand Updates with ClickOnce

On-Demand Updates with ClickOnce

ClickOnce supports both automatic updating and on-demand updates. The default model checks for updates automatically at application launch (when connected), and applies those updates immediately. There are a number of options available for installed applications (available online and offline), including whether to check automatically at all, whether to do it on a background thread, whether to do it on a timed interval, and other options.

However, sometimes you may want to do updates on demand, either as the only update model, or in combination with automatic updating in the background on a periodic interval. To do that, you will need to use the ClickOnce API defined in the System.Deployment framework assembly.

ClickOnce supports both automatic updating and on-demand updates

The main class you will use to support on-demand updates is the ApplicationDeployment-class defined in System.Deployment.Application. You will typically add code in response to a user action (such as selecting a Check For Updates menu-item) that goes out and checks to see if updates are available, and if so retrieves them. You will then need to restart the application to have those changes applied.

A simple but common pattern to accomplish this is something like the following:

' First check to see if we are running in a ClickOnce
' context
If ApplicationDeployment.IsNetworkDeployed Then
  ' Get an instance of the deployment
  Dim deployment As ApplicationDeployment = _
   ApplicationDeployment.CurrentDeployment

  ' Check to see if updates are available
  If deployment.CheckForUpdate() Then
    Dim res As DialogResult = MessageBox.Show(_
      "A new version of the application is available,_
       do you want to update?", _
      "Application Updater", MessageBoxButtons.YesNo)
    If res = DialogResult.Yes Then
      ' Do the update
      deployment.Update()
      Dim res2 As DialogResult = MessageBox.Show(_
       "Update complete, do you want to restart the_
       application to apply the update?",_
       "Application Updater", MessageBoxButtons.YesNo)
      If res2 = DialogResult.Yes Then
        ' Restart the application to apply the update
        Application.Restart()
      End If
    End If
  Else
    MessageBox.Show("No updates available.",_
      "Application Updater")
  End If
Else
  MessageBox.Show("Updates not allowed unless you are_
    launched through ClickOnce.")
End If

There are of course asynchronous versions of the CheckForUpdate- and Update-methods if you want to avoid blocking your UI while this happens.

The project settings you will need may not be completely apparent

The project settings you will need may not be completely apparent. The first important setting is that you need to change the update-behavior of the ClickOnce-deployment to stop automatically checking for updates if you are doing only on-demand updates. You do this through the Publish-tab of the project properties settings, Updates button, shown in figure 1.

Fig. 1: ClickOnce Update Behavior Settings access

When you click that button, the dialog in figure 2 will show with default settings selected as shown.

Fig. 2: ClickOnce Update Settings Dialog

What you will want to do for a pure on-demand updates application is to uncheck the box that says the application should check for updates at the top. The other trick that is not apparent but required is that you have to specify an Update-location at the bottom or you will get an obscure error message when you try to launch the application on the client. So you should set up the update settings like shown in figure 3.

Fig. 3: ClickOnce On-Demand Update Settings

With those settings in place, when you publish your application out and the client launches it, they can invoke the code shown earlier to check for and apply updates on-demand.

Editorial note: For those who are currently using VS 2005 Beta 2 (or earlier), there is a bug in the ClickOnce on-demand update system. The bug has to do with an IllegalOperationException you get when calling CheckForUpdate(). This bug is fixed in the RC1 version of Visual Studio 2005, but there is a work-around. The workaround in Beta 2 is to tell the app to check for updates, set it to do so after the application starts, and set the subscription period (how often to check for updates) to a long value, such as 50 weeks. Be careful, because another bug is that VS will let you set an illegally large value for this subsciption, such as 512 weeks. If you do so though, you will get a deployment error because ClickOnce will see a schema violation, and if you try to edit the manifest with MaGe, it will crash with an unhandled exception. You can read more about this bug on the blog of Brian Noyes, go to http://www.softinsight.com/bnoyes/PermaLink.aspx?guid=896b807d-37a9-4ac1-a6d2-5bacc28d9963 for the article about this.

If you wanted to combine on-demand updates with periodic background checking for updates, you can do that by leaving updates enabled, but you will need to select the option to check for updates after the application starts. You will then want to configure the frequency of checking using the options in the middle of the dialog.

De sources die bij dit artikel horen kunt u downloaden via Noyes_OnDemandUpdatesWithClickOnce_SRC.zip.

Commentaar van anderen:
ChristianLouboutin op 17-8-2010 om 3:38
Christian Louboutin Shoes, Christian Louboutin, Christian Louboutin Shoes, Wedding Shoes, Wedding Shoes, Louboutin Shoes, Christian Louboutin Discount copies of the mirror, so the angel of the practical activities of customers who activities. Christian Louboutin Evening, Manolo Blahnik Shoes, Christian, Louboutin, Christian Louboutin Sale, Louboutin Sale, Cheap Christian Louboutin Can you from your shoes, you give yourself into chargeless travel and bear your lifetime. Christian Louboutin Boots, Christian Louboutin Pumps, Christian Louboutin Sandals, Christian Louboutin Flats, Christian Louboutin Wedges, Christian Louboutin Sandals Do you agree to accept and hear the cases angled heel shoes forward or abstract, tear, buttons, and destruction, and the ability to reduce greenhouse gas emissions, it is exactly like daydreaming and shoes. Yves Saint Laurent Shoes, Christian Louboutin Boots, Manolo Blahnik Shoes, Yves Saint Laurent Boots, Miu Miu Shoes, Christian Dior Shoes this is an extra brand and shoes. However, you will never accept the amorous Louboutin christians through brand. Christian Louboutin Flats, Christian, Herve Leger V Neck Dress, Herve Leger Bandage Dress, Herve Leger Dress, Herve Leger V Neck Dress You can visit in your configuration of the life of their shoes were forgiven a brace, air brief you accept defeat them later anniversary, they are careful bag dust, shoe box that you have to go.
Geef feedback:

CAPTCHA image
Vul de bovenstaande code hieronder in
Verzend Commentaar