@@ -15,7 +15,9 @@ namespace GraphQL.Client.Http
1515{
1616 public class GraphQLHttpClient : IGraphQLClient
1717 {
18- private readonly GraphQLHttpWebSocket _graphQlHttpWebSocket ;
18+ private readonly Lazy < GraphQLHttpWebSocket > _lazyHttpWebSocket ;
19+ private GraphQLHttpWebSocket _graphQlHttpWebSocket => _lazyHttpWebSocket . Value ;
20+
1921 private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource ( ) ;
2022 private readonly ConcurrentDictionary < Tuple < GraphQLRequest , Type > , object > _subscriptionStreams = new ConcurrentDictionary < Tuple < GraphQLRequest , Type > , object > ( ) ;
2123
@@ -63,7 +65,7 @@ public GraphQLHttpClient(GraphQLHttpClientOptions options, IGraphQLWebsocketJson
6365 if ( ! HttpClient . DefaultRequestHeaders . UserAgent . Any ( ) )
6466 HttpClient . DefaultRequestHeaders . UserAgent . Add ( new ProductInfoHeaderValue ( GetType ( ) . Assembly . GetName ( ) . Name , GetType ( ) . Assembly . GetName ( ) . Version . ToString ( ) ) ) ;
6567
66- _graphQlHttpWebSocket = new GraphQLHttpWebSocket ( GetWebSocketUri ( ) , this ) ;
68+ _lazyHttpWebSocket = new Lazy < GraphQLHttpWebSocket > ( ( ) => new GraphQLHttpWebSocket ( GetWebSocketUri ( ) , this ) ) ;
6769 }
6870
6971 #endregion
@@ -186,7 +188,8 @@ protected virtual void Dispose(bool disposing)
186188 Debug . WriteLine ( $ "Disposing GraphQLHttpClient on endpoint { Options . EndPoint } ") ;
187189 _cancellationTokenSource . Cancel ( ) ;
188190 HttpClient . Dispose ( ) ;
189- _graphQlHttpWebSocket . Dispose ( ) ;
191+ if ( _lazyHttpWebSocket . IsValueCreated )
192+ _lazyHttpWebSocket . Value . Dispose ( ) ;
190193 _cancellationTokenSource . Dispose ( ) ;
191194 }
192195 }
0 commit comments