Walkthrough: Downloading Assemblies on Demand with the ClickOnce Deployment API
This documentation is archived and is not being maintained.

Walkthrough: Downloading Assemblies on Demand with the ClickOnce Deployment API 

By default, all of the assemblies included in a ClickOnce application are downloaded when the application is first run. You may, 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 using classes in the System.Deployment.Application namespace when the common language runtime (CLR) demands them.

NoteNote

Your application will need to run in full trust to use this procedure.

To create a project using an on-demand assembly

  1. Open the .NET Framework SDK Command Prompt.

  2. Using Notepad or another text editor, define a class named DynamicClass with a single property named Message.

    Public Class DynamicClass
        Sub New()
    
        End Sub
    
        Public ReadOnly Property Message() As String
            Get
                Message = "Hello, world!"
            End Get
        End Property
    End Class
    
    
  3. Save the text as a file named ClickOnceOnLibrary.cs or ClickOnceLibraryvb, depending on the language you use.

  4. Compile the file into an assembly.

    csc /target:library ClickOnceLibrary.cs
    vbc /target:library ClickOnceLibrary.vb
    
  5. Create a new file using your text editor and enter the following code, which creates a Windows Forms application that downloads the ClickOnceLibrary assembly when it is required.

    Imports System
    Imports System.Windows.Forms
    Imports System.Deployment.Application
    Imports System.Drawing
    Imports System.Reflection
    Imports System.Collections.Generic
    Imports Microsoft.Samples.ClickOnceOnDemand
    
    
    Namespace Microsoft.Samples.ClickOnceOnDemand
       <System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, Unrestricted:=true)> _
       Class Form1
          Inherits Form
    
            ' 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 DllMapping as Dictionary(Of String, String) = new Dictionary(of String, String)()
    
          Public Sub New()
             ' Add button to form.
                Dim GetAssemblyButton As New Button()
                GetAssemblyButton.Location = New Point(100, 100)
                GetAssemblyButton.Text = "Get assembly on demand"
                AddHandler GetAssemblyButton.Click, AddressOf GetAssemblyButton_Click
          
                Me.Controls.Add(GetAssemblyButton)
    
                DllMapping("ClickOnceLibrary") = "ClickOnceLibrary"
                AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf CurrentDomain_AssemblyResolve
          End Sub
       
            <STAThread()> _
            Shared Sub Main()
                Application.EnableVisualStyles()
                Application.Run(New Form1())
            End Sub
    
            Private Function CurrentDomain_AssemblyResolve(ByVal sender As Object, ByVal args As ResolveEventArgs) As Assembly
                If ApplicationDeployment.IsNetworkDeployed Then
                    Dim deploy As ApplicationDeployment = ApplicationDeployment.CurrentDeployment
    
                    ' Get the DLL name from the Name argument.
                    Dim nameParts() as String = args.Name.Split(",")
                    Dim dllName as String = nameParts(0)
    		Dim downloadGroupName as String = DllMapping(dllName)
    
                    Try
                        deploy.DownloadFileGroup(downloadGroupName)
                    Catch de As DeploymentException
    
                    End Try
    
                    ' Load the assembly.
                    Dim newAssembly As Assembly = Nothing
    
                    Try
                        newAssembly = Assembly.LoadFile(Application.StartupPath & "\\" & dllName & ".dll," & _  
    			"Version=1.0.0.0, Culture=en, PublicKeyToken=03689116d3a4ae33")
                    Catch ex As Exception
                        MessageBox.Show("Could not download assembly on demand.")
                    End Try
    
                    CurrentDomain_AssemblyResolve = newAssembly
                Else
                    CurrentDomain_AssemblyResolve = Nothing
                End If
            End Function
    
            Private Sub GetAssemblyButton_Click(ByVal sender As Object, ByVal e As EventArgs)
                Dim ourClass As New DynamicClass()
                MessageBox.Show("DynamicClass string is: " + ourClass.Message)
            End Sub
        End Class
    End Namespace
    
    
  6. Save the file as either Form1.cs or Form1.vb and compile it into an executable.

    csc /target:exe /reference:ClickOnceLibrary.dll Form1.cs
    vbc /target:exe /reference:ClickOnceLibrary.dll Form1.vb
    

To mark assemblies as optional in your ClickOnce application using the Manifest Generation and Editing Tool - Graphical Client (MageUI.exe)

  1. Create your ClickOnce manifests as described in Walkthrough: Deploying a ClickOnce Application Manually. Name your application ClickOnceOnDemand.

  2. Before closing MageUI.exe, select the tab containing your deployment's application manifest, and within that tab select the Files tab.

  3. On the Files tab, find OnDemandAssembly.dll in the list of application files and set its File Type column to None. For the Group column, type ClickOnceOnDemand.dll.

To test your on-demand assembly

  1. Upload your ClickOnce deployment to a Web server.

  2. 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
    
  3. When your main form appears, press the Button. You should see a string in a message box window that reads "Hello, World!"

Show:
© 2016 Microsoft