Skip to content

Commit 6bfe648

Browse files
author
Henrik Frystyk Nielsen
committed
Added more WebHook docs
1 parent 925e35b commit 6bfe648

9 files changed

+112
-17
lines changed

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#*.jpg binary
4444
#*.png binary
4545
#*.gif binary
46+
*.ico binary
4647

4748
###############################################################################
4849
# diff behavior for common document formats

webhooks/favicon.ico

16.8 KB
Binary file not shown.

webhooks/index.rst

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@
88
ASP.NET WebHooks Documentation
99
==============================
1010

11-
.. include:: ../common/stub-overview.txt
11+
.. include:: /../common/stub-overview.txt
1212

1313
.. toctree::
1414
:titlesonly:
1515

1616
overview
1717
receiving/index
1818

19-
.. include:: ../common/contribute.txt
20-
19+
.. include:: /../common/contribute.txt
2120

webhooks/overview.rst

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
.. include:: /../common/stub-topic.txt
2-
31
Overview of ASP.NET WebHooks
42
============================
53

@@ -69,7 +67,7 @@ from GitHub looks like this as a result of a new issue being opened in a particu
6967
"id": 1,
7068
...
7169
}
72-
}
70+
}
7371

7472
To ensure that the WebHook is indeed from the intended sender, the POST request is secured in some way and then
7573
verified by the receiver. For example, GitHub includes an ‘X-Hub-Signature’ HTTP header with a hash of the request
@@ -106,7 +104,4 @@ The two key concepts here are *Receivers* and *Handlers*:
106104
* Handlers are typicaly where user code runs processing the particular WebHook.
107105

108106
In the following nodes these concepts are described in more details.
109-
110-
.. include:: /../common/stub-notice.txt
111107

112-
.. _issue: https://github.com/aspnet/Docs/issues/113
6.68 KB
Loading

webhooks/receiving/dependencies.rst

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Receiver Dependencies
2+
=====================
3+
4+
Microsoft ASP.NET WebHooks is designed with dependency injection in mind. Most dependencies in the system
5+
can be replaced with alternative implementations using a dependency injection engine.
6+
7+
Please see `DependencyScopeExtensions <https://github.com/aspnet/WebHooks/blob/master/src/Microsoft.AspNet.WebHooks.Receivers/Extensions/DependencyScopeExtensions.cs>`_
8+
for a list of receiver dependencies. If no dependency has been registered, a default implementation is used. Please see `ReceiverServices <https://github.com/aspnet/WebHooks/blob/master/src/Microsoft.AspNet.WebHooks.Receivers/Services/ReceiverServices.cs>`_
9+
for a list of default implementations.

webhooks/receiving/handlers.rst

+94-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,101 @@
33
Processing WebHooks
44
===================
55

6+
Once WebHooks requests has been validate by a WebHook receiver, it is
7+
ready to be processed by user code. This is where *handlers* come in.
8+
Handlers derive from the `IWebHookHandler
9+
<https://github.com/aspnet/WebHooks/blob/master/src/Microsoft.AspNet.Web
10+
Hooks.Receivers/WebHooks/WebHookHandler.cs>`_ interface but typically
11+
uses the `WebHookHandler
12+
<https://github.com/aspnet/WebHooks/blob/master/src/Microsoft.AspNet.Web
13+
Hooks.Receivers/WebHooks/WebHookHandler.cs>`_ class instead of deriving
14+
directly from the interface.
615

16+
A WebHook request can be processed by one or more handlers. Handlers are
17+
called in order based on their respective *Order* property going from
18+
lowest to highest where Order is a simple integer (suggested to be
19+
between 1 and 100):
720

21+
.. image:: _static/Handlers.png
22+
23+
A handler can optionally set the *Response* property on the
24+
WebHookHandlerContext which will lead the processing to stop and the
25+
response to be sent back as the HTTP response to the WebHook. In the case
26+
above, Handler C won’t get called because it has a higher order than B
27+
and B sets the response.
828

