引入ASP.NET WebHooks接收器-WebHooks变得容易。

ASP.NET Web Hooks Receivers general architecture

There's been a lot of enthusiasm lately about the direction that ASP.NET is going lately, and rightfully so.

最近,人们对ASP.NET的发展方向充满了热情,理所当然的。

However, while ASP.NET 5 is cool and exciting, it's also not yet released (at the time of this writing, Beta 8 is being worked on). There are very cool things happening around ASP.NET 4.6 which is released and ready to go live today. Something else that's VERY cool that I want to explore today is ASP.NET WebHooks, which just came out as a preview and is being actively worked on.

但是,尽管ASP.NET 5酷炫而令人兴奋,但它尚未发布(在撰写本文时,正在开发Beta 8)。 ASP.NET 4.6发生了很多很酷的事情, ASP.NET 4.6已发布并准备在今天投入使用。 我今天想探索的其他非常酷的东西是ASP.NET WebHooks ,它只是作为预览发布的,正在积极研究中。

Just as there's Web Forms, MVC, SignalR, Web API and they are all components within ASP.NET, you can think of Web Hooks as another member of the ASP.NET family of technologies. When you want it, it's there to plug in. If you don't use it, it costs you nothing in weight or runtime.

就像Web窗体,MVC,SignalR,Web API一样,它们都是ASP.NET中的组件,您可以将Web Hooks视为ASP.NET技术家族的另一个成员。 如果需要,可以在其中插入。如果不使用它,则不会增加重量或运行时间。

什么是WebHooks? (What are WebHooks?)

Let's start with the What Are They part of the conversation. WebHooks are a convention. They are HTTP callbacks. Moreover, they are "user-defined HTTP callbacks." You and/or your app signs up for notification when something happens and your URL endpoint will get an HTTP POST when that thing happens. WebHooks can and should be RESTful as well. That means if you have a nice RESTful Web API then adding WebHooks to your application should not only be easy, it should a natural and clean extension.

让我们从对话的“他们是什么”部分开始。 WebHooks是一个约定。 它们是HTTP回调。 而且,它们是“用户定义的HTTP回调” 。 您和/或您的应用程序在发生某事时注册通知,并且URL端点将在该事发生时获得HTTP POST。 WebHooks也可以并且应该是RESTful的。 这意味着,如果您有一个不错的RESTful Web API,则将WebHooks添加到您的应用程序不仅应该很容易,而且应该是自然而干净的扩展。

所以呢? 为什么我们需要一个图书馆? (So what? Why do we need a library for this?)

Technically you don't, of course. You could theoretically implement the WebHooks pattern with an HttpHandler if you felt you had something to prove. You could more reasonably do it with ASP.NET Web API, but the general thinking is that if there's a clear and common pattern for doing something then it should be made easier and codified for correctness.

从技术上讲,您当然不会。 如果您认为有什么要证明的话,理论上可以使用HttpHandler实现WebHooks模式。 您可以更合理地使用ASP.NET Web API来执行此操作,但是一般的想法是,如果存在一种明确且通用的操作模式,则应简化该操作并将其编码以确保正确性。

Even more, since WebHooks is such a common pattern and it's being used by folks like Dropbox, GitHub,MailChimp, PayPal, Pusher, Salesforce, Slack, Stripe, Trello, and WordPress then it'd be nice if ASP.NET came with support for these right out of the box. And it does. Support for receiving all of these and more is included.

更重要的是,由于WebHooks是一种常见的模式,并且被DropboxGitHubMailChimpPayPalPusherSalesforceSlackStripeTrelloWordPress之类的人使用,因此ASP.NET带有支持是很好的对于这些开箱即用。 确实如此。 包括对接收所有这些以及更多内容的支持。

There is also a need for easy ways for your applications to send events as WebHooks. In order to do that you need to manage and store subscriptions, and when the time comes to correctly make the callbacks to the right set of subscribers.

您的应用程序还需要简便的方法将事件作为WebHooks发送。 为此,您需要管理和存储订阅,并在适当时机正确地对正确的订阅者集进行回调。

ASP.NET Web挂钩 (ASP.NET WebHooks)

