Friday, January 5, 2007

Clickonce Deployment Tidbits

Ok... First, I'm going to talk about CD-ROM based initial installations, 'cause that's what I know best. The scenario where this install type works best is as follows:
  • Application prerequisites are quite large (SQL Server, stuff like that...)
  • Application updates should be delivered via the web
  • Application should be available offline (see the "Offline as well" option in Install Mode and Settings under Publish properties of your VS2005 project).
  • Application may have custom prerequisites (another article in itself, coming soon).

We typically configure our applications so that they don't have a deployment web page, but either way is fine (this option is in the Options... dialog). For the purposes of discussion, I'm going to assume the program name is MyApp and that the only files deployed with the application will be the app.config and the app exe.

To set up the application to publish to CD-ROM or DVD-ROM, you should choose a publish location that is a file path on your local machine. We typically use c:\deployments\... or something like that. For the purposes of discussion, I'm using c:\deploy\MyApp\v1.0.0.0 as my first deployment location. You'll want to have a version number in your deployment location so that you don't overwrite older deployments when creating new ones.

So, for version 1.0 of our application, we'll set the deployment location to c:\deploy\MyApp\v1.0.0.0. It's tempting to put something in the "Installation URL" but we won't for building the initial CDs to deploy from. It's also tempting to let visual studio automatically increment the revision with each publish. However, we prefer to have total control over the version number used for publish and for the assembly version (stored in the AssemblyInfo.cs file). There are actually a ton of different version numbers floating around in .NET applications. In our simple case here, there's the Deployment version, the Assembly version, and the Assembly file version. We like to keep all of them in sync. Things can get very confusing otherwise. I'm not going to talk much about the significance of each different version number here, unless requested to do so, so make a request if you want more details about them.

So, on to the publish. We're looking to publish our application to a CD, but also to be able to update it from the web. Let's say you have a web server available (we'll use localhost for these examples). If you create a site (or virtual directory) called MyApp on your IIS install, it will be easier to follow my examples. I'm going to use the path http://localhost/MyApp/updates as my updates deployment path. So, let's click on the Updates... button and see what we see.

In the updates dialog, we obviously want "Application should check for updates" checked, since that's the whole point of this discussion. I would choose "Before the application starts" for the when, since the other alternative is very strange. For now, leave the "minimum required version" blank. For the update location, let's put our http://localhost/MyApp/updates path.

Close the dialog and click publish now. After a few moments, you should see an explorer window pop up with the deployment files in it. Let's look at those files for a moment. There should be a setup.exe - it's the setup bootstrapper that knows how to install your prerequisites and get the ball rolling on the installation. There's also a couple of .application files. The unversioned one and the versioned one are identical (in contents).

These files can be copied to a CD-ROM or DVD-ROM and installed from there, by clicking the Setup.exe. However, we also need to set up the path for updates too. Let's talk about how to deploy the updates to a web server. Whereever you placed your virtual folder (physically) on your web server, you need to copy the .application files and the MyApp_1_0_0_0 folder to that location. In my case, http://localhost/MyApp refers to a "C:\MyApp Deployment" folder on my local hard drive. Since I want the files to be in the "./updates" path relative to this virtual folder, I need a "updates" folder in this folder. I then need to copy those files to the "updates" folder. I should make sure the permissions on those files and folders are appropriate for the users I wish to be able to access this application (normally anonymous access is needed if you want to deploy the application to anyone that wants it).

Once you've copied those files and set up those folders, you should be good to go for your first CD-based ClickOnce deployment. Run the setup.exe from the CD to install the application. If you run Setup.exe the app should install (after warning you about security) and then immediately run itself. You'll see that the app should also appear in your start menu and in the add/remove programs control panel applet.

Now, let's update the application and see a new version deployed. In my case, the original application didn't do anything and was simply a blank form (from the new application wizard). To make version 1.1, I'm going to add a label to the form with some text so I can tell I've got the new version. Once you've done that, deploy again, but to a v1.1.0.0 folder, and change the deployment version (and assembly versions in AssemblyInfo.cs) first. Do this by going to the publish properties (in the project properties) and setting the major and minor publish versions as appropriate. Now publish to the new folder. Once the publish is done, you should see the explorer window pop up. Copy the .application files and the MyApp_1_1_0_0 folder to the updates folder in your web server again. This will replace the MyApp.application that was put there for version 1.0.

Now, let's see the application update itself. Click the application from the start menu and see what happens. The application should ask you if you want to update. DON'T click SKIP! It won't ask you again for a long time if you do (I'm not sure how long). If you don't want your users to be able to skip updates, set a required version equal to your current version each time you deploy (in the Updates... dialog of the Publish settings).

Anyway, you've just done your first ClickOnce deployment. More tidbits to come.

No comments: