You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过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:

Equivalent to 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

  1. Go to your Azure DevOps project > Project Settings > Service Hooks
  2. Click Create subscription
  3. Select Build as the service, then Build completed as the event
  4. Configure which builds trigger the event (specific definitions, all builds, etc.)
  5. Choose Web Hooks as the action, and enter the URL of your endpoint (e.g., https://yourapp.com/azuredevops/hooks/buildcompleted)
  6. Save the subscription—now Azure DevOps will send a BuildCompletedEvent payload to your endpoint every time a build finishes.
Which Should You Use?
  • Use the polling approach if you want behavior identical to your original PollingCompleted event (client-side checks, no external setup needed).
  • Use the Service Hook + BuildCompletedEvent approach if you prefer a scalable, push-based model that avoids unnecessary API calls.

内容的提问来源于stack exchange,提问作者Efrat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:25:09