Skip to content

Commit 0b49bfc

Browse files
authored
Merge pull request #335 from graphql-dotnet/fix-disposal-of-httpclient
Fix disposal of HttpClient
2 parents dd418d1 + c9bd666 commit 0b49bfc

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

dotnet-tools.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
]
99
},
1010
"gitversion.tool": {
11-
"version": "5.2.4",
11+
"version": "5.6.7",
1212
"commands": [
1313
"dotnet-gitversion"
1414
]

src/GraphQL.Client/GraphQLHttpClient.cs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@ namespace GraphQL.Client.Http
1616
public class GraphQLHttpClient : IGraphQLClient
1717
{
1818
private readonly Lazy<GraphQLHttpWebSocket> _lazyHttpWebSocket;
19-
private GraphQLHttpWebSocket _graphQlHttpWebSocket => _lazyHttpWebSocket.Value;
19+
private GraphQLHttpWebSocket GraphQlHttpWebSocket => _lazyHttpWebSocket.Value;
2020

2121
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
2222
private readonly ConcurrentDictionary<Tuple<GraphQLRequest, Type>, object> _subscriptionStreams = new ConcurrentDictionary<Tuple<GraphQLRequest, Type>, object>();
2323

24+
private readonly bool _disposeHttpClient = false;
25+
2426
/// <summary>
2527
/// the json serializer
2628
/// </summary>
@@ -39,12 +41,12 @@ public class GraphQLHttpClient : IGraphQLClient
3941
/// <summary>
4042
/// Publishes all exceptions which occur inside the websocket receive stream (i.e. for logging purposes)
4143
/// </summary>
42-
public IObservable<Exception> WebSocketReceiveErrors => _graphQlHttpWebSocket.ReceiveErrors;
44+
public IObservable<Exception> WebSocketReceiveErrors => GraphQlHttpWebSocket.ReceiveErrors;
4345

4446
/// <summary>
4547
/// the websocket connection state
4648
/// </summary>
47-
public IObservable<GraphQLWebsocketConnectionState> WebsocketConnectionState => _graphQlHttpWebSocket.ConnectionState;
49+
public IObservable<GraphQLWebsocketConnectionState> WebsocketConnectionState => GraphQlHttpWebSocket.ConnectionState;
4850

4951
#region Constructors
5052

@@ -54,7 +56,12 @@ public GraphQLHttpClient(Uri endPoint, IGraphQLWebsocketJsonSerializer serialize
5456

5557
public GraphQLHttpClient(Action<GraphQLHttpClientOptions> configure, IGraphQLWebsocketJsonSerializer serializer) : this(configure.New(), serializer) { }
5658

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+
}
5865

5966
public GraphQLHttpClient(GraphQLHttpClientOptions options, IGraphQLWebsocketJsonSerializer serializer, HttpClient httpClient)
6067
{
@@ -78,7 +85,7 @@ public async Task<GraphQLResponse<TResponse>> SendQueryAsync<TResponse>(GraphQLR
7885
if (Options.UseWebSocketForQueriesAndMutations ||
7986
!(Options.WebSocketEndPoint is null) && Options.EndPoint is null ||
8087
Options.EndPoint.HasWebSocketScheme())
81-
return await _graphQlHttpWebSocket.SendRequest<TResponse>(request, cancellationToken);
88+
return await GraphQlHttpWebSocket.SendRequest<TResponse>(request, cancellationToken);
8289

8390
return await SendHttpRequestAsync<TResponse>(request, cancellationToken);
8491
}
@@ -99,7 +106,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
99106
if (_subscriptionStreams.ContainsKey(key))
100107
return (IObservable<GraphQLResponse<TResponse>>)_subscriptionStreams[key];
101108

102-
var observable = _graphQlHttpWebSocket.CreateSubscriptionStream<TResponse>(request);
109+
var observable = GraphQlHttpWebSocket.CreateSubscriptionStream<TResponse>(request);
103110

104111
_subscriptionStreams.TryAdd(key, observable);
105112
return observable;
@@ -116,7 +123,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
116123
if (_subscriptionStreams.ContainsKey(key))
117124
return (IObservable<GraphQLResponse<TResponse>>)_subscriptionStreams[key];
118125

119-
var observable = _graphQlHttpWebSocket.CreateSubscriptionStream<TResponse>(request, exceptionHandler);
126+
var observable = GraphQlHttpWebSocket.CreateSubscriptionStream<TResponse>(request, exceptionHandler);
120127
_subscriptionStreams.TryAdd(key, observable);
121128
return observable;
122129
}
@@ -127,7 +134,7 @@ public IObservable<GraphQLResponse<TResponse>> CreateSubscriptionStream<TRespons
127134
/// explicitly opens the websocket connection. Will be closed again on disposing the last subscription
128135
/// </summary>
129136
/// <returns></returns>
130-
public Task InitializeWebsocketConnection() => _graphQlHttpWebSocket.InitializeWebSocket();
137+
public Task InitializeWebsocketConnection() => GraphQlHttpWebSocket.InitializeWebSocket();
131138

132139
#region Private Methods
133140

@@ -195,7 +202,8 @@ protected virtual void Dispose(bool disposing)
195202
{
196203
Debug.WriteLine($"Disposing GraphQLHttpClient on endpoint {Options.EndPoint}");
197204
_cancellationTokenSource.Cancel();
198-
HttpClient.Dispose();
205+
if(_disposeHttpClient)
206+
HttpClient.Dispose();
199207
if ( _lazyHttpWebSocket.IsValueCreated )
200208
_lazyHttpWebSocket.Value.Dispose();
201209
_cancellationTokenSource.Dispose();

src/src.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Update="GitVersionTask" Version="5.3.4">
11+
<PackageReference Update="GitVersionTask" Version="5.6.7">
1212
<PrivateAssets>all</PrivateAssets>
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>

0 commit comments

Comments
 (0)