Posts Tagged ‘ Connnectivity ’

Avoid Data Loss – Version Upgrade with ClickOnce

Avoid Data Loss when Upgrading App Version

If you have an occassionally connected smart client, how do you ensure that your clients do not lose any data when they upgrade the application version? This posting has a tip about the UpdateMode property that helps with this question.

We use the Disconnected Service Agent (DSA) to help manage the occassionally connected scenarios. The DSA queues services calls in a local SQL Compact Database, the items in the queue get sent when a network connection is detected.

The Problem Scenario

But there is a problem with the following scenario:

1. User processes data in disco mode. Closes the application.
2. We put a new release on the server.
3. User re-connects to the network then starts the app.
4. ClickOnce advises the user that there is a new releases and asks if they want to upgrade or skip. (at this point there are queued service calls in the local database)

5. User chooses one of the following paths

Skip (good path)

– App starts with the old version
– Data captured in disco mode is sent to the server
– All good! n days later they will be prompted to upgrade their version again (define number of days in your .csproj)

Upgrade (bad path)

– Upgrade is downloaded from the server
– Local version is upgraded, which equates to a new install of the new release (and therefore a new local database)
– App starts with the new version
– Data captured in disco mode is lost, the new version has a new database.

So data loss can be avoided if the user chooses the right path, i.e. skips the upgrade the first time. But the last thing you want to do is rely on the user to make the right choice, if one path leads to data loss it’s really our fault as the developers for making the option available. So how do we take away the option?

ClickOnce UpdateMode

The problem scenario described above will only be an issue if you have set the UpdateMode property to ‘Foreground’.

UpdateMode may either be Foreground or Background. If it is set to Foreground the prompt above will appear when ClickOnce detects a new version and before the application starts. If it is set to Background, if ClickOnce detects a new version it:

  1. Starts the app with the currently installed version
  2. Downloads the new version, ready to go for the next restart.

Importantly what it means is that the first time they reconnect after working in disco mode, the upgrade will automatically be skipped, they’ll login with the old version and the captured data will be sent to the server. Then next time they start the app they will then be prompted to upgrade.

The ‘Background’ mode has the (minor) added advantage of the new version being downloaded in the background. In Foreground mode the user must wait for the download to complete when they click Upgrade.

So unless you have a very strong business case for it using Foreground, I’d recommend always setting the UpdateMode to Background!

Determine if Internet Connection Available

Smart Client

Be careful with your logic when using

NetworkInterface.GetIsNetworkAvailable()

This tells you if any network is available…which doesn’t necessarily mean that you can connect to the internet!

For example if in my app if I unplug my network cable the method returns false…until I plug in my mobile device and suddenly (and accurately) it returns true. Plugging my mobile in does not mean I have internet access BUT IT MIGHT. 

The best way to check for an internet ready connection is to make some call out to the internet and see what happens, there’s a few suggestions for which approach, I like this one:-