這項工具採用 Gemini 生成式 AI 模型,可生成程式碼並翻新 .NET 應用程式。這項工具可協助您將依附於 Windows 的 Microsoft .NET Framework 應用程式翻新為跨平台 .NET 程式碼。
與 .NET Framework 應用程式不同,跨平台 .NET 應用程式可在 Linux 上執行,因此不需要 Windows 授權,並可啟用容器化等雲端原生功能。
在本指南中,您將為 Google Cloud專案授予 IAM 角色、建立 Cloud Storage 值區、建立 Gemini API 金鑰,並在本機安裝 code-modernization-for-dotnet
擴充功能,將 .NET Framework 應用程式重構為跨平台 .NET 程式碼。
事前準備
- 如果您還沒有帳戶,請建立 Google 帳戶。您可以使用任何電子郵件地址建立 Google 帳戶,包括公司電子郵件地址。
- 提交 Code Modernization for .NET:許可清單要求表單,即可加入許可清單。您必須等到這項要求獲准,才能繼續執行這個頁面的步驟。
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Secret Manager, Cloud Storage JSON API, Generative Language API, Cloud Logging, and Compute Engine APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
-
Enable the Cloud Build, Secret Manager, Cloud Storage JSON API, Generative Language API, Cloud Logging, and Compute Engine APIs.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin
), which contains theserviceusage.services.enable
permission. Learn how to grant roles. - 如果尚未安裝,請安裝 VS Code。
如要授予 Google Cloud 專案的 IAM 角色,請前往「IAM 與管理」頁面。
新增下列 IAM 角色:
使用者權限
- Storage 物件使用者 (
roles/storage.objectUser
) - Cloud Build 編輯者 (
roles/cloudbuild.builds.editor
)
- Storage 物件使用者 (
Cloud Build 服務帳戶
- Cloud Build 服務帳戶 (
roles/cloudbuild.builds.builder
) - Secret Manager 檢視者 (
roles/secretmanager.viewer
) - 記錄寫入者 (
roles/logging.logWriter
) - Storage 物件使用者 (
roles/storage.objectUser
)
- Cloud Build 服務帳戶 (
指派 Storage 物件使用者 (
roles/storage.objectUser
) 角色:gcloud projects add-iam-policy-binding PROJECT_ID \ --member="user:USER_ID" \ --role="roles/storage.objectUser"
指派 Cloud Build 編輯者 (
roles/cloudbuild.builds.editor
) 角色:gcloud projects add-iam-policy-binding PROJECT_ID \ --member="user:USER_ID" \ --role="roles/cloudbuild.builds.editor"
指派 Cloud Build 服務帳戶 (
roles/cloudbuild.builds.builder
) 角色:gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:SERVICE_ACCOUNT" \ --role="roles/cloudbuild.builds.builder"
指派 Secret Manager 存取者 (
roles/secretmanager.secretAccessor
) 角色:gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:SERVICE_ACCOUNT" \ --role="roles/secretmanager.secretAccessor"
指派「記錄寫入者」角色 (
roles/logging.logWriter
):gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:SERVICE_ACCOUNT" \ --role="roles/logging.logWriter"
指派 Storage 物件使用者 (
roles/storage.objectUser
) 角色:gcloud projects add-iam-policy-binding PROJECT_ID \ --member="serviceAccount:SERVICE_ACCOUNT" \ --role="roles/storage.objectUser"
在 VS Code 中,前往活動列的「擴充功能」。
按一下「Views and More Actions」(檢視畫面和更多動作) more_horiz,然後選取「Install from VSIX」(從 VSIX 安裝)。
找到並選取 VSIX 套件檔案。這樣一來,系統就會在本機電腦的 VS Code 中安裝
Code-modernization-for-dotnet
擴充功能。安裝完成後,請重新啟動 VS Code。
將
Code Modernization for .NET extension
設定更新為下列內容:- 擴充模式:
LocalGit (experimental)
- 專案 ID:
PROJECT_ID
- Bucket Name (值區名稱):
BUCKET_NAME
- Region (區域):
us-central1
- Gemini API 密鑰:
gemini-api-key-secret-name
- Google 帳戶:
GOOGLE_ACCOUNT
- 目標架構:
.NET 8.0
更改下列內容:
PROJECT_ID
:您的 Google Cloud專案 ID,例如1234567890
。BUCKET_NAME
:Cloud Storage 值區的名稱。GOOGLE_ACCOUNT
:您的 Google 帳戶,例如your_full_name@gmail.com
。
- 擴充模式:
如要登入 Google Cloud,請執行下列指令:
gcloud auth login --update-adc
如要觸發現代化工作流程,請在活動列中選取「Source Control」(來源控管),按一下「更多動作」more_horiz開啟內容選單,然後選取「Google Cloud: Modernize .NET code」(Google Cloud:將 .NET 程式碼現代化)。
授予 Identity and Access Management (IAM) 角色
主控台
gcloud
如要新增必要的 IAM 角色,請執行下列gcloud
指令:
建立 Cloud Storage 值區
如要建立 Cloud Storage bucket,請執行下列指令:
gcloud storage buckets create gs://BUCKET_NAME \
--project=PROJECT_ID
建立 Gemini API 金鑰並儲存在 Secret Manager 中
如要建立 Gemini API 金鑰,請執行下列操作:
gcloud services api-keys create --project=PROJECT_ID \
--display-name="API Key for .Net Modernization" \
--api-target="service=generativelanguage.googleapis.com" \
--key-id="gemini-api-key"
如要在 Secret Manager 中儲存 Gemini API 金鑰,請執行下列指令:
echo -n "(gcloud services api-keys get-key-string gemini-api-key \
--project=PROJECT_ID \
--format="value(keyString)")" | \
gcloud secrets create "gemini-api-key" \
--project=PROJECT_ID \
--data-file=- \
--replication-policy="automatic"
安裝及設定擴充功能
.NET 程式碼現代化:許可清單要求表單通過核准後,您會收到 .VSIX
檔案格式的擴充功能。如要在本機電腦上安裝擴充功能,請按照下列步驟操作:
觸發建構作業
將專案加入許可清單
如要將專案加入允許清單,請將 Cloud Build 服務帳戶 ID 傳送電子郵件至 code-modernization-for-dotnet-discuss@google.com
。
你也可以透過這個電子郵件地址取得支援、提出問題或提供意見回饋。