Deployment has been a thorn in most developers’ sides for a long time. Developing complex distributed applications is challenging enough just from a design and implementation perspective. Failing to plan for deployment or having the deployed environment change on you can kill an application quickly, no matter how well you plan for it. ClickOnce does not solve this problem, but it definitely takes a big step in the right direction for streamlining deployment of smart client applications.
I was first exposed to ClickOnce more than three years ago at an early adopter lab on the Microsoft campus in Redmond. At the time, I was just starting to get immersed in smart client technology and beginning to think about how to address all aspects of the application lifecycle as an architect. Having experienced a fair amount of deployment pain myself in the past, I instantly fell in love with ClickOnce as a technology. I quickly saw the potential for ClickOnce to be a key enabler for the broad adoption of smart client architectures because without a way to get those smart client applications in your users’ hands, you might as well not build them.
The most common question that I got as I was working on this book was, “How can you write a whole book on ClickOnce?” This usually came from someone who had seen demos of ClickOnce but had not yet tried to use it for something real. ClickOnce is incredibly powerful, yet it seems simple on the surface. It takes only five to ten minutes to run an end-to-end demo of what ClickOnce can do for you. When people have seen this kind of presentation of ClickOnce, they do not realize that ClickOnce addresses a lot more than a single common deployment scenario. However, whenever you try to provide flexibility and power, a fair amount of complexity also comes along with it.
I think the ClickOnce team did a really good job of making the simple, straightforward use of ClickOnce as easy as possible. If you are building a new smart client application from scratch with ClickOnce in mind, using ClickOnce to deploy it requires minimal effort for both your administrators and your users. However, real applications are rarely simple and straightforward (even though, because of poor architecture, the complexity is often unnecessary or disproportionate to what the applications are designed to do). ClickOnce has many variations and options that let you address a broad range of scenarios to deal with those complexities. And once you start getting into those capabilities, you dive into the deep end of the ClickOnce pool and really need to understand a lot more about what is going on under the covers; what the effects are of setting publishing, update, and security options different from the defaults; how your application is going to behave in the ClickOnce runtime environment; and so on.
Who Should Read This Book?
This book is written for those developers and architects who need to understand the full range of capabilities of ClickOnce so that they can make educated decisions early in the development lifecycle and put those decisions into practice when the product is getting close to complete. You will need this understanding to make sure you can get your smart client applications deployed through ClickOnce and to address the complexities of real-world applications in that environment. It is primarily written for intermediate to advanced developers or architects, but IT professionals who are responsible for deploying and maintaining ClickOnce applications can also get a lot out of most of the chapters as well.
This is not a book about programming, although there are aspects of ClickOnce covered in several of the chapters that require coding to use. ClickOnce is mainly driven through Visual Studio project configuration, tools, and processes, not through code. So a fair percentage of the book will describe these aspects, and only a small portion will discuss code directly.
There is a programmatic API that is discussed in several chapters, and there are other coding practices that are important from within your application that can affect the deployment and execution of a ClickOnce application. For the sections that cover coding, I expect that readers already know how to code .NET applications. I will point out appropriate references when necessary for complex topics, but to understand the code samples, you need to have some experience developing Windows Forms applications in .NET. 1
Deploying ClickOnce applications is mostly about tools and less about code. However, there are a number of code samples in this book, and to help make things easier, I have adopted some common conventions.
First, any time I refer to classes, variables, namespaces, and other artifacts that manifest themselves in code, I will use a monospace font to make it clear if I am talking about an instance of the ApplicationDeployment class as opposed to talking about a coding construct in a conceptual way. Short code listings will be presented inline within the text using a monospaced font as well.
Longer listings will use a similar font, but will be broken out into labeled listings that can be referred to throughout the text (based on listing numbers). Within code listings, I will sometimes set in bold particularly relevant portions of the code, especially to highlight “evolving code.” I may remove details that are not relevant to a discussion, and if so will insert a comment that indicates that there are more details, identifiable by a comment with an ellipses (//...). What this means is that more code is needed to complete the example or that there exists more code generated by the designer, but you don’t need it to understand the concept. On occasion, I will add explanatory comments to code in order to show context.
This book was written using the released version of Visual Studio 2005 and the .NET Framework 2.0. ClickOnce capabilities are available in all versions of Visual Studio, including Visual C# 2005 Express and Visual Basic 2005 Express. You do not need Visual Studio to use ClickOnce because it is a core capability of the .NET Framework, but to use ClickOnce for any serious project, you will need Visual Studio to do the initial publishing. Throughout the book, I will refer to Visual Studio instead of Visual Studio 2005 for brevity, but you can always infer that I mean Visual Studio 2005 or one of the Express versions (even though they do not have “Studio” in their official names).
If you plan to run the samples available from the download site or the walkthroughs and code listings in this book, you will need a version of Visual Studio 2005, Visual C# 2005 Express, or Visual Basic 2005 Express installed on your machine. One of the samples uses a SQL Server 2005 Compact Edition database to provide sample data to the application, but it includes the libraries needed to run that. One sample uses the Northwind database to provide sample data. Because the particular functionality of the application is not important to the topic being covered (how to deploy and update these applications with ClickOnce), don’t get wrapped up in trying to get these samples running if you don’t already have the databases available; just create an empty Windows Forms application and use it.
Choice of Language
I chose to present the code samples in this book in C#. The downloadable code is available in both C# and Visual Basic. It is a fact of life that there will continue to be a mix of C# and Visual Basic available in articles, books, and code samples for a long time to come. Even though I prefer C# myself, that is just a preference, and I feel that Visual Basic is a solid choice for developers who have a strong background in earlier versions of Visual Basic.
I firmly believe that to be an effective .NET developer, you need to be able to read code from either language, even if you spend most of your time with one. If you are not already comfortable reading C# code, I encourage you to use this opportunity to get comfortable with reading C#. It will expand your horizons in terms of the amount of reference material that is available to you, it may help you in your job, and it will give you bragging rights over the many silly and close-minded C# developers who can’t or won’t read Visual Basic.
This book steps you progressively through all of the concepts you will need to master to use ClickOnce to deploy real-world smart client applications. It starts with the basics in the first chapter, giving you a high-level view of ClickOnce and the surrounding context of smart client applications. Then it steps through deploying and updating applications, with all the associated options. It then focuses on more of the infrastructure for ClickOnce deployment, including how to manage application files, security, prerequisites, and advanced capabilities. It ends with an appendix that tells you what is different with respect to Windows Presentation Foundation application deployment (not much).
Here is a quick breakdown of the contents of each chapter.
Chapter 1: Introduction to ClickOnce. This chapter sets the context for the rest of the book. It starts by describing what a smart client application is and what deployment challenges ClickOnce was designed to address. It describes the high-level features of ClickOnce, and then walks you through a sample deployment and update of a client application using ClickOnce. This chapter wraps up describing the system requirements for ClickOnce, how it relates to other deployment technologies, when to use it and when not to, and a quick discussion of smart client architecture.
Chapter 2: Initial Deployment with ClickOnce. This chapter covers the process of publishing an initial version of an application and deploying it to clients. It discusses all of the publishing options that you have available for getting your applications placed on the deployment server and what that process involves using Visual Studio. It describes what happens when the user deploys the application to the client machine using ClickOnce from an end user’s perspective, and what is going on under the covers. It finishes by describing the process of manually publishing your application using the SDK Mage tools for an environment where Visual Studio cannot access the production servers.
Chapter 3: Automatic Application Updates. This chapter covers the capabilities of ClickOnce for automatically deploying application updates to users. The publishing for updates is discussed briefly because it is effectively the same as for initial deployment. Then the deployment process is detailed, showing what the experience is for users and what the options are. The chapter describes all of the options you have for configuring when and how updates occur. It discusses what happens behind the scenes on the deployment server as well as on the client machine when an update is detected and deployed. It also covers how to manage rolling back an update either from the client or the server side.
Chapter 4: On-Demand Updates. This chapter builds on Chapter 3, showing how to use the programmatic API for ClickOnce to trigger updates from your application code. It discusses performing synchronous and asynchronous updates, detecting updates, and pulling them down when desired using the API. It covers several strategies for doing the updates asynchronously to avoid blocking the client application. It also describes other functionality exposed through the API, including the ability to check detailed update information and find out about the version of the application that is currently deployed on the client machine.
Chapter 5: Application and Data File Management. This chapter discusses in detail managing the files that your application is composed of. It describes how ClickOnce treats those files, how to select and control what files get deployed with your application, and where they will end up on the client machine. It discusses download groups, which provide a way to download portions of your application files on demand. It discusses managing application data files, and how those files are migrated by ClickOnce when an application update occurs. It covers using plug-in DLLs in a ClickOnce application to enhance your application’s functionality with individual optional modules. The chapter closes by covering how to manage the application files that get published using the SDK Mage tools.
Chapter 6: ClickOnce Security. This chapter discusses the security features and protections of ClickOnce in detail. It covers the deployment time and runtime protections for the client machine, and discusses the tamper protections for the application files both on the server and client sides. The chapter describes how to configure and control what permissions the application requires, and how those permissions are evaluated and granted on the client machine. It discusses how user prompting can be used for permission elevation, and how to use the Trusted Publisher mechanism to automatically elevate permissions based on an administrator’s predeployed publisher certificate. It covers managing the ClickOnce manifest digital signature with publisher certificates. Finally, the chapter covers how to protect sections of your application from malicious code and includes a quick introduction to using role-based security in .NET to restrict permissions in the client application based on user roles.
Chapter 7: Prerequisite Deployment with the Bootstrapper. This chapter covers how to get things preinstalled on the client machine that cannot be deployed through ClickOnce. It discusses the Bootstrapper’s features for wrapping multiple installer packages and running them through a single launch experience on the client machine. It describes creating and configuring Bootstrapper manifest files, and what the runtime experience is when deploying prerequisites. The chapter steps through several scenarios of types of files you might want to deploy with the Bootstrapper.
Chapter 8: Advanced ClickOnce Topics. This chapter covers a variety of additional questions that often come up for ClickOnce applications to address specialized scenarios. The chapter discusses using command line parameters, debugging ClickOnce applications, deploying non-.NET applications with ClickOnce, working with the Firefox Web browser, and deploying COM components with your application, to name a few.
Appendix A: ClickOnce Deployment of WPF Applications. The appendix covers the differences in using ClickOnce with Windows Presentation Foundation (WPF) applications. For the most part ClickOnce works the same with WPF, but there are some subtle differences. The appendix covers the Web Browser Application model, what you need to do differently when creating and publishing the project, and how the application deploys and runs on the client machine.
Book’s Web Site and Sample Download Code
The Web site for this book is at www.softinsight.com/clickoncebook. All of the sample code in this book is available for download on that site. Links are available on the site to other ClickOnce-related resources, including the author’s blog (www.softinsight.com/bnoyes/).
1. Windows Forms 2.0 Programming by Chris Sells and Michael Weinhardt (Addison-Wesley, 2006) is the best overall reference and tutorial for learning all aspects of Windows Forms programming. My other book in this series, Data Binding with Windows Forms 2.0 (Addison-Wesley, 2006), will give you a deep dive on how to present data in your applications. Programming .NET Components, Second Edition, by Juval Löwy (O’Reilly & Associates, 2005) is an excellent deep dive on overall .NET programming, and many of the topics covered in that book are prerequisites for the more advanced code samples in this book.