使用 Visual Studio 部署 .NET Core 應用程式Deploy .NET Core apps with Visual Studio

您可以將 .NET Core 應用程式部署為「與 Framework 相依的部署」,其中包含您的應用程式二進位檔,但取決於目標系統上是否有 .NET Core 存在,也可以部署為「自封式部署」,其中包含您的應用程式和 .NET Core 二進位檔。You can deploy a .NET Core application either as a framework-dependent deployment, which includes your application binaries but depends on the presence of .NET Core on the target system, or as a self-contained deployment, which includes both your application and .NET Core binaries. 如需 .NET Core 應用程式部署的概觀,請參閱 .NET Core 應用程式部署For an overview of .NET Core application deployment, see .NET Core Application Deployment.

下列各節顯示如何使用 Microsoft Visual Studio 來建立下列類型的部署:The following sections show how to use Microsoft Visual Studio to create the following kinds of deployments:

  • 與 Framework 相依的部署Framework-dependent deployment
  • 有協力廠商相依性的 Framework 相依部署Framework-dependent deployment with third-party dependencies
  • 自封式部署Self-contained deployment
  • 有協力廠商相依性的自封式部署Self-contained deployment with third-party dependencies

如需使用 Visual Studio 來開發 .NET Core 應用程式的資訊,請參閱 Windows 上 .NET Core 的必要條件For information on using Visual Studio to develop .NET Core applications, see Prerequisites for .NET Core on Windows.

與 Framework 相依的部署Framework-dependent deployment

