@@ -343,7 +343,7 @@ public InfluxDBClient(ClientConfig config)
343
343
config . Validate ( ) ;
344
344
345
345
_config = config ;
346
- _httpClient = CreateAndConfigureHttpClient ( _config ) ;
346
+ _httpClient = CreateOrGetHttpClient ( _config ) ;
347
347
FlightSqlClient = new FlightSqlClient ( config : _config , httpClient : _httpClient ) ;
348
348
_restClient = new RestClient ( config : _config , httpClient : _httpClient ) ;
349
349
_gzipHandler = new GzipHandler ( config . WriteOptions != null ? config . WriteOptions . GzipThreshold : 0 ) ;
@@ -840,7 +840,12 @@ await _restClient
840
840
841
841
public void Dispose ( )
842
842
{
843
- _httpClient . Dispose ( ) ;
843
+ // _config.HttpClient == null means HttpClient is created by the library, not from the user.
844
+ // so the client will be responsible for disposing of the HttpClient.
845
+ if ( _config . HttpClient == null )
846
+ {
847
+ _httpClient . Dispose ( ) ;
848
+ }
844
849
FlightSqlClient . Dispose ( ) ;
845
850
_disposed = true ;
846
851
}
@@ -876,7 +881,28 @@ private static StringBuilder ToLineProtocolBody(IEnumerable<object?> data, Write
876
881
return sb ;
877
882
}
878
883
879
- internal static HttpClient CreateAndConfigureHttpClient ( ClientConfig config )
884
+ internal static HttpClient CreateOrGetHttpClient ( ClientConfig config )
885
+ {
886
+ var httpClient = config . HttpClient ;
887
+ if ( httpClient == null )
888
+ {
889
+ httpClient = CreateHttpClient ( config ) ;
890
+ }
891
+
892
+ if ( httpClient . BaseAddress == null )
893
+ {
894
+ httpClient . BaseAddress = new Uri ( config . Host ) ;
895
+ }
896
+
897
+ if ( ! string . IsNullOrEmpty ( config . Token ) )
898
+ {
899
+ _setAuthenticationHeader ( httpClient , config ) ;
900
+ }
901
+
902
+ return httpClient ;
903
+ }
904
+
905
+ private static HttpClient CreateHttpClient ( ClientConfig config )
880
906
{
881
907
var handler = new HttpClientHandler ( ) ;
882
908
if ( handler . SupportsRedirectConfiguration )
@@ -916,17 +942,17 @@ internal static HttpClient CreateAndConfigureHttpClient(ClientConfig config)
916
942
{
917
943
Timeout = config . Timeout
918
944
} ;
919
-
920
945
client . DefaultRequestHeaders . UserAgent . ParseAdd ( AssemblyHelper . GetUserAgent ( ) ) ;
921
- if ( ! string . IsNullOrEmpty ( config . Token ) )
922
- {
923
- string authScheme = string . IsNullOrEmpty ( config . AuthScheme ) ? "Token" : config . AuthScheme ! ;
924
- client . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( authScheme , config . Token ) ;
925
- }
926
946
927
947
return client ;
928
948
}
929
949
950
+ private static void _setAuthenticationHeader ( HttpClient httpClient , ClientConfig config )
951
+ {
952
+ var authScheme = string . IsNullOrEmpty ( config . AuthScheme ) ? "Token" : config . AuthScheme ! ;
953
+ httpClient . DefaultRequestHeaders . Authorization = new AuthenticationHeaderValue ( authScheme , config . Token ) ;
954
+ }
955
+
930
956
private static string OptionMessage ( string property )
931
957
{
932
958
return $ "Please specify the '{ property } ' as a method parameter or use default configuration " +
0 commit comments