Skip to content

Commit 8233cd4

Browse files
Update v6.4.5
- Fixed issue with MTConnectHttpClientStream that caused an exception to be thrown when using multiple MTConnectClient connections. Moved the _httpClient variable from being static to be a class member and now requires the MTConnectHttpClientStream class to be disposed.
1 parent 48e861a commit 8233cd4

File tree

3 files changed

+32
-27
lines changed

3 files changed

+32
-27
lines changed

build/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System.Reflection;
22

3-
[assembly: AssemblyVersion("6.4.4")]
4-
[assembly: AssemblyFileVersion("6.4.4")]
3+
[assembly: AssemblyVersion("6.4.5")]
4+
[assembly: AssemblyFileVersion("6.4.5")]
55
[assembly: AssemblyCompany("TrakHound Inc.")]
66
[assembly: AssemblyCopyright("Copyright (c) 2024 TrakHound Inc., All Rights Reserved.")]

libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClient.cs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -717,23 +717,25 @@ private async Task Worker()
717717
if (CurrentOnly) url = CreateCurrentUrl(Authority, Device, Interval, _streamPath);
718718

719719
// Create and Start the Stream
720-
_stream = new MTConnectHttpClientStream(url, DocumentFormat);
721-
_stream.Timeout = Heartbeat * 3;
722-
_stream.ContentEncodings = ContentEncodings;
723-
_stream.ContentType = ContentType;
724-
_stream.Starting += (s, o) => StreamStarting?.Invoke(this, url);
725-
_stream.Started += (s, o) => StreamStarted?.Invoke(this, url);
726-
_stream.Stopping += (s, o) => StreamStopping?.Invoke(this, url);
727-
_stream.Stopped += (s, o) => StreamStopped?.Invoke(this, url);
728-
_stream.DocumentReceived += (s, doc) => ProcessSampleDocument(doc, _stop.Token);
729-
_stream.ErrorReceived += (s, doc) => ProcessSampleError(doc);
730-
_stream.FormatError += (s, r) => FormatError?.Invoke(this, r);
731-
_stream.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex);
732-
_stream.InternalError += (s, ex) => InternalError?.Invoke(this, ex);
733-
734-
// Run Stream (Blocking call)
735-
await _stream.Run(_stop.Token);
736-
720+
using (_stream = new MTConnectHttpClientStream(url, DocumentFormat))
721+
{
722+
_stream.Timeout = Heartbeat * 3;
723+
_stream.ContentEncodings = ContentEncodings;
724+
_stream.ContentType = ContentType;
725+
_stream.Starting += (s, o) => StreamStarting?.Invoke(this, url);
726+
_stream.Started += (s, o) => StreamStarted?.Invoke(this, url);
727+
_stream.Stopping += (s, o) => StreamStopping?.Invoke(this, url);
728+
_stream.Stopped += (s, o) => StreamStopped?.Invoke(this, url);
729+
_stream.DocumentReceived += (s, doc) => ProcessSampleDocument(doc, _stop.Token);
730+
_stream.ErrorReceived += (s, doc) => ProcessSampleError(doc);
731+
_stream.FormatError += (s, r) => FormatError?.Invoke(this, r);
732+
_stream.ConnectionError += (s, ex) => ConnectionError?.Invoke(this, ex);
733+
_stream.InternalError += (s, ex) => InternalError?.Invoke(this, ex);
734+
735+
// Run Stream (Blocking call)
736+
await _stream.Run(_stop.Token);
737+
}
738+
737739
initialRequest = false;
738740

739741
if (!_stop.Token.IsCancellationRequested)

libraries/MTConnect.NET-HTTP/Clients/MTConnectHttpClientStream.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,19 @@ namespace MTConnect.Clients
1818
/// <summary>
1919
/// An Http Stream for reading MTConnect Sample or Current streams and returns MTConnectStreamsResponse documents
2020
/// </summary>
21-
public class MTConnectHttpClientStream
21+
public class MTConnectHttpClientStream : IDisposable
2222
{
2323
private const int DefaultTimeout = 300000;
2424
private const byte LineFeed = 10;
2525
private const byte CarriageReturn = 13;
2626
private const byte Dash = 45;
27-
private static readonly HttpClient _httpClient;
2827
private static readonly byte[] _trimBytes = new byte[] { 10, 13 };
28+
private readonly HttpClient _httpClient;
2929

3030
private CancellationTokenSource _stop;
3131
private string _documentFormat = DocumentFormat.XML;
3232

3333

34-
static MTConnectHttpClientStream()
35-
{
36-
_httpClient = new HttpClient();
37-
_httpClient.Timeout = TimeSpan.FromMilliseconds(DefaultTimeout);
38-
}
39-
4034
public MTConnectHttpClientStream(string url, string documentFormat = DocumentFormat.XML)
4135
{
4236
Id = Guid.NewGuid().ToString();
@@ -45,6 +39,14 @@ public MTConnectHttpClientStream(string url, string documentFormat = DocumentFor
4539
_documentFormat = documentFormat;
4640
ContentEncodings = HttpContentEncodings.DefaultAccept;
4741
ContentType = MimeTypes.Get(documentFormat);
42+
43+
_httpClient = new HttpClient();
44+
_httpClient.Timeout = TimeSpan.FromMilliseconds(DefaultTimeout);
45+
}
46+
47+
public void Dispose()
48+
{
49+
if (_httpClient != null) _httpClient.Dispose();
4850
}
4951

5052

@@ -158,6 +160,7 @@ public async Task Run(CancellationToken cancellationToken)
158160
httpRequest.RequestUri = new Uri(Url);
159161
httpRequest.Method = HttpMethod.Get;
160162

163+
161164
using (var response = await _httpClient.SendAsync(httpRequest, HttpCompletionOption.ResponseHeadersRead, stop.Token))
162165
#if NET5_0_OR_GREATER
163166
using (var stream = await response.Content.ReadAsStreamAsync(stop.Token))

0 commit comments

Comments
 (0)