ASP.NET WebHooks is open source, being actively developed on GitHub and is targeting ASP.NET Web API 2 and ASP.NET MVC 5 today. It helps deal with the administrivia involved in dealing with WebHooks. It was announced on the Microsoft WebDev blog (you should subscribe) a few weeks back.

ASP.NET WebHooks是开源的,正在GitHub上积极开发,并且今天面向ASP.NET Web API 2和ASP.NET MVC 5 。 它有助于处理涉及WebHooks的管理事务。 几周前,它已在Microsoft WebDev博客(您应订阅)上宣布。

There's some great docs already being written but the most interesting bits are in the many examples.

已经编写了一些很棒的文档,但是在许多示例中,最有趣的是。

When you install ASP.NET WebHooks you get a WebHook Handler that is the receiver to accept WebHook requests from services. Using a GitHub WebHook as an example, you can easily make a new project then publish it to Azure WebSites. GitHub WebHooks are required to use SSL for their transport which could be a barrier, but Azure WebSites using the *.azurewebsites.net domain get SSL for free. This will make your first WebHook and testing easier.

当您安装ASP.NET WebHooks时,您将获得一个WebHook处理程序,该处理程序是接受来自服务的WebHook请求的接收者。 以GitHub WebHook为例,您可以轻松地创建一个新项目,然后将其发布到Azure WebSites。 GitHub WebHooks必须使用SSL进行传输,这可能是一个障碍,但是使用* .azurewebsites.net域的Azure WebSite可以免费获得SSL。 这将使您的第一个WebHook和测试更加容易。

A good starter WebHook to try creating is one that gets called when an event happens on GitHub. For example, you might want a notification when someone comments on a GitHub issue as a first step in creating a GitHub bot.

尝试创建的一个很好的入门WebHook是在GitHub上发生事件时调用的。 例如,作为创建GitHub机器人的第一步,当有人评论GitHub问题时,您可能希望收到通知。

The default routing structure is https://<host>/api/webhooks/incoming/<receiver> which you'll put in your GitHub repository's settings, along with a SHA256 hash or some other big secret. The secret is then put in a config setting called MS_WebHookReceiverSecret_GitHub in this example.

默认的路由结构是https:// <host> / api / webhooks / incoming / <receiver>,您将把它与GitHub存储库的设置一起放入SHA256哈希或其他一些大秘密。 然后,在此示例中,将秘密放入名为MS_WebHookReceiverSecret_GitHub的配置设置中。

public class GitHubHandler : WebHookHandler
{
public override Task ExecuteAsync(string receiver, WebHookHandlerContext context)
{
string action = context.Actions.First();
JObject data = context.GetDataOrDefault<JObject>();

return Task.FromResult(true);
}
}

In this tiny example, the "action" string will contain "issues" if someone comments on an issue (meaning it's an event coming from the "issues" source).

在这个小例子中,如果有人对问题发表评论(表示这是来自“问题”来源的事件),则“操作”字符串将包含“问题”。

Once you've been triggered by a WebHook callback, you can decide what to do about it. You might want to simply respond, log something, or start a more sophisticated process. There's even a way to trigger an Azure WebJob with this new extension.

一旦被WebHook回调触发,就可以决定要怎么做。 您可能只想响应,记录某些内容或开始一个更复杂的过程。 甚至还可以通过此新扩展来触发Azure WebJob

更多WebHooks (More WebHooks)

Sending WebHooks is similarly simple and there's already a great post on how to get started here. Finally there's even some skunkworks tooling by Brady Gaster that plugs into Visual Studio 2015 and makes things even easier.

发送WebHooks同样也很简单,并且已经有很多关于如何开始的文章。 最后,甚至还有一些由布雷迪·加斯特(Brady Gaster)编写的skunkworks工具,工具可插入Visual Studio 2015 ,并使事情变得更加轻松。

What a lovely dialog box for making ASP.NET WebHooks even easier!

Go check out ASP.NET Web Hooks and give your feedback in the GitHub issues or directly to Henrik or Brady on Twitter!

去看看ASP.NET Web Hooks,并在GitHub问题中提供您的反馈,或者直接在Twitter上给HenrikBrady

翻译自: https://www.hanselman.com/blog/introducing-aspnet-webhooks-receivers-webhooks-made-easy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值