Share via


逐步解說:使用 ClickOnce 部署 API 視需要下載組件

第一次執行 ClickOnce 應用程式時,預設會下載該應用程式中包含的所有組件。 不過,您可能有小部分使用者所使用的應用程式組件。 在此情況下,只有在建立組件的其中一種類型時,才會想要下載組件。 下列逐步解說示範如何將應用程式中的特定組件標示為「選擇性」,以及在 Common Language Runtime 需要時,使用 System.Deployment.Application 命名空間中的類別來下載它們。

注意

.NET Core 和 .NET 5 和更新版本中不支援 System.Deployment.Application 命名空間中的 ApplicationDeployment 類別和 API。 .NET 7 支援存取應用程式部署屬性的新方法。 如需詳細資訊,請參閱在 .NET 中存取 ClickOnce 部署屬性。 .NET 7 不支援 ApplicationDeployment 方法的同等項。

注意

您的應用程式必須以完全信任執行,才能使用此程序。

必要條件

您需要下列其中一個元件才能完成此逐步解說:

  • Windows SDK。 Windows SDK 可從 Microsoft 下載中心下載。

  • Visual Studio。

建立專案

建立使用隨選組件的專案

  1. 建立名為 ClickOnceOnDemand 的目錄。

  2. 開啟 Windows SDK 命令提示字元或 Visual Studio 命令提示字元。

  3. 變更為 ClickOnceOnDemand 目錄。

  4. 使用下列命令產生公開/私密金鑰組:

    sn -k TestKey.snk
    
  5. 使用 [記事本] 或其他文字編輯器,以名為 Message 的單一屬性來定義名為 DynamicClass 的類別。

    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. 視您使用的語言而定,將文字儲存為名為 ClickOnceLibrary.csClickOnceLibrary.vb 的檔案並將它儲存到 ClickOnceOnDemand 目錄。

  7. 將檔案編譯成組件。

    csc /target:library /keyfile:TestKey.snk ClickOnceLibrary.cs
    
  8. 若要取得組件的公開金鑰權杖,請使用下列命令:

    sn -T ClickOnceLibrary.dll
    
  9. 使用文字編輯器建立新的檔案,並輸入下列程式碼。 此程式碼會建立 Windows Forms 應用程式,以在需要時下載 ClickOnceLibrary 組件。

    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);
            }
        }
    }
    
  10. 在此程式碼中,尋找對 LoadFile 的呼叫。

  11. PublicKeyToken 設定為您稍早擷取的值。

  12. 將檔案儲存為 Form1.csForm1.vb

  13. 使用下列命令將其編譯為可執行檔。

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

將組件標示為選擇性

使用 MageUI.exe 將組件標示為 ClickOnce 應用程式中的選用組件

  1. 使用 MageUI.exe 建立應用程式資訊清單,如逐步解說:手動部署 ClickOnce 應用程式所述。 針對應用程式資訊清單使用下列設定:

    • 將應用程式資訊清單命名為 ClickOnceOnDemand

    • 在 [檔案] 頁面的 [ClickOnceLibrary.dll] 資料列中,將 [檔案類型] 資料行設定為 [無]

    • 在 [檔案] 頁面的 [ClickOnceLibrary.dll] 資料列中,於 [群組] 資料行中輸入 ClickOnceLibrary.dll

  2. 使用 MageUI.exe 建立部署資訊清單,如逐步解說:手動部署 ClickOnce 應用程式所述。 針對部署資訊清單使用下列設定:

    • 將部署資訊清單命名為 ClickOnceOnDemand

測試新的組件

測試隨選組件

  1. 將您的 ClickOnce 部署上傳至網頁伺服器。

  2. 輸入部署資訊清單的 URL,從網頁瀏覽器啟動使用 ClickOnce 部署的應用程式。 如果您呼叫 ClickOnce 應用程式 ClickOnceOnDemand,並將其上傳至 adatum.com 的根目錄,您的 URL 會如下所示:

    http://www.adatum.com/ClickOnceOnDemand/ClickOnceOnDemand.application
    
  3. 您的主要表單出現時,請按 Button。 您應該會在訊息方塊視窗中看見內容為 "Hello, World!" 的字串。