title | description | ms.date | ms.topic | dev_langs | helpviewer_keywords | author | ms.author | manager | ms.subservice | ||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Download assemblies on demand (ClickOnce API) |
Learn how to mark certain assemblies in your ClickOnce application as optional and download them when the common language runtime needs them. |
11/04/2016 |
conceptual |
|
|
mikejo5000 |
mikejo |
mijacobs |
deployment |
By default, all of the assemblies included in a ClickOnce application are downloaded when the application is first run. However, you may have parts of your application that are used by a small set of your users. In this case, you want to download an assembly only when you create one of its types. The following walkthrough demonstrates how to mark certain assemblies in your application as "optional", and how to download them by using classes in the xref:System.Deployment.Application namespace when the common language runtime (CLR) demands them.
[!INCLUDEndptecclick]
Note
Your application will have to run in full trust to use this procedure.
You will need one of the following components to complete this walkthrough:
-
The Windows SDK. The Windows SDK can be downloaded from the Microsoft Download Center.
-
Visual Studio.
-
Create a directory named ClickOnceOnDemand.
-
Open the Windows SDK Command Prompt or the Visual Studio Command Prompt.
-
Change to the ClickOnceOnDemand directory.
-
Generate a public/private key pair using the following command:
sn -k TestKey.snk
-
Using Notepad or another text editor, define a class named
DynamicClass
with a single property namedMessage
.:::code language="csharp" source="../snippets/csharp/VS_Snippets_Winforms/ClickOnceLibrary/CS/Class1.cs" id="Snippet1":::
-
Save the text as a file named ClickOnceLibrary.cs or ClickOnceLibrary.vb, depending on the language you use, to the ClickOnceOnDemand directory.
-
Compile the file into an assembly.
csc /target:library /keyfile:TestKey.snk ClickOnceLibrary.cs
vbc /target:library /keyfile:TestKey.snk ClickOnceLibrary.vb
-
To get the public key token for the assembly, use the following command:
sn -T ClickOnceLibrary.dll
-
Create a new file using your text editor and enter the following code. This code creates a Windows Forms application that downloads the ClickOnceLibrary assembly when it is required.
:::code language="csharp" source="../snippets/csharp/VS_Snippets_Winforms/ClickOnceOnDemandCmdLine/CS/Form1.cs" id="Snippet1":::
-
In the code, locate the call to xref:System.Reflection.Assembly.LoadFile%2A.
-
Set
PublicKeyToken
to the value that you retrieved earlier. -
Save the file as either Form1.cs or Form1.vb.
-
Compile it into an executable using the following command.
csc /target:exe /reference:ClickOnceLibrary.dll Form1.cs
vbc /target:exe /reference:ClickOnceLibrary.dll Form1.vb
-
Using MageUI.exe, create an application manifest as described in Walkthrough: Manually deploy a ClickOnce application. Use the following settings for the application manifest:
-
Name the application manifest
ClickOnceOnDemand
. -
On the Files page, in the ClickOnceLibrary.dll row, set the File Type column to None.
-
On the Files page, in the ClickOnceLibrary.dll row, type
ClickOnceLibrary.dll
in the Group column.
-
-
Using MageUI.exe, create a deployment manifest as described in Walkthrough: Manually deploy a ClickOnce application. Use the following settings for the deployment manifest:
- Name the deployment manifest
ClickOnceOnDemand
.
- Name the deployment manifest
-
Upload your ClickOnce deployment to a Web server.
-
Start your application deployed with ClickOnce from a Web browser by entering the URL to the deployment manifest. If you call your ClickOnce application
ClickOnceOnDemand
, and you upload it to the root directory of adatum.com, your URL would look like this:http://www.adatum.com/ClickOnceOnDemand/ClickOnceOnDemand.application
-
When your main form appears, press the xref:System.Windows.Forms.Button. You should see a string in a message box window that reads "Hello, World!".
- xref:System.Deployment.Application.ApplicationDeployment