部署無任何協力廠商相依性的 Framework 相依部署,只涉及建置、測試和發行應用程式。Deploying a framework-dependent deployment with no third-party dependencies simply involves building, testing, and publishing the app. 以 C# 撰寫的簡單範例會說明此程序。A simple example written in C# illustrates the process.

  1. 建立專案。Create the project.

    選取 [檔案] > [新增] > [專案]。Select File > New > Project. 在 [新增專案] 對話方塊中,展開 [已安裝] 專案類型窗格中的語言 (C# 或 Visual Basic) 專案類別,選擇 [.NET Core],然後選擇中央窗格中的 [主控台應用程式 (.NET Core)] 範本。In the New Project dialog, expand your language's (C# or Visual Basic) project categories in the Installed project types pane, choose .NET Core, and then select the Console App (.NET Core) template in the center pane. 在 [名稱] 文字方塊中輸入專案名稱,例如 "FDD"。Enter a project name, such as "FDD", in the Name text box. 選取 [確定] 按鈕。Select the OK button.

  2. 新增應用程式的原始程式碼。Add the application's source code.

    在編輯器中開啟 Program.csProgram.vb 檔案,並用下列程式碼取代自動產生的程式碼。Open the Program.cs or Program.vb file in the editor and replace the auto-generated code with the following code. 它會提示使用者輸入文字,並顯示使用者輸入的個別文字。It prompts the user to enter text and displays the individual words entered by the user. 它會使用規則運算式 \w+ 分隔輸入文字中的字詞。It uses the regular expression \w+ to separate the words in the input text.

    using System;
    using System.Text.RegularExpressions;
    
    namespace Applications.ConsoleApps
    {
        public class ConsoleParser
        {
            public static void Main()
            {
                Console.WriteLine("Enter any text, followed by <Enter>:\n");
                String s = Console.ReadLine();
                ShowWords(s);
                Console.Write("\nPress any key to continue... ");
                Console.ReadKey();
            }
    
            private static void ShowWords(String s)
            {
                String pattern = @"\w+";
                var matches = Regex.Matches(s, pattern);
                if (matches.Count == 0)
                {
                    Console.WriteLine("\nNo words were identified in your input.");
                }
                else
                {
                    Console.WriteLine($"\nThere are {matches.Count} words in your string:");
                    for (int ctr = 0; ctr < matches.Count; ctr++)
                    {
                        Console.WriteLine($"   #{ctr,2}: '{matches[ctr].Value}' at position {matches[ctr].Index}");
                    }
                }
            }
        }
    }
    
    
    Imports System.Text.RegularExpressions
    
    Namespace Applications.ConsoleApps
        Public Module ConsoleParser
            Public Sub Main()
                Console.WriteLine("Enter any text, followed by <Enter>:")
                Console.WriteLine()
                Dim s = Console.ReadLine()
                ShowWords(s)
                Console.Write($"{vbCrLf}Press any key to continue... ")
                Console.ReadKey()
            End Sub
    
            Private Sub ShowWords(s As String)
                Dim pattern = "\w+"
                Dim matches = Regex.Matches(s, pattern)
                Console.WriteLine()   
                If matches.Count = 0 Then
                    Console.WriteLine("No words were identified in your input.")
                Else
                    Console.WriteLine($"There are {matches.Count} words in your string:")
                    For ctr = 0 To matches.Count - 1
                        Console.WriteLine($"   #{ctr,2}: '{matches(ctr).Value}' at position {matches(ctr).Index}")
                    Next
                End If
                Console.WriteLine()
            End Sub
        End Module
    End Namespace
    
    
  3. 建立應用程式的偵錯組建。Create a Debug build of your app.

    選取 [組建] > [組建方案]。Select Build > Build Solution. 您也可以編譯並執行應用程式的偵錯組建,方法是選取 [偵錯] > [開始偵錯]。You can also compile and run the Debug build of your application by selecting Debug > Start Debugging.

  4. 部署應用程式。Deploy your app.

    在您偵錯並測試程式之後,請建立要隨應用程式一起部署的檔案。After you've debugged and tested the program, create the files to be deployed with your app. 若要從 Visual Studio 發行,請執行下列作業:To publish from Visual Studio, do the following:

    1. 在工具列上將方案組態從 [偵錯] 變更為 [發行],以組建應用程式的發行 (而不是偵錯) 版本。Change the solution configuration from Debug to Release on the toolbar to build a Release (rather than a Debug) version of your app.

    2. 以滑鼠右鍵按一下方案總管中的專案 (而非方案),然後選取 [發行]。Right-click on the project (not the solution) in Solution Explorer and select Publish.

    3. 在 [發行] 索引標籤中,選取 [發行]。In the Publish tab, select Publish. Visual Studio 會將構成應用程式的檔案寫入至本機檔案系統。Visual Studio writes the files that comprise your application to the local file system.

    4. [發行] 索引標籤現在會顯示單一設定檔 FolderProfileThe Publish tab now shows a single profile, FolderProfile. 設定檔的組態設定顯示在索引標籤的 [摘要] 區段中。The profile's configuration settings are shown in the Summary section of the tab.

    產生的檔案會放在 Windows 系統上名為 Publish 的目錄,以及 Unix 系統上名為 publish 的目錄中,而該目錄位於您專案之 .\bin\release\netcoreapp2.1 子目錄的子目錄中。The resulting files are placed in a directory named Publish on Windows and publish on Unix systems that is in a subdirectory of your project's .\bin\release\netcoreapp2.1 subdirectory.

隨著應用程式檔案一起,發佈程序會發出程式資料庫 (.pdb) 檔案,其中包含應用程式的偵錯資訊。Along with your application's files, the publishing process emits a program database (.pdb) file that contains debugging information about your app. 該檔案主要是用於例外狀況偵錯。The file is useful primarily for debugging exceptions. 您可以選擇不與您的應用程式檔案一起封裝它。You can choose not to package it with your application's files. 不過,如果您要對應用程式的發行組建進行偵錯,則應該將其保存。You should, however, save it in the event that you want to debug the Release build of your app.

以任何您想要的方式,部署整組應用程式檔案。Deploy the complete set of application files in any way you like. 例如,您可以使用簡單的 copy 命令將它們封裝在 ZIP 檔案中,或與您選擇的任何安裝套件一起部署。For example, you can package them in a Zip file, use a simple copy command, or deploy them with any installation package of your choice. 安裝之後,使用者可以使用 dotnet 命令並提供應用程式的檔名 (例如,dotnet fdd.dll),來執行您的應用程式。Once installed, users can then execute your application by using the dotnet command and providing the application filename, such as dotnet fdd.dll.

除了應用程式二進位檔之外,安裝程式也應該配套共用的 Framework 安裝程式,,或勾選為必要條件當成應用程式安裝的一部分。In addition to the application binaries, your installer should also either bundle the shared framework installer or check for it as a prerequisite as part of the application installation. 共用 Framework 安裝需要系統管理員/根目錄存取權,因為它要通行全機器。Installation of the shared framework requires Administrator/root access since it is machine-wide.

有協力廠商相依性的 Framework 相依部署Framework-dependent deployment with third-party dependencies

部署具有一或多個協力廠商相依性的 Framework 相依部署時,需要任何相依性都可供專案使用。Deploying a framework-dependent deployment with one or more third-party dependencies requires that any dependencies be available to your project. 在組建您的應用程式之前,需要執行下列其他步驟:The following additional steps are required before you can build your app:

  1. 使用 [NuGet 套件管理員],將 NuGet 套件的參考新增至您的專案;如果您的系統上還沒有該套件,請安裝它。Use the NuGet Package Manager to add a reference to a NuGet package to your project; and if the package is not already available on your system, install it. 若要開啟套件管理員,請選取 [工具] > [NuGet 套件管理員] > [管理方案的 NuGet 套件]。To open the package manager, select Tools > NuGet Package Manager > Manage NuGet Packages for Solution.

  2. 確認 Newtonsoft.Json 已安裝在您的系統上,如果尚未安裝,請安裝它。Confirm that Newtonsoft.Json is installed on your system and, if it is not, install it. [已安裝] 索引標籤會列出您系統上已安裝的 NuGet 套件。The Installed tab lists NuGet packages installed on your system. 如果 Newtonsoft.Json 未列於該處,請選取 [瀏覽] 索引標籤,然後在 [搜尋] 方塊中輸入 "Newtonsoft.Json"。If Newtonsoft.Json is not listed there, select the Browse tab and enter "Newtonsoft.Json" in the search box. 選取 Newtonsoft.Json,並先在右窗格中選取您的專案,然後選取 [安裝]。Select Newtonsoft.Json and, in the right pane, select your project before selecting Install.

  3. 如果 Newtonsoft.Json 已安裝在您的系統上,請在 [管理方案的封裝] 索引標籤的右窗格中選取您的專案,以將它新增至您的專案。If Newtonsoft.Json is already installed on your system, add it to your project by selecting your project in the right pane of the Manage Packages for Solution tab.

請注意,具有協力廠商相依性的 Framework 相依部署,可攜性只與其協力廠商相依性一致。Note that a framework-dependent deployment with third-party dependencies is only as portable as its third-party dependencies. 例如,如果協力廠商程式庫只支援 macOS,則應用程式就無法攜至 Windows 系統。For example, if a third-party library only supports macOS, the app isn't portable to Windows systems. 如果協力廠商相依性本身依賴於原生程式碼,就會發生這種情況。This happens if the third-party dependency itself depends on native code. 其中一個絶佳範例是 Kestrel 伺服器,它需要對 libuv 的原生相依性。A good example of this is Kestrel server, which requires a native dependency on libuv. 當具有這類協力廠商相依性的應用程式建立了 FDD 時,已發行輸出就會包含原生相依性支援的每個執行階段識別碼 (RID) 資料夾 (存在於其 NuGet 套件中)。When an FDD is created for an application with this kind of third-party dependency, the published output contains a folder for each Runtime Identifier (RID) that the native dependency supports (and that exists in its NuGet package).

沒有協力廠商相依性的自封式部署Self-contained deployment without third-party dependencies

部署無任何協力廠商相依性的自封式部署,涉及建立專案、修改 csproj 檔案、組建、測試以及發行應用程式。Deploying a self-contained deployment with no third-party dependencies involves creating the project, modifying the csproj file, building, testing, and publishing the app. 以 C# 撰寫的簡單範例會說明此程序。A simple example written in C# illustrates the process. 您首先要建立、編碼和測試您的專案,就像相依於 Framework 的部署一樣:You begin by creating, coding, and testing your project just as you would a framework-dependent deployment:

  1. 建立專案。Create the project.

    選取 [檔案] > [新增] > [專案]。Select File > New > Project. 在 [新增專案] 對話方塊中,展開 [已安裝] 專案類型窗格中的語言 (C# 或 Visual Basic) 專案類別,選擇 [.NET Core],然後選擇中央窗格中的 [主控台應用程式 (.NET Core)] 範本。In the New Project dialog, expand your language's (C# or Visual Basic) project categories in the Installed project types pane, choose .NET Core, and then select the Console App (.NET Core) template in the center pane. 在 [名稱] 文字方塊中輸入專案名稱 (例如 "SCD"),然後選取 [確定] 按鈕。Enter a project name, such as "SCD", in the Name text box, and select the OK button.

  2. 新增應用程式的原始程式碼。Add the application's source code.

    在您的編輯器中開啟Program.csProgram .vb檔案,並以下列程式碼取代自動產生的程式碼。Open the Program.cs or Program.vb file in your editor, and replace the auto-generated code with the following code. 它會提示使用者輸入文字,並顯示使用者輸入的個別文字。It prompts the user to enter text and displays the individual words entered by the user. 它會使用規則運算式 \w+ 分隔輸入文字中的字詞。It uses the regular expression \w+ to separate the words in the input text.

    using System;
    using System.Text.RegularExpressions;
    
    namespace Applications.ConsoleApps
    {
        public class ConsoleParser
        {
            public static void Main()
            {
                Console.WriteLine("Enter any text, followed by <Enter>:\n");
                String s = Console.ReadLine();
                ShowWords(s);
                Console.Write("\nPress any key to continue... ");
                Console.ReadKey();
            }
    
            private static void ShowWords(String s)
            {
                String pattern = @"\w+";
                var matches = Regex.Matches(s, pattern);
                if (matches.Count == 0)
                {
                    Console.WriteLine("\nNo words were identified in your input.");
                }
                else
                {
                    Console.WriteLine($"\nThere are {matches.Count} words in your string:");
                    for (int ctr = 0; ctr < matches.Count; ctr++)
                    {
                        Console.WriteLine($"   #{ctr,2}: '{matches[ctr].Value}' at position {matches[ctr].Index}");
                    }
                }
            }
        }
    }
    
    
    Imports System.Text.RegularExpressions
    
    Namespace Applications.ConsoleApps
        Public Module ConsoleParser
            Public Sub Main()
                Console.WriteLine("Enter any text, followed by <Enter>:")
                Console.WriteLine()
                Dim s = Console.ReadLine()
                ShowWords(s)
                Console.Write($"{vbCrLf}Press any key to continue... ")
                Console.ReadKey()
            End Sub
    
            Private Sub ShowWords(s As String)
                Dim pattern = "\w+"
                Dim matches = Regex.Matches(s, pattern)
                Console.WriteLine()   
                If matches.Count = 0 Then
                    Console.WriteLine("No words were identified in your input.")
                Else
                    Console.WriteLine($"There are {matches.Count} words in your string:")
                    For ctr = 0 To matches.Count - 1
                        Console.WriteLine($"   #{ctr,2}: '{matches(ctr).Value}' at position {matches(ctr).Index}")
                    Next
                End If
                Console.WriteLine()
            End Sub
        End Module
    End Namespace
    
    
  3. 決定您是否想使用全域無差異模式。Determine whether you want to use globalization invariant mode.

    尤其當您的應用程式以 Linux 為目標時,使用全域無差異模式能減少您部署的總大小Particularly if your app targets Linux, you can reduce the total size of your deployment by taking advantage of globalization invariant mode. 全域無差異模式適用於非全域應用程式,其能使用格式化慣例、大小寫慣例及字串比較,還有不因文化特性而異的排列次序Globalization invariant mode is useful for applications that are not globally aware and that can use the formatting conventions, casing conventions, and string comparison and sort order of the invariant culture.

    在您的專案 (而非解決方案) 點擊右鍵,進入 [方案總管],然後選取 [Edit SCD.csproj] (編輯 SCD.csproj) 或 [Edit SCD.vbproj] (編輯 SCD.vbproj) 啟用非變異模式。To enable invariant mode, right-click on your project (not the solution) in Solution Explorer, and select Edit SCD.csproj or Edit SCD.vbproj. 接著,將反白處新增至檔案中:Then add the following highlighted lines to the file:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.2</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <RuntimeHostConfigurationOption Include="System.Globalization.Invariant" Value="true" />
  </ItemGroup> 
</Project>
  1. 建立應用程式的偵錯組建。Create a Debug build of your application.

    選取 [組建] > [組建方案]。Select Build > Build Solution. 您也可以編譯並執行應用程式的偵錯組建,方法是選取 [偵錯] > [開始偵錯]。You can also compile and run the Debug build of your application by selecting Debug > Start Debugging. 透過此偵錯步驟,您可以識別應用程式在主機平台上執行時的問題。This debugging step lets you identify problems with your application when it's running on your host platform. 您仍然必須在每個目標平台上進行測試。You still will have to test it on each of your target platforms.

    如果您啟用了不因全球化而異的模式,請務必測試您的應用程式是否適合缺少不區分文化特性的資料。If you've enabled globalization invariant mode, be particularly sure to test whether the absence of culture-sensitive data is suitable for your application.

完成偵錯之後,您即可以發行獨立式部署:Once you've finished debugging, you can publish your self-contained deployment:

在您偵錯並測試程式之後,請針對每個目標平台建立要隨應用程式一起部署的檔案。After you've debugged and tested the program, create the files to be deployed with your app for each platform that it targets.

若要從 Visual Studio 發行您的應用程式,請執行下列作業:To publish your app from Visual Studio, do the following:

  1. 定義您應用程式目標的平台。Define the platforms that your app will target.

    1. 以滑鼠右鍵按一下 [方案總管] 中的專案 (而非解決方案),然後選取 [編輯 SCD.csproj]。Right-click on your project (not the solution) in Solution Explorer and select Edit SCD.csproj.

    2. 在定義應用程式目標平台之 csproj 檔案的 <PropertyGroup> 區段中建立 <RuntimeIdentifiers> 標記,並指定每個目標平台的執行階段識別碼 (RID)。Create a <RuntimeIdentifiers> tag in the <PropertyGroup> section of your csproj file that defines the platforms your app targets, and specify the runtime identifier (RID) of each platform that you target. 請注意,您也必須加上分號來分隔 RID。Note that you also need to add a semicolon to separate the RIDs. 如需執行階段識別碼清單,請參閱 .NET Core RID 類別目錄See Runtime IDentifier catalog for a list of runtime identifiers.

    例如,下列範例指出應用程式在 64 位元 Windows 10 作業系統和 64 位元 OS X 版本 10.11 作業系統上執行。For example, the following example indicates that the app runs on 64-bit Windows 10 operating systems and the 64-bit OS X Version 10.11 operating system.

    <PropertyGroup>
       <RuntimeIdentifiers>win10-x64;osx.10.11-x64</RuntimeIdentifiers>
    </PropertyGroup>
    

    請注意,<RuntimeIdentifiers> 項目可以移至您 csproj 檔案中的任何 <PropertyGroup>Note that the <RuntimeIdentifiers> element can go into any <PropertyGroup> that you have in your csproj file. 本節稍後會提供完整的 csproj 檔案範例。A complete sample csproj file appears later in this section.

  2. 發行您的應用程式。Publish your app.

    在您偵錯並測試程式之後,請針對每個目標平台建立要隨應用程式一起部署的檔案。After you've debugged and tested the program, create the files to be deployed with your app for each platform that it targets.

    若要從 Visual Studio 發行您的應用程式,請執行下列作業:To publish your app from Visual Studio, do the following:

    1. 在工具列上將方案組態從 [偵錯] 變更為 [發行],以組建應用程式的發行 (而不是偵錯) 版本。Change the solution configuration from Debug to Release on the toolbar to build a Release (rather than a Debug) version of your app.

    2. 以滑鼠右鍵按一下方案總管中的專案 (而非方案),然後選取 [發行]。Right-click on the project (not the solution) in Solution Explorer and select Publish.

    3. 在 [發行] 索引標籤中,選取 [發行]。In the Publish tab, select Publish. Visual Studio 會將構成應用程式的檔案寫入至本機檔案系統。Visual Studio writes the files that comprise your application to the local file system.

    4. [發行] 索引標籤現在會顯示單一設定檔 FolderProfileThe Publish tab now shows a single profile, FolderProfile. 設定檔的組態設定顯示在索引標籤的 [摘要] 區段中。[目標執行階段] 識別已發行的執行階段,而 [目標位置] 則識別自封式部署的檔案寫入位置。The profile's configuration settings are shown in the Summary section of the tab. Target Runtime identifies which runtime has been published, and Target Location identifies where the files for the self-contained deployment were written.

    5. Visual Studio 預設會將所有發行的檔案寫入到單一目錄。Visual Studio by default writes all published files to a single directory. 為了方便起見,最好是建立每個目標執行階段的個別設定檔,並將已發行的檔案放在特定平台目錄中。For convenience, it's best to create separate profiles for each target runtime and to place published files in a platform-specific directory. 這牽涉到建立每個目標平台的個別發行設定檔。This involves creating a separate publishing profile for each target platform. 因此,現在請執行下列作業來重建每個平台的應用程式:So now rebuild the application for each platform by doing the following:

      1. 選取 [建立新設定檔] 中的 [發行] 對話方塊。Select Create new profile in the Publish dialog.

      2. 在 [挑選發行目標] 對話方塊中,將 [選擇資料夾] 位置變更為 bin\Release\PublishOutput\win10 x64In the Pick a publish target dialog, change the Choose a folder location to bin\Release\PublishOutput\win10-x64. 選取 [確定]。Select OK.

      3. 選取設定檔清單中的新設定檔 (FolderProfile1),並確定 [目標執行階段] 是 win10-x64Select the new profile (FolderProfile1) in the list of profiles, and make sure that the Target Runtime is win10-x64. 如果不是,請選取 [設定]。If it isn't, select Settings. 在 [設定檔設定] 對話方塊中,將 [目標執行階段] 變更為 win10-x64,然後選取 [儲存]。In the Profile Settings dialog, change the Target Runtime to win10-x64 and select Save. 否則,請選取 [取消]。Otherwise, select Cancel.

      4. 選取 [發行] 來發行適用於 64 位元 Windows 10 平台的應用程式。Select Publish to publish your app for 64-bit Windows 10 platforms.

      5. 請再次遵循上述步驟,以建立適用於 osx.10.11-x64 平台的設定檔。Follow the previous steps again to create a profile for the osx.10.11-x64 platform. [目標位置] 是 bin\Release\PublishOutput\osx.10.11-x64,而 [目標執行階段] 是 osx.10.11-x64The Target Location is bin\Release\PublishOutput\osx.10.11-x64, and the Target Runtime is osx.10.11-x64. Visual Studio 指派給此設定檔的名稱是 FolderProfile2The name that Visual Studio assigns to this profile is FolderProfile2.

    請注意,每個目標位置都包含啟動應用程式所需的一組完整檔案 (應用程式檔案和所有的 .NET Core 檔案)。Note that each target location contains the complete set of files (both your app files and all .NET Core files) needed to launch your app.

隨著應用程式檔案一起,發佈程序會發出程式資料庫 (.pdb) 檔案,其中包含應用程式的偵錯資訊。Along with your application's files, the publishing process emits a program database (.pdb) file that contains debugging information about your app. 該檔案主要是用於例外狀況偵錯。The file is useful primarily for debugging exceptions. 您可以選擇不與您的應用程式檔案一起封裝它。You can choose not to package it with your application's files. 不過,如果您要對應用程式的發行組建進行偵錯,則應該將其儲存。You should, however, save it in the event that you want to debug the Release build of your app.

請使用任何您想要的方式,部署已發行的檔案。Deploy the published files in any way you like. 例如,您可以使用簡單的 copy 命令將它們封裝在 ZIP 檔案中,或與您選擇的任何安裝套件一起部署。For example, you can package them in a Zip file, use a simple copy command, or deploy them with any installation package of your choice.

下面是此專案的完整 csproj 檔案。The following is the complete csproj file for this project.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RuntimeIdentifiers>win10-x64;osx.10.11-x64</RuntimeIdentifiers>
  </PropertyGroup>
</Project>

有協力廠商相依性的自封式部署Self-contained deployment with third-party dependencies

部署具有一或多個協力廠商相依性的自封式部署,涉及新增相依性。Deploying a self-contained deployment with one or more third-party dependencies involves adding the dependencies. 在組建您的應用程式之前,需要執行下列其他步驟:The following additional steps are required before you can build your app:

  1. 使用 [NuGet 套件管理員],將 NuGet 套件的參考新增至您的專案;如果您的系統上還沒有該套件,請安裝它。Use the NuGet Package Manager to add a reference to a NuGet package to your project; and if the package is not already available on your system, install it. 若要開啟套件管理員,請選取 [工具] > [NuGet 套件管理員] > [管理方案的 NuGet 套件]。To open the package manager, select Tools > NuGet Package Manager > Manage NuGet Packages for Solution.

  2. 確認 Newtonsoft.Json 已安裝在您的系統上,如果尚未安裝,請安裝它。Confirm that Newtonsoft.Json is installed on your system and, if it is not, install it. [已安裝] 索引標籤會列出您系統上已安裝的 NuGet 套件。The Installed tab lists NuGet packages installed on your system. 如果 Newtonsoft.Json 未列於該處,請選取 [瀏覽] 索引標籤,然後在 [搜尋] 方塊中輸入 "Newtonsoft.Json"。If Newtonsoft.Json is not listed there, select the Browse tab and enter "Newtonsoft.Json" in the search box. 選取 Newtonsoft.Json,並先在右窗格中選取您的專案,然後選取 [安裝]。Select Newtonsoft.Json and, in the right pane, select your project before selecting Install.

  3. 如果 Newtonsoft.Json 已安裝在您的系統上,請在 [管理方案的封裝] 索引標籤的右窗格中選取您的專案,以將它新增至您的專案。If Newtonsoft.Json is already installed on your system, add it to your project by selecting your project in the right pane of the Manage Packages for Solution tab.

下面是此專案的完整 csproj 檔案:The following is the complete csproj file for this project:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RuntimeIdentifiers>win10-x64;osx.10.11-x64</RuntimeIdentifiers>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Newtonsoft.Json" Version="10.0.2" />
  </ItemGroup>
</Project>

當您部署應用程式時,應用程式中任何協力廠商相依性也包含在應用程式檔案中。When you deploy your application, any third-party dependencies used in your app are also contained with your application files. 應用程式執行所在的系統上不需要協力廠商程式庫。Third-party libraries aren't required on the system on which the app is running.

請注意,您只能將具有協力廠商程式庫的自封式部署,部署到該程式庫支援的平台上。Note that you can only deploy a self-contained deployment with a third-party library to platforms supported by that library. 這類似於在與 Framework 相依的部署中擁有仰賴原生相依性的協力廠商相依性;在其中,原生相依性不會存在於目標平台上,除非先前已在該處安裝這些相依性。This is similar to having third-party dependencies with native dependencies in your framework-dependent deployment, where the native dependencies won't exist on the target platform unless they were previously installed there.

另請參閱See also