目录
概述
使用微软官方的类库 (Microsoft.AspNet.WebHooks.Receivers)
- 使用说明:https://docs.microsoft.com/en-us/aspnet/webhooks/receiving/handlers
- NuGet:https://www.nuget.org/packages/Microsoft.AspNet.WebHooks.Receivers.VSTS/
- 示例代码:https://github.com/aspnet/AspNetWebHooks/tree/main/samples/VstsReceiver
net core版本的也有,但是目前对azure devops本身还没有支持,现在还没有 Microsoft.AspNetCore.WebHooks.Receivers.VSTS https://github.com/aspnet/WebHooks
实现
web项目配置
绑定到IIS站点,F5就可以直接附加到iis站点,可使用postman来触发(body中的内容可以去ads的webhook的请求中复制),也可以直接等待来自ad是触发(前提是站点地址配置到ads的webhook中)
C# 示例代码
public partial class VstsWebHookHandler : VstsWebHookHandlerBase
{
public VstsWebHookHandler() : base()
{
}
/// <summary>
/// We use <see cref="VstsWebHookHandlerBase"/> so just have to override the methods we want to process WebHooks for.
/// This one processes the <see cref="BuildCompletedPayload"/> WebHook.
/// </summary>
public override Task ExecuteAsync(WebHookHandlerContext context, BuildCompletedPayload payload)
{
try // 记得一定要try catch,web hook上会有错误计数,到了一定的数量就会自动停止webhook
{
if (payload.Resource.Reason.ToLower() == "usercreated")
{
Logger.Info("hello !");
}
}
catch (Exception ex)
{
Logger.Error("Webhooks Occurred Error.Evnet:" + payload.EventType, ex);
}
return Task.FromResult(true);
}
}
Azure Devops 配置
项目设置 ,服务挂钩,点击“+”新建服务挂钩,选择“Web 挂钩”,点击“下一步”按钮
选择出发的事件,或者指定的字段
录入URL地址,例如:
http://ads.jasonchen.com:8080/api/webhooks/incoming/vsts?code=83699ec7c1d794c0c780e49a5c72972590571fd8
确保状态“已启动”,如果有错误可以通过“历史记录”查看最新的错误