@@ -16,11 +16,13 @@ namespace GraphQL.Client.Http
16
16
public class GraphQLHttpClient : IGraphQLClient
17
17
{
18
18
private readonly Lazy < GraphQLHttpWebSocket > _lazyHttpWebSocket ;
19
- private GraphQLHttpWebSocket _graphQlHttpWebSocket => _lazyHttpWebSocket . Value ;
19
+ private GraphQLHttpWebSocket GraphQlHttpWebSocket => _lazyHttpWebSocket . Value ;
20
20
21
21
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource ( ) ;
22
22
private readonly ConcurrentDictionary < Tuple < GraphQLRequest , Type > , object > _subscriptionStreams = new ConcurrentDictionary < Tuple < GraphQLRequest , Type > , object > ( ) ;
23
23
24
+ private readonly bool _disposeHttpClient = false ;
25
+
24
26
/// <summary>
25
27
/// the json serializer
26
28
/// </summary>
@@ -39,12 +41,12 @@ public class GraphQLHttpClient : IGraphQLClient
39
41
/// <summary>
40
42
/// Publishes all exceptions which occur inside the websocket receive stream (i.e. for logging purposes)
41
43
/// </summary>
42
- public IObservable < Exception > WebSocketReceiveErrors => _graphQlHttpWebSocket . ReceiveErrors ;
44
+ public IObservable < Exception > WebSocketReceiveErrors => GraphQlHttpWebSocket . ReceiveErrors ;
43
45
44
46
/// <summary>
45
47
/// the websocket connection state
46
48
/// </summary>
47
- public IObservable < GraphQLWebsocketConnectionState > WebsocketConnectionState => _graphQlHttpWebSocket . ConnectionState ;
49
+ public IObservable < GraphQLWebsocketConnectionState > WebsocketConnectionState => GraphQlHttpWebSocket . ConnectionState ;
48
50
49
51
#region Constructors
50
52
@@ -54,7 +56,12 @@ public GraphQLHttpClient(Uri endPoint, IGraphQLWebsocketJsonSerializer serialize
54
56
55
57
public GraphQLHttpClient ( Action < GraphQLHttpClientOptions > configure , IGraphQLWebsocketJsonSerializer serializer ) : this ( configure . New ( ) , serializer ) { }
56
58
57
- public GraphQLHttpClient ( GraphQLHttpClientOptions options , IGraphQLWebsocketJsonSerializer serializer ) : this ( options , serializer , new HttpClient ( options . HttpMessageHandler ) ) { }
59
+ public GraphQLHttpClient ( GraphQLHttpClientOptions options , IGraphQLWebsocketJsonSerializer serializer ) : this (
60
+ options , serializer , new HttpClient ( options . HttpMessageHandler ) )
61
+ {
62
+ // set this flag to dispose the internally created HttpClient when GraphQLHttpClient gets disposed
63
+ _disposeHttpClient = true ;
64
+ }
58
65
59
66
public GraphQLHttpClient ( GraphQLHttpClientOptions options , IGraphQLWebsocketJsonSerializer serializer , HttpClient httpClient )
60
67
{
@@ -78,7 +85,7 @@ public async Task<GraphQLResponse<TResponse>> SendQueryAsync<TResponse>(GraphQLR
78
85
if ( Options . UseWebSocketForQueriesAndMutations ||
79
86
! ( Options . WebSocketEndPoint is null ) && Options . EndPoint is null ||
80
87
Options . EndPoint . HasWebSocketScheme ( ) )
81
- return await _graphQlHttpWebSocket . SendRequest < TResponse > ( request , cancellationToken ) ;
88
+ return await GraphQlHttpWebSocket . SendRequest < TResponse > ( request , cancellationToken ) ;
82
89
83
90
return await SendHttpRequestAsync < TResponse > ( request , cancellationToken ) ;
84
91
}
@@ -99,7 +106,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
99
106
if ( _subscriptionStreams . ContainsKey ( key ) )
100
107
return ( IObservable < GraphQLResponse < TResponse > > ) _subscriptionStreams [ key ] ;
101
108
102
- var observable = _graphQlHttpWebSocket . CreateSubscriptionStream < TResponse > ( request ) ;
109
+ var observable = GraphQlHttpWebSocket . CreateSubscriptionStream < TResponse > ( request ) ;
103
110
104
111
_subscriptionStreams . TryAdd ( key , observable ) ;
105
112
return observable ;
@@ -116,7 +123,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
116
123
if ( _subscriptionStreams . ContainsKey ( key ) )
117
124
return ( IObservable < GraphQLResponse < TResponse > > ) _subscriptionStreams [ key ] ;
118
125
119
- var observable = _graphQlHttpWebSocket . CreateSubscriptionStream < TResponse > ( request , exceptionHandler ) ;
126
+ var observable = GraphQlHttpWebSocket . CreateSubscriptionStream < TResponse > ( request , exceptionHandler ) ;
120
127
_subscriptionStreams . TryAdd ( key , observable ) ;
121
128
return observable ;
122
129
}
@@ -127,7 +134,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
127
134
/// explicitly opens the websocket connection. Will be closed again on disposing the last subscription
128
135
/// </summary>
129
136
/// <returns></returns>
130
- public Task InitializeWebsocketConnection ( ) => _graphQlHttpWebSocket . InitializeWebSocket ( ) ;
137
+ public Task InitializeWebsocketConnection ( ) => GraphQlHttpWebSocket . InitializeWebSocket ( ) ;
131
138
132
139
#region Private Methods
133
140
@@ -195,7 +202,8 @@ protected virtual void Dispose(bool disposing)
195
202
{
196
203
Debug . WriteLine ( $ "Disposing GraphQLHttpClient on endpoint { Options . EndPoint } ") ;
197
204
_cancellationTokenSource . Cancel ( ) ;
198
- HttpClient . Dispose ( ) ;
205
+ if ( _disposeHttpClient )
206
+ HttpClient . Dispose ( ) ;
199
207
if ( _lazyHttpWebSocket . IsValueCreated )
200
208
_lazyHttpWebSocket . Value . Dispose ( ) ;
201
209
_cancellationTokenSource . Dispose ( ) ;
0 commit comments