To avoid socket exhaustion,
you need to pool HttpClient
instances and inject them into FhirClient
. The
default constructor will instantiate its own HttpClient
, which can leak sockets
and cause latency spikes in your app.
You can use IHttpClientFactory
to help with this. See the
documentation
for all the nitty-gritty details.
Note that while this example considers the open source
FhirClient
library, you can use this pattern with anyHttpClient
wrapper that lets you inject theHttpClient
instance.
In your Startup
class:
1 | public class Startup |
Registering Custom Handlers
What if you are using a custom HttpMessageHandler
to do some authorization such
as adding a client credentialed bearer token?
You can still set that up with DI. Just make your custom handler inherit from
DelegatingHandler
so that the AddHttpClient
machinery can reuse the
inner-most HttpMessageHandler
.
1 | public class CustomAuthHandler : DelegatingHandler { |
Now you can configure the HttpClient
instances to be automatically wired up
with this handler.
1 | public class Startup |