Tutorial: Descargar ensamblados a petición con la API de implementación de ClickOnceWalkthrough: Downloading Assemblies on Demand with the ClickOnce Deployment API

De forma predeterminada, todos los ensamblados incluyen en una ClickOnceClickOnce aplicación se descargan cuando se ejecuta la aplicación por primera vez.By default, all of the assemblies included in a ClickOnceClickOnce application are downloaded when the application is first run. Sin embargo, habrá partes de la aplicación que usan un pequeño conjunto de usuarios.However, you may have parts of your application that are used by a small set of your users. En tal caso, es probable que quiera descargar un ensamblado solo cuando cree uno de sus tipos.In this case, you want to download an assembly only when you create one of its types. El tutorial siguiente muestra cómo marcar determinados ensamblados de la aplicación como "opcionales" y cómo descargarlos utilizando clases en el System.Deployment.Application espacio de nombres cuando common language runtime (CLR) los solicite.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 (CLR) demands them.

Nota

La aplicación deberá ejecutarse con plena confianza para poder usar este procedimiento.Your application will have to run in full trust to use this procedure.

Requisitos previosPrerequisites

Necesitará uno de los componentes siguientes para completar este tutorial:You will need one of the following components to complete this walkthrough:

  • El SDK de Windows.The Windows SDK. El SDK de Windows puede descargarse desde Microsoft Download Center.The Windows SDK can be downloaded from the Microsoft Download Center.

  • Visual Studio.Visual Studio.

Crear los proyectosCreating the Projects

Para crear un proyecto que usa un ensamblado a peticiónTo create a project that uses an on-demand assembly

  1. Cree un directorio denominado ClickOnceOnDemand.Create a directory named ClickOnceOnDemand.

  2. Abra el símbolo del sistema de Windows SDK o la Visual StudioVisual Studio símbolo del sistema.Open the Windows SDK Command Prompt or the Visual StudioVisual Studio Command Prompt.

  3. Cambie al directorio ClickOnceOnDemand.Change to the ClickOnceOnDemand directory.

  4. Generar un par de claves pública/privada mediante el siguiente comando:Generate a public/private key pair using the following command:

    sn -k TestKey.snk  
    
  5. Con el Bloc de notas u otro editor de texto, defina una clase llamada DynamicClass con una propiedad única denominada Message.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
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace Microsoft.Samples.ClickOnceOnDemand
    {
        public class DynamicClass
        {
            public DynamicClass() {}
    
            public string Message
            {
                get
                {
                    return ("Hello, world!");
                }
            }
        }
    }
    
  6. Guarde el texto como un archivo denominado ClickOnceLibrary.cs o ClickOnceLibrary.vb, dependiendo del lenguaje que utilice, en el directorio ClickOnceOnDemand.Save the text as a file named ClickOnceLibrary.cs or ClickOnceLibrary.vb, depending on the language you use, to the ClickOnceOnDemand directory.

  7. Compile el archivo en un ensamblado.Compile the file into an assembly.

    csc /target:library /keyfile:TestKey.snk ClickOnceLibrary.cs  
    
    vbc /target:library /keyfile:TestKey.snk ClickOnceLibrary.vb  
    
  8. Para obtener el token la clave pública para el ensamblado, utilice el siguiente comando:To get the public key token for the assembly, use the following command:

    sn -T ClickOnceLibrary.dll  
    
  9. Cree un nuevo archivo con el texto de editor y escriba el código siguiente.Create a new file using your text editor and enter the following code. Este código crea una aplicación de Windows Forms que descarga el ensamblado ClickOnceLibrary cuando sea necesario.This code creates a Windows Forms application that downloads the ClickOnceLibrary assembly when it is required.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;
    using System.Deployment.Application;
    using Microsoft.Samples.ClickOnceOnDemand;
    
    namespace ClickOnceOnDemand
    {
        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand, Unrestricted=true)]
        public class Form1 : 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. 
            Dictionary<String, String> DllMapping = new Dictionary<String, String>();
    
            public static void Main()
            {
                Form1 NewForm = new Form1();
                Application.Run(NewForm);
            }
    
            public Form1()
            {
                // Configure form. 
                this.Size = new Size(500, 200);
                Button getAssemblyButton = new Button();
                getAssemblyButton.Size = new Size(130, getAssemblyButton.Size.Height);
                getAssemblyButton.Text = "Test Assembly";
                getAssemblyButton.Location = new Point(50, 50);
                this.Controls.Add(getAssemblyButton);
                getAssemblyButton.Click += new EventHandler(getAssemblyButton_Click);
    
                DllMapping["ClickOnceLibrary"] = "ClickOnceLibrary";
                AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
            }
    
            /*
             * Use ClickOnce APIs to download the assembly on demand.
             */
            private Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
            {
                Assembly newAssembly = null;
    
                if (ApplicationDeployment.IsNetworkDeployed)
                {
                    ApplicationDeployment deploy = ApplicationDeployment.CurrentDeployment;
    
                    // Get the DLL name from the Name argument.
                    string[] nameParts = args.Name.Split(',');
                    string dllName = nameParts[0];
                    string downloadGroupName = DllMapping[dllName];
    
                    try
                    {
                        deploy.DownloadFileGroup(downloadGroupName);
                    }
                    catch (DeploymentException de)
                    {
                        MessageBox.Show("Downloading file group failed. Group name: " + downloadGroupName + "; DLL name: " + args.Name);
                        throw (de);
                    }
    
                    // 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," +  
    			"Version=1.0.0.0, Culture=en, PublicKeyToken=03689116d3a4ae33");
                    }
                    catch (Exception e)
                    {
                        throw (e);
                    }
                }
                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."));
                }
    
    
                return (newAssembly);
            }
    
            private void getAssemblyButton_Click(object sender, EventArgs e)
            {
                DynamicClass dc = new DynamicClass();
                MessageBox.Show("Message: " + dc.Message);
            }
        }
    }
    
    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
    
  10. En el código, busque la llamada a LoadFile.In the code, locate the call to LoadFile.

  11. EstablecerPublicKeyToken en el valor que recuperó anteriormente.SetPublicKeyToken to the value that you retrieved earlier.

  12. Guarde el archivo como Form1.cs o Form1.vb.Save the file as either Form1.cs or Form1.vb.

  13. Compila en una aplicación ejecutable mediante el siguiente comando.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  
    

