如何使用唯一/我自己的名字注册网络挂钩

时间:2019-07-16 07:55:39

标签: c# asp.net webhooks

摘要::我想要一个带有特定Webhook名称的Wehook网址,而不是... / custom /或... / genericjson /(如果使用这些nuget包)。

https://localhost:44300/api/webhooks/incoming/MySpecialSuperDuperWebhook?code=1234567890123456789012345678901234567890


我发现的所有指南等都是针对现有Webhook nuget软件包(Github,bitbucket,slack等)或“通用”和“自定义”软件包(除了名称,我真的不了解它们的区别)但这又是另一回事。
我已经启动并运行了 genericjson ,它正在执行预期的操作,但是我宁愿为Webhook拥有一个更正确的url和/或名称。

那么我该如何获得(我假设这是我需要使其按需运行)

config.InitializeReceiveGenericJsonWebHooks();

相反,就像在工作一样

config.InitializeReceiveMySpecialSuperDuperWebhookWebHooks();

,因此我的服务器在上述URL上侦听webhook(前提是我将密钥添加到appSettings中,依此类推)。

我尝试查看 InitializeReceiveGenericJsonWebHooks() InitializeReceiveGitHubWebHooks()的定义,但是那里几乎没有任何代码,我也不知道如何/在什么地方创建我自己的版本。
我是Asp.net的新手(以前在Apache上大多数都是在html / css / php / javascript中编码的网站,而不是在IIS上是Asp.net/C#),所以我认为应该有一些简单的方法来解决它,但是经过一整天的搜索和尝试之后,我对下一步的工作还是无能为力。


修改:
目前,这就是我的代码(与Webhook相关)的外观。没什么特别的,只是让网络挂钩正常工作的最低限度。

Web.config

  <appSettings>
    <add key="MS_WebHookReceiverSecret_GenericJson" value="1234567890123456789012345678901234567890"/>
  </appSettings>

WebApiConfig.cs

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        //The revelant part to the question
        config.InitializeReceiveGenericJsonWebHooks();
    }
}

GenericJsonWebHookHandler.cs

namespace Project.API.Webhooks
{
    public class GenericJsonWebHookHandler : WebHookHandler
    {
        public GenericJsonWebHookHandler()
        {
            this.Receiver = "genericjson";
        }

        public override Task ExecuteAsync(string receiver, WebHookHandlerContext context)
        {
            // Get JSON from WebHook
            JObject data = context.GetDataOrDefault<JObject>();
            //Do something with webhook
            return Task.FromResult(true);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

我终于设法解决了。

通过查看GitHub of AspNetWebHooks并深入到我正在使用的GenericJSON WebHook,我终于找到了所缺少的内容。

唯一需要做的就是创建一个名为HttpConfigurationExtensions.cs的文件,然后将代码从HttpConfigurationExtensions.cs复制并粘贴到新创建的文件中并进行更改

public static void InitializeReceiveGenericJsonWebHooks(this HttpConfiguration config)

我想要它听的东西

public static void InitializeReceiveMyWebhookWebHooks(this HttpConfiguration config)

像这样

然后,我还需要创建一个名为MyWebhookReceiver.cs的文件,然后复制并粘贴GenericJsonWebHookReceiver.cs中的代码,并稍作更改

    public class GenericJsonWebHookReceiver : WebHookReceiver
    {
        internal const string RecName = "genericjson";

进入

    public class MyWebhookWebHookReceiver : WebHookReceiver
    {
        internal const string RecName = "MyWebhook";

它仍然像GenericJSON webhook(这就是我想要的)一样工作,但是现在我可以使用/api/incoming/MyWebhook/whateverIWant?code=123456790作为webhook URL,而不必像/genericjson/MyWebhook?code=1234567890那样。

您仍然需要处理程序文件,但是以同样的方式,这两个文件略有更改,您可以对处理程序执行相同的操作。创建MyWebHookHandler.cs,只需复制并粘贴代码,将GenericJson更改为MyWebHook,就可以了。

答案 1 :(得分:0)

好工作!如果还有其他人关注,则仅缺少几件事是Web.config条目

<add key="MS_WebHookReceiverSecret_GenericJson" value="i=xxx,z=xxx" /> 

成为:

<add key="MS_WebHookReceiverSecret_MyWebhook" value="i=xxx,z=xxx" /> 

因为键名的最后一部分需要与最后一部分中新命名约定的名称匹配。

处理程序本身-在构造函数上:

应该是:

public MyWebhookHandler() 
{ 
    this.Receiver = GenericJsonWebHookReceiver.ReceiverName; 
} 

不是:

public MyWebhookHandler() 
{ 
    this.Receiver = "genericjson"; 
}

而且-执行HttpConfigurationExtensions.cs步骤没有任何好处。

但是,您确实需要执行GenericJsonWebHookReceiver.cs步骤才能使该过程正常工作。