Adding an On-Demand Programmatic Update
Retired Content |
---|
This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. |
This topic describes how to use the ClickOnce programmatic API to control when an update will be applied on a deployed application.
Prerequisites
Before you can complete the steps in this topic, you must complete the following tasks:
- Make a copy of the ReferenceImpl folder. You will be modifying the Bank Branch Workbench reference implementation in this procedure. To avoid having to unzip the Smart Client Software Factory again to restore the original version, we recommend that you make a copy of the reference implementation solution folder now.
- Use the Control Panel Programs option to uninstall all previous versions of the GlobalBank.Shell application that were installed through ClickOnce.
- Remove previous publications from the deployment server. To do this, remove all contents from the Publish folder (C:\Inetpub\wwwroot\GlobalBank).
- Remove previous publications from the local folder used to stage the deployments before they are moved to the Web server (C:\_Publish\SCSF).
Steps
In this procedure, you will do the following:
- Configure ClickOnce update settings by turning off automatic updating and supplying an update location URL.
- Add code to the application to perform on-demand updates.
- Test the update process.
Step 1: Configuring ClickOnce Update Settings
To configure ClickOnce update settings
In the Shell application, double-click Properties to show the Property pages, and then click the Publish tab.
Click Updates.
Clear the The application should check for updates check box.
In the Update Location text box at the bottom of the dialog box, type the update URL (https://localhost/GlobalBank/) as shown in Figure 1.
Figure 1
Application Updates dialog box
Note
If you disable update checking, you must explicitly set the update location; otherwise, the deployment provider URL will not be set in the deployment manifest and on-demand updates will fail. However, you could manually set the deployment provider URL through the Manifest Manager Utility or Mage.
Click OK.
In the Publish Version text boxes, set the version to 1.0.0.0.
Clear the Automatically increment revision with each publish check box.
Your Publish properties should look like Figure 2.
Figure 2
Publish properties
Click Publish Now.
Step 2: Adding Code to Perform On-Demand Updates
To add timed on-demand update checking to the application
Run Visual Studio as an administrator.
In the Designer, open ShellForm.cs.
Move a timer from the Toolbox onto the form.
In the Properties window of the timer, set the Interval property to 60000 (milliseconds) to check for updates every minute.
In the Properties window, go to the Events view.
Add an event handler named OnCheckForUpdates for the Tick event.
In the Code view, add using statements for the System.Deployment.Application and System.ComponentModel namespaces, as shown in the following example.
using System.Deployment.Application; using System.ComponentModel;
Modify the constructor to enable the timer (that is, start it ticking) and hook up event handlers if the application has been launched through ClickOnce, as shown in the following example.
public ShellForm() { InitializeComponent(); _layoutWorkspace.Name = WorkspaceNames.LayoutWorkspace; // Check to see whether you are running through a ClickOnce launch. if (ApplicationDeployment.IsNetworkDeployed) { timer1.Enabled = true; // Hook up an event handler for update check completed events. ApplicationDeployment.CurrentDeployment.CheckForUpdateCompleted += OnCheckForUpdatesCompleted; ApplicationDeployment.CurrentDeployment.UpdateCompleted += new AsyncCompletedEventHandler(OnUpdateCompleted); } }
Add the following code to the event handler for the Tick event.
private void OnCheckForUpdates(object sender, System.EventArgs e) { // Check to ensure the application is running through ClickOnce. if (ApplicationDeployment.IsNetworkDeployed) { // Check for updates asynchronization. ApplicationDeployment.CurrentDeployment.CheckForUpdateAsync(); } }
Add the following Boolean flag member to the form.
bool m_RequiredUpdateDetected = false;
Add the following event handler for the CheckForUpdateCompleted event.
void OnCheckForUpdatesCompleted(object sender, CheckForUpdateCompletedEventArgs e) { if (e.UpdateAvailable) { if (e.IsUpdateRequired) { m_RequiredUpdateDetected = true; } ApplicationDeployment.CurrentDeployment.UpdateAsync(); } }
Add the following event handler for the UpdateCompleted event.
void OnUpdateCompleted(object sender, AsyncCompletedEventArgs e) { if (m_RequiredUpdateDetected) { MessageBox.Show("Required update downloaded, application will restart"); Application.Restart(); } else { DialogResult result = MessageBox.Show( "Application update downloaded. Would you like to restart?", "Global Bank", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) Application.Restart(); } }
Step 3: Testing the Update Process
To test the update process
- Publish an initial version of the Bank Branch client application with the on-demand update code in it. Use the procedure in How to: Publish an Initial Version of an Application.
- Launch the initial version of the Bank Branch client application. Use the procedure in How to: Deploy an Initial Version of an Application.
- Close the application so that it is not checking for updates while you are publishing the new version.
- Make a simple but noticeable update to the Shell application. An easy example is to modify the title bar text (Text property) for the ShellForm form.
- In the Shell application, double-click Properties to display the Property pages, and then click the Publish tab.
- In the Publish Version text boxes, set the version to 1.1.0.0.
- Publish the updated version. Use the procedure in How to: Publish an Updated Version of an Application.
- Use the Start menu to launch the client application. After about a minute, it should detect and download the update, and then prompt you to restart.
Outcome
Your application checks for updates in the background based on a timer interval that you control. When updates are detected, they are immediately downloaded and the user is prompted to restart the application.