Delegating Handlers from container (#266)

* #259 quickly hacked poc for this together, needs tidying up maybe

* #266 +semver: breaking removed adding delegating handler funcs directly...i feel from container is enough
This commit is contained in:
Tom Pallister
2018-03-09 07:37:37 +00:00
committed by GitHub
parent 28cc519341
commit a31a3ae0fc
21 changed files with 975 additions and 456 deletions

View File

@ -4,28 +4,21 @@ 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..
This will register the Handlers as singletons. Because Ocelot caches the HttpClient for the downstream services to avoid
socket exhaustion (well known http client issue) you can only register singleton handlers.
.. code-block:: csharp
var handlerOne = new FakeHandler();
var handlerTwo = new FakeHandler();
services.AddOcelot()
.AddDelegatingHandler(() => handlerOne)
.AddDelegatingHandler(() => handlerTwo);
.AddDelegatingHandler<FakeHandler>()
.AddDelegatingHandler<FakeHandlerTwo>()
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.
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. If you are also registering handlers in DI these will be
run first.
In order to create a class that can be used a delegating handler it must look as follows