Friday, July 27, 2007

ClickOnce Deployment in .NET Framework 2.0

ClickOnce Deployment in .NET Framework 2.0
By Thiru Thangarathinam
Rating: 4.2 out of 5
Rate this article

It is very common among the developers of previous generations to choose web applications over rich Windows UIs because of the deployment challenges associated with deploying a Smart Client Windows Forms application. However with the release of Visual Studio 2005, Microsoft has released a new technology named ClickOnce that is designed to solve the deployment issues for a windows forms application. This new technology not only provides an easy application installation mechanism but also enables easy deployment of upgrades to existing applications. This article will demonstrate how to take advantage of the excellent features of the ClickOnce deployment technology by discussing examples on how to utilize this new feature.

Introduction

Since the introduction of the powerful server side web technologies such as ASP, JSP, ASP.NET, developers have shown more interest in building web applications rather than in windows applications. The factors that attracted the developers toward web applications can be summarized as follows:

  • A web application is ubiquitous, making it accessible in all the places where an internet connection is available.
  • The second and most important factor is the deployment. With web applications, there is no need to deploy any software on the client side. All the client application needs is just the browser. This makes it possible for the developers to easily deploy updates to the existing web application without impacting the client machines.

If you talk to the developers, you will find that the main reason for preference for web applications over windows applications is the second point in the above list. Even though this is true with traditional applications, Microsoft is making every attempt to ensuring that windows applications can be deployed and updated with the same ease as the web applications. You can see proofs of this in the initial release of .NET Framework when Microsoft introduced the deployment of windows forms application through HTTP. Using this approach, you could simply use HREF HTML element to point to a managed executable (.exe). Then when you click on the HREF link, Internet Explorer can automatically download and install the executable on the client machine. Even though this approach sounds very promising, it also presents some interesting challenges. One of the most important challenges is the downloading of the updated code through the HTTP. Since this process was not transacted, it was possible for the application to be left in an inconsistent state. Moreover there was no way for you to specify if the application could work in offline mode apart from the traditional online mode. Combined with the operational mode issue, this approach also did not provide the ability to create shortcuts that can be used to launch the application. Even though this approach presented itself with a lot of issues, it could still be used in controlled environments. However for complex multi-assembly dependant windows forms applications, you needed a transacted and easily updateable way of deployment. This is exactly what the ClickOnce technology introduced with .NET Framework 2.0 provides. In this article, we will see how to effectively use this technology to deploy a windows form application.

How does ClickOnce Deployment Technology Work?

Before we look at an example, let us understand how this technology works.

  • You create a Windows forms application and use the Publish option to deploy the application onto any of the following locations: File System, Local Web Server, FTP Site, or a Remote Web Site.
  • Once the application is deployed onto the target location, the users of the application can browse to the publish.htm file and install the application onto their machine. Note that publish.htm file is the entry point for installing the application and this will be discussed in the later part of this article.
  • Once the user has installed the application, a shortcut icon will be added to the desktop and the application will also be listed in the Control Panel/Add Remove Programs.
  • When the user launches the application again, the manifest will contain all the information to decide if the application should go to the source location and check for updates to the original application. Let us say, for instance, a newer version of the application is available, it will be automatically downloaded and made available to the user. Note that when the new version is downloaded, it is performed in a transacted manner meaning that either the entire update is downloaded or nothing is downloaded. This will ensure that the application integrity is preserved.

Let us look at an example to understand this.

A Simple ClickOnce Deployment Example

Let us start by creating a simple Visual C# Windows forms application named ClickOnceDemo using Visual Studio 2005. Once the project is created, place a command button on the form and double click on the command button and modify its code to look like the following.

private void btnClick_Click(object sender, EventArgs e)
{
MessageBox.Show(System.Reflection.Assembly.
GetExecutingAssembly().Location);
}

If you run the ClickOnceDemo application and click on the button, you will see an output that is somewhat similar to the following depending on the location of your project folder.

Now that we have created the application, let us publish it. But before we do that, let us look at some of the properties associated with publishing of the forms application. Right click on the ClickOnceDemo project from the solution explorer and select Properties from the context menu. You should see the following dialog box being displayed.

The Publish tab in the Project Properties dialog box shown in the above screenshot provides a number of options for configuring publish related settings. Let us look at each one of these options in detail.

