mirror of
https://github.com/nsnail/Ocelot.git
synced 2025-06-19 08:18:16 +08:00
Feature/expose http handlers (#224)
* temp commit * trying to work out how to expose the http handlers in a decent way.. * pissing about at lunch * changed to func so you can instanciate object or new it up each time * docs for dele handlers * upgraded to sdk 2.1.4 * some validation for consul services
This commit is contained in:
43
docs/features/delegatinghandlers.rst
Normal file
43
docs/features/delegatinghandlers.rst
Normal file
@ -0,0 +1,43 @@
|
||||
Delegating Handers
|
||||
==================
|
||||
|
||||
Ocelot allows the user to add delegating handlers to the HttpClient transport. This feature was requested `GitHub #208 <https://github.com/TomPallister/Ocelot/issues/208>`_ and I decided that it was going to be useful in various ways.
|
||||
|
||||
Usage
|
||||
^^^^^^
|
||||
|
||||
In order to add delegating handlers to the HttpClient transport you need to do the following.
|
||||
|
||||
.. code-block:: csharp
|
||||
|
||||
services.AddOcelot()
|
||||
.AddDelegatingHandler(() => new FakeHandler())
|
||||
.AddDelegatingHandler(() => new FakeHandler());
|
||||
|
||||
Or for singleton like behaviour..
|
||||
|
||||
.. code-block:: csharp
|
||||
|
||||
var handlerOne = new FakeHandler();
|
||||
var handlerTwo = new FakeHandler();
|
||||
|
||||
services.AddOcelot()
|
||||
.AddDelegatingHandler(() => handlerOne)
|
||||
.AddDelegatingHandler(() => handlerTwo);
|
||||
|
||||
You can have as many DelegatingHandlers as you want and they are run in a first in first out order. If you are using Ocelot's QoS functionality then that will always be run after your last delegating handler.
|
||||
|
||||
In order to create a class that can be used a delegating handler it must look as follows
|
||||
|
||||
.. code-block:: csharp
|
||||
|
||||
public class FakeHandler : DelegatingHandler
|
||||
{
|
||||
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
|
||||
{
|
||||
//do stuff and optionally call the base handler..
|
||||
return await base.SendAsync(request, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
Hopefully other people will find this feature useful!
|
@ -33,6 +33,8 @@ Thanks for taking a look at the Ocelot documentation. Please use the left hand n
|
||||
features/requestid
|
||||
features/middlewareinjection
|
||||
features/loadbalancer
|
||||
features/delegatinghandlers
|
||||
|
||||
|
||||
.. toctree::
|
||||
:maxdepth: 2
|
||||
|
Reference in New Issue
Block a user