Marcar ensamblados como opcionalesMarking Assemblies as Optional

Para marcar los ensamblados como opcionales en la aplicación ClickOnce mediante MageUI.exeTo mark assemblies as optional in your ClickOnce application by using MageUI.exe

  1. Con MageUI.exe, cree un manifiesto de aplicación como se describe en Tutorial: implementar manualmente una aplicación ClickOnce.Using MageUI.exe, create an application manifest as described in Walkthrough: Manually Deploying a ClickOnce Application. Utilice la siguiente configuración para el manifiesto de aplicación:Use the following settings for the application manifest:

    • Nombre del manifiesto de aplicación ClickOnceOnDemand.Name the application manifest ClickOnceOnDemand.

    • En el archivos página, en la fila de ClickOnceLibrary.dll, establezca la tipo de archivo columna ninguno.On the Files page, in the ClickOnceLibrary.dll row, set the File Type column to None.

    • En el archivos página, en el tipo de la fila ClickOnceLibrary.dll ClickOnceLibrary.dll en el grupo columna.On the Files page, in the ClickOnceLibrary.dll row, type ClickOnceLibrary.dll in the Group column.

  2. Con MageUI.exe, cree un manifiesto de implementación tal y como se describe en Tutorial: implementar manualmente una aplicación ClickOnce.Using MageUI.exe, create a deployment manifest as described in Walkthrough: Manually Deploying a ClickOnce Application. Utilice la siguiente configuración para el manifiesto de implementación:Use the following settings for the deployment manifest:

    • Nombre del manifiesto de implementación ClickOnceOnDemand.Name the deployment manifest ClickOnceOnDemand.

Probar el nuevo ensambladoTesting the New Assembly

Para probar el ensamblado a peticiónTo test your on-demand assembly

  1. Cargar su ClickOnceClickOnce implementación en un servidor Web.Upload your ClickOnceClickOnce deployment to a Web server.

  2. Inicie la aplicación implementada con ClickOnceClickOnce desde un explorador Web escribiendo la dirección URL y el manifiesto de implementación.Start your application deployed with ClickOnceClickOnce from a Web browser by entering the URL to the deployment manifest. Si se llama a su ClickOnceClickOnce aplicación ClickOnceOnDemandy lo carga en el directorio raíz de adatum.com, la dirección URL sería similar al siguiente:If you call your ClickOnceClickOnce 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. Cuando aparezca el formulario principal, pulse el Button.When your main form appears, press the Button. Debería ver una cadena en una ventana de cuadro de mensaje que dice "Hello, World!".You should see a string in a message box window that reads "Hello, World!".

Vea tambiénSee Also

ApplicationDeployment