JKorf / Binance.Net

A C# .netstandard client library for the Binance REST and Websocket Spot and Futures API focusing on clear usage and models
https://jkorf.github.io/Binance.Net/
MIT License
1.02k stars 420 forks source link

Set specific HttpClient to use #1385

Open Steffx115 opened 2 weeks ago

Steffx115 commented 2 weeks ago

I am currently trying to read bulk data from BinanceApi into a DB, but cant modify the default httpclient as i assume it would interfere with other libraries using the default client.

Is it possible to set a specific httpclient(factory) to use?

JKorf commented 2 weeks ago

If you're using DI to register the Binance clients (services.AddBinance(..);) then the http client will be internally registered as a typed client like this

services.AddHttpClient<IBinanceRestClient, BinanceRestClient>(options =>
{
    options.Timeout = restOptions.RequestTimeout;
})

If you're not using DI you can pass an http client in the BinanceRestClient constructor:

var client = new BinanceRestClient(httpClientInstance, loggerFactory, options =>{});
Steffx115 commented 1 week ago

Is a different HttpClient used for each request if none is set?

JKorf commented 1 week ago

A single HttpClient is used per client. So if you're re-using the same client the same HttpClient is used, but if you're constructing clients each time then a new HttpClient is used each time