这是我所做的:
1 - 我已经安装了 nuget 包:Microsoft.AspNet.WebHooks.Receivers.Custom 1.2.0-beta
2 - 我配置了WebApiConfig
接收自定义 webhook:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.InitializeReceiveCustomWebHooks(); //<<<---
}
3 - 我在 web.config 中设置了一个密钥:
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
...
<add key="MS_WebHookReceiverSecret_GenericJson" value="z=SECRET"/>
</appSettings>
4 - 我编写了一个基本接收器(使用 genericjson 钩子捕获)
public class GenericJsonWebHookHandler : WebHookHandler
{
public static string dataReceived;
public GenericJsonWebHookHandler()
{
this.Receiver = "genericjson";
}
public override Task ExecuteAsync(string generator, WebHookHandlerContext context)
{
// Get JSON from WebHook
JObject data = context.GetDataOrDefault<JObject>();
if (context.Id == "i")
{
// do stuff
}
else if (context.Id == "z")
{
// do more stuff
dataReceived = data.ToString();
File.Create(@"c:\test\test1.txt");
}
return Task.FromResult(true);
}
}
现在,通过上述步骤,如果设置 Webhook 发送方将 Json 发布到 IIS 托管站点,它应该将通知捕获为 Json,将捕获的数据分配给dataReceived
并写入一个空白文本文件c:\test\test.txt
- 事实并非如此
目前,我正在使用 Team Foundation Server 进行测试,将 webhook 测试发送到https://mywebbhooksite.com:5050/?z=SECRET
,和它succeeds- 但是,当我检查那个小测试文件是否已创建时,它不存在。我还在主页上运行了一些 JavaScript 来轮询是否有任何更改dataReceived
但我看到什么也没有发生。
这里提到:我有一个附加到 w3wp.exe 进程的远程调试器,ExecuteAsync 和 GenericJsonWebHookHandler 上的断点没有被命中
为了捕获 Webhook 是否还需要执行任何其他特定设置?