At the top of the properties page is the Publishing Location dropdown box that allows you to specify the location in which the application will be published. When you click on the ... button right next to the dropdown, it brings up the following window wherein you can specify the target virtual directory for publishing.

Using the Publish properties dialog box, you can also specify if the application is available only in online mode or in both online and offline modes. This option is available in the Install Mode and Settings panel. This panel also contains options that let you specify the prerequisites for your windows forms application. To specify this, click on the Prerequisites... command button and you will see the following dialog box.

By default, the .NET Framework 2.0 option will be checked and you can choose any of the remaining options depending on your application requirements.

Right below the Prerequisities option, there is a command button named Updates... that lets you configure the manner and timing in which the updates to the application will be delivered to the client machine. Clicking on the Updates... button will result in the following dialog box.

As you can see from the above dialog box, you have a number of options that allow you to exercise finer level of control over the application updates.

Right below the Updates button is the Options... button that brings up the following dialog box when clicked.

Using the Publish options, you can not only indicate the language in which the application will be published but also specify the Start menu shortcut name. Once you publish this application to the target location, the root directory of the application install will contain a file called publish.htm, which you can use to launch the application on the client machine. By default, the name of the file is set to publish.htm and can be modified using the above dialog box.

Now that we have a understanding of the properties in a general sense, let us see how to publish the application.

Publishing the Application

To publish the application, start by selecting Publish ClickOnceDemo from the Publish menu. This will bring up the first step in the publish wizard, which is shown below.

In the above dialog box, you can specify the location where you want to publish the application. The locations to which you can deploy the application are: File System, Local IIS, FTP Sites, and Remote Sites. For the purposes of this example, let us leave the default value of http://localhost/ClickOnceDemo. Clicking Next in the above dialog box will result in the following dialog box, which allows you to specify the modes in which the application will be available.

The default value in the above screen is dependant on the value set in the Install Mode Settings panel of the Publish properties dialog box. Then click Next and you will be asked to specify the key file used for strongly signing the application. Since we haven't created a key file yet, select the option that says "" as shown in the following screenshot.

Clicking Next in the above dialog box will bring up the Confirm dialog box, wherein you can hit Finish to complete the publishing.

If you hit Finish in the above dialog box, Visual Studio will build the project, publish the application to the ClickOnceDemo folder under the default web site, and then automatically bring up the publish.htm file in the browser. Before we look at the publish.htm file, let us examine the files generated by the wizard. Navigate to the ClickOnceDemo folder in the default web site through the Windows explorer. You will see the following screenshot.

The ClickOnceDemo_1.0.0.0 folder is the one that contains the .exe file and the corresponding manifest files. Since we mentioned that the prerequisites (specified through the Publish properties dialog box in the earlier section) for this application is .NET Framework 2.0, Visual Studio automatically created a folder named dotnetfx that contained the executable for installing the .NET Framework 2.0. Now that we have had a look at the files created by the wizard, let us turn our focus to the publish.htm that provides a means for launching the application.

As you can see from the above screenshot, this html page enables you to install the ClickOnceDemo application through a hyperlink. Click the Install ClickOnceDemo hyperlink to install the application. You will be presented with the following dialog box, wherein you can just hit Install to continue the installation.

Once the application is installed, it will automatically execute the application and bring up the form. Click on the command and you will see an output that is somewhat similar to the following.

As you can see, now the path displayed the message box is different than the previously generated message box. This is because of the fact that the application is now downloaded and locally cached in the folder shown in the above message box. If you navigate to that folder from windows explorer, you will see folders as listed in the following screenshot. These folders contain files such as manifests, and the actual executable itself and so on.

You will also notice the following changes as a result of the installation.

  • An icon is added to the Start menu
  • An entry is added in Control Panel -> Add/Remove Programs.

From this point onwards, you can launch the application through the shortcut in the Start -> Programs menu. From Add/Remove Programs, you can either completely uninstall the application or revert to the previous version, if there was one.

Conclusion

The features of ClickOnce we have seen in this article are only the tip of the iceberg and it offers many more benefits. However, the key motivation for using ClickOnce is the ease of deployment, installation, and versioning for your Windows Forms applications.

No comments: