Walkthrough: Downloading Assemblies on Demand with the ClickOnce Deployment API Using the Designer
By default, all of the assemblies included in a ClickOnce application are downloaded when the application is first run. You might, however, 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 System.Deployment.Application namespace when the common language runtime demands them.
Note |
|---|
| Your application will need to run in full trust to use this procedure. |
Note |
|---|
| The dialog boxes and menu commands you see might differ from those described in Help depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. For more information, see Visual Studio Settings. |
To create a project using an on-demand assembly with Visual Studio
-
Create a new Windows Forms project in Visual Studio. On the File menu, point to Add, and then click New Project. Choose a Class Library project in the dialog box and name it ClickOnceLibrary.
Note In Visual Basic, we recommend that you edit the project properties to change the root namespace for this project to Microsoft.Samples.ClickOnceOnDemand or to a namespace of your choosing. For simplicity, the two projects in this walkthrough exist in the same namespace.
-
Define a class named DynamicClass with a single property named Message.
-
Select your Windows Forms project in Solution Explorer. Add a reference to the System.Deployment.Application assembly and a project reference to the ClickOnceLibrary project.
Note In Visual Basic, we recommend that you edit the project properties to change the root namespace for this project to Microsoft.Samples.ClickOnceOnDemand or to a namespace of your choosing. For simplicity, the two projects in this walkthrough exist in the same namespace.
-
Right-click the form, choose View Code from the menu, and add the following references to your form.
-
Add the following code to download this assembly on demand. This code shows how to map a set of assemblies to a group name using a generic Dictionary class. Because we are only downloading a single assembly in this walkthrough, there is only one assembly in our group. In a real application, you would likely want to download all assemblies related to a single feature in your application at the same time. The mapping table allows you to do this easily by associating all of the DLLs belonging to a feature with a download group name.
' Maintain a dictionary mapping DLL names to download file groups. This is trivial for this sample, ' but will be important in real-world applications where a feature is spread across multiple DLLs, ' and you want to download all DLLs for that feature in one shot. Dim DllMappingTable As New Dictionary(Of String, String)() <SecurityPermission(SecurityAction.Demand, ControlAppDomain:=True)> _ Sub New() ' This call is required by the Windows Form Designer. InitializeComponent() ' Add any initialization after the InitializeComponent() call. DllMappingTable("ClickOnceLibrary") = "ClickOnceLibrary" End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf Me.CurrentDomain_AssemblyResolve End Sub Private Function CurrentDomain_AssemblyResolve(ByVal sender As Object, ByVal args As ResolveEventArgs) As System.Reflection.Assembly Dim NewAssembly As Assembly = Nothing If (ApplicationDeployment.IsNetworkDeployed) Then Dim Deploy As ApplicationDeployment = ApplicationDeployment.CurrentDeployment ' Get the DLL name from the argument. Dim NameParts As String() = args.Name.Split(",") Dim DllName As String = NameParts(0) Dim DownloadGroupName As String = DllMappingTable(DllName) Try Deploy.DownloadFileGroup(DownloadGroupName) Catch ex As Exception MessageBox.Show("Could not download file group from Web server. Contact administrator. Group name: " & DownloadGroupName & "; DLL name: " & args.Name) Throw (ex) End Try ' Load the assembly. ' Assembly.Load() doesn't work here, as the previous failure to load the assembly ' is cached by the CLR. LoadFrom() is not recommended. Use LoadFile() instead. Try NewAssembly = Assembly.LoadFile(Application.StartupPath & "\" & DllName & ".dll") Catch ex As Exception Throw (ex) End Try Else ' Major error - not running under ClickOnce, but missing assembly. Don't know how to recover. Throw New Exception("Cannot load assemblies dynamically - application is not deployed using ClickOnce.") End If Return NewAssembly End Function
-
On the View menu, click Toolbox. Drag a Button from the Toolbox onto the form. Double-click the button and add the following code to the Click event handler.
To mark assemblies as optional in your ClickOnce application using Visual Studio
-
Right-click your Windows Forms project in Solution Explorer and select Properties from the shortcut menu. Select the Publish tab.
-
Click the Application Files button.
-
Find the listing for ClickOnceLibrary.dll. Set the Publish Status drop-down box to Include.
-
Expand the Group drop-down box and select New. Enter the name ClickOnceLibrary as the new group name.
-
Continue publishing your application as described in How to: Publish a ClickOnce Application.
To mark assemblies as optional in your ClickOnce application using Manifest Generation and Editing Tool — Graphical Client (MageUI.exe)
-
Create your ClickOnce manifests as described in Walkthrough: Deploying a ClickOnce Application Manually.
-
Before closing MageUI.exe, select the tab containing your deployment's application manifest, and within that tab select the Files tab.
-
Find ClickOnceLibrary.dll in the list of application files and set its File Type column to None. For the Group column, type ClickOnceLibrary.dll.