如何通过VSTS WebApi监听构建完成?求PollingCompleted等价方案
Got it, let's break this down for you—since you're shifting from XAML builds using the old IQueuedBuild model to the Azure DevOps Build Web API, you're right to look for an equivalent to the PollingCompleted event. Here's what you need to know:
PollingCompleted in Azure DevOps Build Web API The Web API doesn't have a direct client-side event like PollingCompleted (since it's REST-based, not a stateful client connection), but you have two solid options to get notified when a build finishes:
Option 1: Simulate Polling (Closest to Your Original Workflow)
This mimics the behavior of PollingCompleted exactly—you'll periodically check the build status until it's no longer in progress. It's straightforward and aligns with how your original code worked.
Here's a C# example using the BuildClient from the Azure DevOps .NET SDK:
using Microsoft.TeamFoundation.Build.WebApi; using System; using System.Threading.Tasks; public async Task MonitorBuildUntilCompletion(int buildId, BuildClient buildClient) { Build currentBuild; do { // Fetch the latest build status currentBuild = await buildClient.GetBuildAsync(buildId); // Wait before polling again (adjust the interval to fit your needs) await Task.Delay(TimeSpan.FromSeconds(15)); } while (currentBuild.Status == BuildStatus.InProgress || currentBuild.Status == BuildStatus.NotStarted); // Trigger your build completion logic here—this is the "PollingCompleted" equivalent HandleBuildCompletion(currentBuild); } private void HandleBuildCompletion(Build completedBuild) { // Your existing logic to analyze build results goes here Console.WriteLine($"Build {completedBuild.Id} finished with status: {completedBuild.Status}"); }
Option 2: Use BuildCompletedEvent with Service Hooks (Push Notifications)
If you want a true event-driven approach (no polling), you can use the BuildCompletedEvent from Microsoft.TeamFoundation.Build.WebApi.Events by setting up an Azure DevOps Service Hook. This lets Azure DevOps push a notification to your app as soon as the build finishes.
Step 1: Set Up a Web Endpoint in Your App
First, create an API endpoint to receive the event payload. Here's an example using ASP.NET Core:
using Microsoft.AspNetCore.Mvc; using Microsoft.TeamFoundation.Build.WebApi.Events; [ApiController] [Route("azuredevops/hooks")] public class BuildHookController : ControllerBase { [HttpPost("buildcompleted")] public IActionResult ReceiveBuildCompleted([FromBody] BuildCompletedEvent buildEvent) { // Extract build details from the event var buildId = buildEvent.Resource.Id; var buildStatus = buildEvent.Resource.Status; var buildDefinitionName = buildEvent.Resource.Definition.Name; // Run your build result analysis logic ProcessBuildResults(buildId, buildStatus, buildDefinitionName); return Ok(); // Acknowledge the notification to Azure DevOps } private void ProcessBuildResults(int buildId, BuildStatus status, string definitionName) { // Custom logic to analyze the build outcome } }
Step 2: Configure the Service Hook in Azure DevOps
- Go to your Azure DevOps project > Project Settings > Service Hooks
- Click Create subscription
- Select Build as the service, then Build completed as the event
- Configure which builds trigger the event (specific definitions, all builds, etc.)
- Choose Web Hooks as the action, and enter the URL of your endpoint (e.g.,
https://yourapp.com/azuredevops/hooks/buildcompleted) - Save the subscription—now Azure DevOps will send a
BuildCompletedEventpayload to your endpoint every time a build finishes.
- Use the polling approach if you want behavior identical to your original
PollingCompletedevent (client-side checks, no external setup needed). - Use the Service Hook +
BuildCompletedEventapproach if you prefer a scalable, push-based model that avoids unnecessary API calls.
内容的提问来源于stack exchange,提问作者Efrat

