Determinism
Determinism
CC#
Created by Determinism on 9/19/2023 in #help
❔ Is it possible to serialize this struct?
got it
12 replies
CC#
Created by Determinism on 9/19/2023 in #help
❔ Is it possible to serialize this struct?
What if I wanted to make all of the fields readonly? How can I deserialize this?
namespace MillTracking.Shared.Models.Telegrams;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public readonly record struct EndOfBarTelegram
{
public static readonly string Key = nameof(EndOfBarTelegram);

public readonly uint MessageLength;
public readonly uint MessageId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
public readonly string ProfileType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]
public readonly string ProfileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public readonly string BloomId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public readonly string TimeIn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public readonly string TimeOut;
public readonly float BarLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
public readonly float[] Mean;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
public readonly float[] Max;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
public readonly float[] Min;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
public readonly float[] DefectLength;
}
namespace MillTracking.Shared.Models.Telegrams;

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
public readonly record struct EndOfBarTelegram
{
public static readonly string Key = nameof(EndOfBarTelegram);

public readonly uint MessageLength;
public readonly uint MessageId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 2)]
public readonly string ProfileType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 30)]
public readonly string ProfileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 20)]
public readonly string BloomId;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public readonly string TimeIn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 14)]
public readonly string TimeOut;
public readonly float BarLength;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
public readonly float[] Mean;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
public readonly float[] Max;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
public readonly float[] Min;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 50)]
public readonly float[] DefectLength;
}
public static T? DecodeJson<T>(ReadOnlyMemory<byte> buffer, JsonSerializerOptions? jsonOptions = null)
{
try
{
JsonSerializerOptions options = jsonOptions ?? defaultJsonOptions;
T val = JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(buffer.Span), options);
return (val != null) ? val : default(T);
}
catch (Exception)
{
return default(T);
}
}
public static T? DecodeJson<T>(ReadOnlyMemory<byte> buffer, JsonSerializerOptions? jsonOptions = null)
{
try
{
JsonSerializerOptions options = jsonOptions ?? defaultJsonOptions;
T val = JsonSerializer.Deserialize<T>(Encoding.UTF8.GetString(buffer.Span), options);
return (val != null) ? val : default(T);
}
catch (Exception)
{
return default(T);
}
}
12 replies
CC#
Created by Determinism on 9/19/2023 in #help
❔ Is it possible to serialize this struct?
That works thanks
12 replies
CC#
Created by Determinism on 9/19/2023 in #help
❔ Is it possible to serialize this struct?
I can't turn them into properties because of marshalas
12 replies
CC#
Created by Determinism on 1/18/2023 in #help
❔ Generic factory with dependency injection
Is fine
6 replies
CC#
Created by Determinism on 1/18/2023 in #help
❔ Generic factory with dependency injection
I guess I'm really asking if the way it is
6 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
Np, thanks for looking over it
151 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
Why would it do that? the id is outside the loop
151 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
151 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
It's a different BackgroundService
151 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
Which implements BackgroundService
151 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
Well I only use it in that one class
151 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
Okay, thanks, so it should be fine for the ConcurrentDictionary as well then
151 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
The GetStream on every loop was a mistype
151 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
It actually is in a backgroundservice
151 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
151 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
But I can't because I need to avoid new
151 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
That's why I would want to do this
private async ValueTask WriteDowntimeMessageToStandController(ReadOnlySequence<byte> buffer,
CancellationToken cancellationToken)
{
if (!_tcpClient.Connected)
{
try
{
_tcpClient = new();
await _tcpClient.ConnectAsync(_connectionsOptions.StandControllerIp, _connectionsOptions.DataCollectorPort, cancellationToken); // reusing this port
_logger.LogInformation("Connected to the stand controller on {_standControllerIp}:{_standControllerPort}",
_connectionsOptions.StandControllerIp, _connectionsOptions.StandControllerPort);
}
catch (Exception ex)
{
_logger.LogError(ex, "WriteDowntimeMessageToStandController-Exception");
}
}

try
{
foreach (var segment in buffer)
{
var stream = _tcpClient.GetStream();

await stream.WriteAsync(segment, cancellationToken);
}
}
catch (Exception ex)
{
_logger.LogError(ex,
"Error sending downtime to the stand controller at {_standControllerIp}.",
_connectionsOptions.StandControllerIp);

_tcpClient.Close();
}
}
private async ValueTask WriteDowntimeMessageToStandController(ReadOnlySequence<byte> buffer,
CancellationToken cancellationToken)
{
if (!_tcpClient.Connected)
{
try
{
_tcpClient = new();
await _tcpClient.ConnectAsync(_connectionsOptions.StandControllerIp, _connectionsOptions.DataCollectorPort, cancellationToken); // reusing this port
_logger.LogInformation("Connected to the stand controller on {_standControllerIp}:{_standControllerPort}",
_connectionsOptions.StandControllerIp, _connectionsOptions.StandControllerPort);
}
catch (Exception ex)
{
_logger.LogError(ex, "WriteDowntimeMessageToStandController-Exception");
}
}

try
{
foreach (var segment in buffer)
{
var stream = _tcpClient.GetStream();

await stream.WriteAsync(segment, cancellationToken);
}
}
catch (Exception ex)
{
_logger.LogError(ex,
"Error sending downtime to the stand controller at {_standControllerIp}.",
_connectionsOptions.StandControllerIp);

_tcpClient.Close();
}
}
151 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
I'm just initializing the TcpClient in the constructor
151 replies
CC#
Created by Determinism on 1/17/2023 in #help
Avoiding 'new' in IOC
That method is the only one that uses the TcpClient
151 replies