????Web 應用程序通常需要作為后臺任務運行的進程,并在特定時間間隔進行計劃或在事件中觸發。它們不需要花哨的 IO 接口,因為重點是過程而不是輸出。Azure WebJobs 提供了出色的支持,通常在云環境中通過 Web 控制臺應用程序來實現此目的。WebJob?是?Azure 應用服務的一項功能,可用于在與 Web 應用、API 應用或移動應用相同的實例中運行程序或腳本。?Azure 應用服務是一種基于 HTTP 的服務,用于托管 Web 應用程序、REST API 和移動后端。
????.NET Framework?中的 Web 控制臺應用可輕松使用 Azure 服務生成和部署 WebJobs。此外,可以使用?Azure DevOps Pipelines?輕松為 CI/CD 配置 WebJobs 控制臺應用,以便在每次成功生成時自動將 Web 應用部署到 Azure 應用服務。因此,Azure 管道支持使用 Azure DevOps 服務以持續集成 (CI) /持續交付 (CD)?方式進行生成、測試和部署。
????如果在 .NET Framework 中生成 WebJob 控制臺應用,則通過將父 API 或 Web 應用鏈接到 Web/API 應用,可以非常輕松地生成和部署 WebJob,如下所示:


????.NET Core 中的設計不支持上述 WebJob 控制臺應用程序與父 Web/API 應用的鏈接。那么我們該怎么做呢?
????在本文中,我們將了解如何獨立于父 Web/API 應用生成 .NET Core WebJob 控制臺應用,并使用 Azure DevOps 生成和發布管道將其部署在現有的父 Web/API 應用下。
生成管道:生成 .NET Core 控制臺應用
若要生成 .NET Core 控制臺應用,我們需要在 Azure DevOps 生成管道中執行四個基本生成任務。
dotnet restore
dotnet build
dotnet publish
將生成項目發布到放置位置
Azure Web 作業僅支持以下文件類型:
.cmd、.bat、.exe
.ps1(使用 PowerShell.ps1 (using?PowerShell)
.sh(使用 Bash)
.php(使用 PHP)
.py(使用?Python)
.js(使用Node.js)
.jar(使用?Java)
由于我們位于 .NET Core 中,因此我們期望有一個 .exe 文件作為控制臺應用的輸出。如果僅通過運行來生成 .NET Core 應用,它將生成一個 .DLL 文件,但不會生成.EXE文件。若要生成.EXE文件作為生成輸出,請在步驟中使用以下參數:dotnet build
publish
--configuration $(BuildConfiguration) --self-contained -r win10-x64 --output $(build.artifactstagingdirectory)
????這個參數在這里會有所不同,因為這將在構建中生成一個.EXE文件。--self-contained -r win10-x64
????接下來,選擇該選項。這將從生成輸出中創建一個 ZIP 文件。Enable Zip Published Projects
一旦它被發布到ZIP文件夾并完成,我們需要將此ZIP文件拖放到放置位置。用作要發布的路徑。使用您選擇的任何項目名稱。在此示例中,項目名稱為:$(Build.ArtifactStagingDirectory)
webjobs_drop
發布管道:將生成項目部署到 Azure 應用服務
運行生成管道后,你將在位置獲取生成輸出。webjobs_drop/.zip
若要將其部署到 Azure 應用服務中運行的任何現有 Web/API 應用的 WebJob,我們必須運行 Azure PowerShell 腳本任務來實現此目的。
我們將用于將生成輸出部署到應用服務。這會將壓縮的生成輸出解壓縮到文件夾中。KUDU Zip Deploy API
wwwroot\App_Data\Jobs\\?
若要設置發布管道,我們需要使用以下管道變量創建一個空管道:
resourceGroupName
- 應用程序的資源組名稱scheduleName
- 有爭議/觸發(在本例中,它是連續的)scmApiUrl
- SCM API 網址;示例:https://.scm.azurewebsites.net/apiwebJobName
- 您的 WebJob 名稱;您的代碼將部署在 wwwroot\App_Data\Jobs\scheduleName\webJobName 文件夾下zippedArtifactPath
- 構建輸出文件的壓縮工件路徑

設置變量后,我們需要執行以下操作:
添加一個
Azure PowerShell script task
選擇“Azure 連接”類型
選擇 WebApp 所在的 Azure 訂閱
選擇“腳本類型”作為內聯腳本
在內聯腳本編輯器中,添加以下 PowerShell 腳本:
#Zipped artifact path - get the path from Azure DevOps Pipeline variables
$path = "$(System.DefaultWorkingDirectory)\$($env:zippedArtifactPath)"#Test the path if exists
if (-not (Test-Path $path))
{throw [System.IO.FileNotFoundException] "$($path) not found."
}#Resource type and details
$resourceType = "Microsoft.Web/sites/config"
$resourceName = "$($env:webAppName)/publishingcredentials"#Get the Publishing Profile details
$publishingCredentials = Invoke-AzureRmResourceAction -ResourceGroupName $($env:resourceGroupName) -ResourceType $resourceType -ResourceName $resourceName -Action list -Force#Creating the Auth Token using user name and password from Publish profile credentials
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName,$publishingCredentials.Properties.PublishingPassword)))#Get the file from the Build Artifacts path
$files = Get-ChildItem -Path $path -Recurse
Test-Path -Path $files[0] #Authentication Header
$authHeader = " Basic " + $base64AuthInfo#Kudu Zip Deploy API URL
$deployUrl = "$($env:scmApiUrl)/zip/site/wwwroot/App_Data/jobs/$($env:scheduleName)/$($env:webJobName)/"#Invoke Kudu Zip Deploy API to deploy the WebJob
$contentType = "multipart/form-data"
$userAgent = "powershell/1.0"
$ZipHeaders = @{
Authorization = $authHeader
}$response = Invoke-RestMethod -Uri ([System.Uri]::new($deployUrl)) -Headers $ZipHeaders -UserAgent $userAgent -Method PUT -InFile $files[0] -ContentType $contentTypeWrite-Host "Deployment Successful"
此 PowerShell 腳本將獲取 Web 應用的發布配置文件詳細信息,并使用此信息登錄到 KUDU ZipDeploy API,以將 Zipped 生成輸出上傳到所需位置。
部署位置定義如下:
SCM API URL 和虛擬目錄路徑
?https://<>.scm.azurewebsites.net/api/zip?
site/wwwroot/App_Data/jobs/<continuous/triggered>/
訪問特定于環境的配置
上述生成和發布管道將為 .NET Core 控制臺應用程序生成并生成.EXE文件,并將生成輸出部署到 Azure 應用服務虛擬目錄。在某些情況下,Web 作業可能需要讀取某些特定于環境的配置。在這種情況下,您必須將特定于環境的文件添加為 .appsettings.json
appsettings.json
要從特定于環境的 JSON 文件中讀取值,我們可以依賴該變量。可以從 Azure 門戶手動設置此變量,也可以在應用設置部分使用 Azure 發布管道部署父 WebApp/API 應用時設置此變量。ASPNETCORE_ENVIRONMENT
builder.ConfigureAppConfiguration((hostingContext, config) =>{var environmentName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");Console.WriteLine("Environment Variable: " + environmentName);var env = hostingContext.HostingEnvironment;Console.WriteLine("hostingContext.HostingEnvironment: " + env.EnvironmentName);config.SetBasePath(Directory.GetCurrentDirectory()).AddJsonFile($"appsettings.json", optional: true, reloadOnChange: true).AddJsonFile($"appsettings.{environmentName}.json", optional: true, reloadOnChange: true).AddEnvironmentVariables().Build();});
將上述代碼添加到文件中,以便從文件中讀取特定于環境的值。Program.cs
appsettings.json
總而言之,我們執行了以下操作:
dotnet publish
with 參數,用于生成 WebJob 支持的 EXE 文件--self-contained -r win10-x64?
壓縮生成項目目錄
使用 API 將 WebJob 控制臺應用程序發布到現有的 web/API 應用程序虛擬目錄。
KUDU ZipDeploy
Azure PowerShell Script