在 App Engine 彈性環境中建立 .NET 應用程式

區域 ID

REGION_ID 是 Google 根據您在建立應用程式時選取的地區所指派的縮寫代碼。此代碼不對應至國家/地區或省份,即使部分區域 ID 可能與常用的國家/地區和省份代碼相似。如果是 2020 年 2 月後建立的應用程式,App Engine 網址會包含 REGION_ID.r。如果是這段時間前建立的現有應用程式,網址可選擇是否包含地區 ID。

進一步瞭解區域 ID

本快速入門導覽課程說明如何建立及部署可顯示簡短訊息的應用程式。您可以在 app.yaml 檔案中指定執行階段版本和作業系統,在任何支援的 .NET 版本中使用本快速入門中的範例應用程式。 如果您未在 app.yaml 檔案中指定執行階段版本,App Engine 預設會使用最新的可用 LTS .NET 版本。

事前準備

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Make sure that billing is enabled for your Google Cloud project.

  4. Enable the Cloud Build API.

    Enable the API

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  7. To initialize the gcloud CLI, run the following command:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  9. Make sure that billing is enabled for your Google Cloud project.

  10. Enable the Cloud Build API.

    Enable the API

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

  13. To initialize the gcloud CLI, run the following command:

    gcloud init
  14. 由於新專案中 Cloud Build 使用服務帳戶的預設行為有所變更,以及預設安全機構政策變更,您可能需要將額外角色授予部署服務帳戶。如要進一步瞭解如何授予特定角色,請參閱疑難排解指南
  15. 其他必要條件

    1. 使用您的專案將 App Engine 應用程式初始化,然後選擇地區:

      gcloud app create --project=[YOUR_PROJECT_ID]
      

      收到系統提示時,選取 App Engine 應用程式的存放地區

    2. 在本機電腦上安裝下列項目:

      • 安裝 .NET Core SDK (LTS 版本)

      • 如果您使用 Visual Studio,就必須安裝 2015 版或更新的版本。針對 .NET Core 1.0、1.1、2.0 和 2.1 編寫的 ASP.NET Core 應用程式,有映像檔可用。

      • 如要直接從 Visual Studio 部署至 App Engine,請安裝 Tools for Visual Studio

    App Engine 位置

    App Engine 具有「地區性」,這表示執行應用程式的基礎架構位於特定地區,且由 Google 代管,可為該地區內的所有區域提供備援功能。

    選擇應用程式的執行地區時,請將您在延遲時間、可用性和耐用性方面的要求,做為主要考量。一般來說,您可以選擇距離您應用程式使用者最近的地區,但您也應該考慮 App Engine 的可用位置,以及應用程式使用的其他Google Cloud 產品和服務的位置。如果跨多個位置使用服務,可能會影響應用程式的延遲時間和定價

    應用程式的地區設定完成後即無法更改。

    如果您已建立 App Engine 應用程式,可以透過下列方式查看其區域:

    • 請執行 gcloud app describe 指令。

    • 在 Google Cloud 控制台中開啟 App Engine 資訊主頁。區域會顯示在頁面頂端附近。

    本快速入門導覽課程假設您已熟悉使用 C# 建構網路應用程式。

下載 Hello World 應用程式

我們建立了 App Engine 適用的 Hello World 應用程式,方便您快速瞭解在 Google Cloud部署應用程式的操作步驟。Hello World 應用程式類似於您在建立空白的 ASP.NET Core 應用程式時,Visual Studio 所建立的應用程式。範例應用程式會新增 app.yaml 檔案。app.yaml 檔案是 App Engine 的設定檔,可用來指定您的執行階段和其他 App Engine 設定。

  1. 將 Hello World 範例應用程式存放區複製到本機電腦。

    git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples
    

    您也可以下載 zip 格式的範例,然後解壓縮該檔案。

  2. 變更為包含範例程式碼的目錄。

       cd dotnet-docs-samples/appengine/flexible/HelloWorld
    

在本機電腦上執行 Hello World

  1. dotnet-docs-samples/appengine/flexible/HelloWorld/HelloWorld.Sample 目錄執行下列指令:

    dotnet restore
    dotnet run
    
  2. 在網路瀏覽器中前往 http://localhost:5000。 此時您會在頁面上看到來自範例應用程式的「Hello World」訊息。在終端機視窗中,按下 Ctrl+C 來關閉網路伺服器。

在 App Engine 上部署並執行 Hello World

  1. 透過指令列執行 gcloud app deploy
  2. 啟動瀏覽器,並前往 https://PROJECT_ID.REGION_ID.r.appspot.com gcloud app browse 查看應用程式

此時,顯示 Hello World 訊息的頁面是由在 App Engine 執行個體上運作的網路伺服器提供。

恭喜!您已成功將第一個 App Engine 應用程式部署至 App Engine 彈性環境!

如果部署應用程式時發生錯誤,請參閱疑難排解提示

請參閱下列各節,瞭解清除所用資源的資訊,並取得相關後續步驟的連結。

清除所用資源

如要避免支付費用,您可以刪除 Google Cloud 專案,這樣系統就會停止對該專案使用的所有資源計費。

  1. In the Google Cloud console, go to the Manage resources page.

    Go to Manage resources

  2. In the project list, select the project that you want to delete, and then click Delete.
  3. In the dialog, type the project ID, and then click Shut down to delete the project.

後續步驟

瞭解整個平台

現在您已瞭解如何開發及部署 App Engine 應用程式,接著可進一步探索 Google Cloud。您已安裝 Google Cloud CLI,因此擁有能與 Cloud SQL、Cloud Storage、Firestore 等產品互動的工具。

瞭解 App Engine 彈性環境

以下是一些可協助您瞭解 App Engine 的主題:

Hello World 程式碼審查

Hello World 是再簡單不過的 App Engine 應用程式,只包含一項服務、只有一種版本,且所有程式碼都位於應用程式的根目錄中。本節將詳細說明應用程式的每個檔案。

Program.cs

Hello World 是簡單的 ASP.NET 應用程式:

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/", () => builder.Configuration["My:Greeting"]);

app.Run();

app.yaml

app.yaml 檔案說明應用程式的下列設定:

  • 設定 env: flex,指出應用程式使用 App Engine 彈性環境
  • 指定應用程式使用的執行階段。

    runtime: aspnetcore
    env: flex
    
    runtime_config:
      operating_system: ubuntu22
    
    # This sample incurs costs to run on the App Engine flexible environment. 
    # The settings below are to reduce costs during testing and are not appropriate
    # for production use. For more information, see:
    # https://cloud.google.com/appengine/docs/flexible/dotnet/configuring-your-app-with-app-yaml
    manual_scaling:
      instances: 1
    resources:
      cpu: 1
      memory_gb: 0.5
      disk_size_gb: 10
    
    env_variables:
      # The __ in My__Greeting will be translated to a : by ASP.NET.
      My__Greeting: Hello AppEngine!