9-
.. include:: /../common/stub-notice.txt
29+
Setting the response is typically only relevant for
30+
WebHooks where the response can carry information back to the
31+
originating API. This is for example the case with Slack WebHooks where
32+
the response is posted back to the channel where the WebHook came from.
33+
Handlers can set the Receiver property if they only want to receive
34+
WebHooks from that particular receiver. If they don’t set the receiver
35+
they are called for all of them.
1036

11-
.. _issue: https://github.com/aspnet/Docs/issues/111
37+
One other common use of a response is to use a *410 Gone* response to
38+
indicate that the WebHook no longer is active and no further requests
39+
should be submitted.
40+
41+
By default a handler will be called by all WebHook receivers. However,
42+
if the *Receiver* property is set to the name of a handler then that
43+
handler will only receive WebHook requests from that receiver.
44+
45+
Processing a WebHook
46+
--------------------
47+
48+
When a handler is called, it gets a `WebHookHandlerContext
49+
<https://github.com/aspnet/WebHooks/blob/master/src/Microsoft.AspNet.Web
50+
Hooks.Receivers/WebHooks/WebHookHandlerContext.cs>`_ containing
51+
information about the WebHook request. The data, typically the HTTP
52+
request body, is available from the *Data* property.
53+
54+
The type of the data is typically JSON or HTML form data, but it is
55+
possible to cast to a more specific type if desired. For example, the
56+
custom WebHooks generated by ASP.NET WebHooks can be cast to the type
57+
`CustomNotifications
58+
<https://github.com/aspnet/WebHooks/blob/master/src/Microsoft.AspNet.Web
59+
Hooks.Receivers.Custom/WebHooks/CustomNotifications.cs>`_ as follows::
60+
61+
public class MyWebHookHandler : WebHookHandler
62+
{
63+
public CustomWebHookHandler()
64+
{
65+
this.Receiver = "custom";
66+
}
67+
68+
public override Task ExecuteAsync(string generator, WebHookHandlerContext context)
69+
{
70+
CustomNotifications notifications = context.GetDataOrDefault<CustomNotifications>();
71+
foreach (var notification in notifications.Notifications)
72+
{
73+
...
74+
}
75+
return Task.FromResult(true);
76+
}
77+
}
78+
79+
Queued Processing
80+
-----------------
81+
82+
Most WebHook senders will resend a WebHook if a response is not
83+
generated within a handful of seconds. This means that your handler must
84+
complete the processing within that timeframe in order not for it to be
85+
called again.
86+
87+
If the processing takes longer, or is better handled separately then the
88+
`WebHookQueueHandler
89+
<https://github.com/aspnet/WebHooks/blob/master/src/Microsoft.AspNet.WebHooks.Receivers/WebHooks/WebHookQueueHandler.cs>`_ can be used to submit
90+
the WebHook request to a queue, for example `Azure Storage Queue <https://msdn.microsoft.com/en-us/library/azure/dd179353.aspx>`_.
91+
92+
An outline of a WebHookQueueHandler_ implementation is provided here::
93+
94+
public class QueueHandler : WebHookQueueHandler
95+
{
96+
public override Task EnqueueAsync(WebHookQueueContext context)
97+
{
98+
99+
// Enqueue WebHookQueueContext to your queueing system of choice
100+
101+
return Task.FromResult(true);
102+
}
103+
}

webhooks/receiving/index.rst

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
Receivers
22
---------
33

4+
.. include:: /../common/stub-overview.txt
5+
46
.. toctree::
57
:titlesonly:
68

79
receivers
8-
handlers
10+
handlers
11+
dependencies
12+

webhooks/receiving/receivers.rst

-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
.. include:: /../common/stub-topic.txt
2-
31
WebHook Receivers
42
=================
53

@@ -107,6 +105,3 @@ WebHook Receivers are initialized by registering them, typically in the *WebApiC
107105
}
108106
}
109107

110-
.. include:: /../common/stub-notice.txt
111-
112-
.. _issue: https://github.com/aspnet/Docs/issues/111

0 commit comments

Comments
 (0)