Pannekoekje
Pannekoekje
CC#
Created by Pannekoekje on 3/13/2024 in #help
Target multiple architectures at once
Hey guys, I have an app I want to publish for linux-x64 and linux-arm64. I'm using the "RunTimeIdentifier" option in the .csproj file, I tried linux-x64;linux-arm64 but this didn't work. Can't really find an answer online, is this not possible?
17 replies
CC#
Created by Pannekoekje on 2/20/2024 in #help
Backgroundworker IsBusy not reporting correct status?
I have this code : https://dotnetfiddle.net/pmiBoz Which does not run in dotnetfiddle somehow, but it writes "False" to console whenever the worker.IsBusy is encountered. Am I doing something wrong?
34 replies
CC#
Created by Pannekoekje on 2/12/2024 in #help
Async await in minimal API
Hey guys, I have a minimal API app which also hosts a client which has a websocket connection. The websocket gets in a disconnect state a lot so I reconnect if I can't get the data I want. However, it seems like the await for the reconnect isn't properly awaited, is there some special async/await mechanics in these minimal API routed calls? Code:
app.MapGet("/account", async Task<Results<Ok<AccountOverview>, NoContent>>
() =>
{
try
{
await _client.GetAccount();
}
catch
{
_client.Reconnect().Wait();
await _client.Authenticate();
await _client.GetAccount();
}
...
}
);
app.MapGet("/account", async Task<Results<Ok<AccountOverview>, NoContent>>
() =>
{
try
{
await _client.GetAccount();
}
catch
{
_client.Reconnect().Wait();
await _client.Authenticate();
await _client.GetAccount();
}
...
}
);
I've tried with both .Wait and await for the reconnect call.
51 replies
CC#
Created by Pannekoekje on 1/4/2024 in #help
✅ How to deal with different JSON types
I have an API endpoint and depending on the query it can return a list of objects or an object as such: Query for multiple
{"option": "1", "response":[{"A": "A"}, {"B": "B"}]}
{"option": "1", "response":[{"A": "A"}, {"B": "B"}]}
Query for single
{"option": "1", "response":{"A": "A"}}
{"option": "1", "response":{"A": "A"}}
For now I'm only allowing queries for multiple but would like to support both.
public class Response
{
public string A { get; set; }
public string B { get; set; }
}

public class Root
{
public string option { get; set; }
public List<Response> response { get; set; }
}
public class Response
{
public string A { get; set; }
public string B { get; set; }
}

public class Root
{
public string option { get; set; }
public List<Response> response { get; set; }
}
Is there a way to support this?
11 replies
CC#
Created by Pannekoekje on 12/31/2023 in #help
Serialize value of property only
Given this code: https://dotnetfiddle.net/C3Pt3C How would I only serialize the value of the "TempType" rather than the whole object?
15 replies
CC#
Created by Pannekoekje on 12/30/2023 in #help
Variable scoping
Considering this code:
BackgroundWorker backgroundWorker = new();


client.Initialize();
await client.Authenticate();
client.AddCallback<TestObject>(ShowOrders);

var testString = "a";
XX test = new()
{
Test = "xx"
};

void ShowOrders(TestObject response)
{
test.Test = "xx";
testString = "b";
}

for (var x = 0; x<9; x++)
{
await client.GetAccount();
await Task.Delay(500);
}

backgroundWorker.RunWorkerAsync(client.Receive());
await Task.Delay(5000);

System.Console.WriteLine(testString);

public class XX {

public string? Test { get; set; }

}
BackgroundWorker backgroundWorker = new();


client.Initialize();
await client.Authenticate();
client.AddCallback<TestObject>(ShowOrders);

var testString = "a";
XX test = new()
{
Test = "xx"
};

void ShowOrders(TestObject response)
{
test.Test = "xx";
testString = "b";
}

for (var x = 0; x<9; x++)
{
await client.GetAccount();
await Task.Delay(500);
}

backgroundWorker.RunWorkerAsync(client.Receive());
await Task.Delay(5000);

System.Console.WriteLine(testString);

public class XX {

public string? Test { get; set; }

}
Trying to compile, within the "ShowOrders" function, the client.AddCallback<TestObject>(ShowOrders); line gives the error : Use of unassigned local variable 'test' Why does it not complain about testString ?
24 replies
CC#
Created by Pannekoekje on 12/29/2023 in #help
JSON deserialization (proper way?)
Hi, I'm writing a client that consumes some JSON from a websocket. The socket can give multiple types of messages over it, what would be the most efficient (fastest) way to deserialize the JSON objects to classes? All the type of messages are known beforehand. The way I'm doing it now is:
var jsonObject = JsonDocument.Parse(inputJson);
IReponseObject returnObject = null;

if (jsonObject.RootElement.TryGetProperty("error", out JsonElement error))
{
throw new Exception($"Issue found in response: \"{error.ToString()}\"");
}

if (jsonObject.RootElement.TryGetProperty("event", out JsonElement jsonEvent))
{
switch (jsonEvent.ToString())
{
case "authenticate":
returnObject = JsonSerializer.Deserialize<AuthenticatedResponse>(jsonObject);
break;
default:
break;
}
}

if (jsonObject.RootElement.TryGetProperty("action", out JsonElement jsonAction))
{
switch (jsonAction.ToString())
{
case "getTime":
responseContent = jsonObject.RootElement.GetProperty("response");
IReponseObject = JsonSerializer.Deserialize<TimeResponse>(responseContent);

break;
case "getPressure":
responseContent = jsonObject.RootElement.GetProperty("response");
IReponseObject = new PressureResponse() { Pressures = new List<Pressure>() };
if (responseContent.ValueKind == JsonValueKind.Object)
{
var pressure = JsonSerializer.Deserialize<PressureResponse>(responseContent);
(IReponseObject as PressureResponse).Pressure.Add(Pressure);
}
else
{
var pressure = JsonSerializer.Deserialize<List<PressureResponse>>(responseContent);
(IReponseObject as PressureResponses).Pressures.AddRange(Pressure);
}
break;
}
var jsonObject = JsonDocument.Parse(inputJson);
IReponseObject returnObject = null;

if (jsonObject.RootElement.TryGetProperty("error", out JsonElement error))
{
throw new Exception($"Issue found in response: \"{error.ToString()}\"");
}

if (jsonObject.RootElement.TryGetProperty("event", out JsonElement jsonEvent))
{
switch (jsonEvent.ToString())
{
case "authenticate":
returnObject = JsonSerializer.Deserialize<AuthenticatedResponse>(jsonObject);
break;
default:
break;
}
}

if (jsonObject.RootElement.TryGetProperty("action", out JsonElement jsonAction))
{
switch (jsonAction.ToString())
{
case "getTime":
responseContent = jsonObject.RootElement.GetProperty("response");
IReponseObject = JsonSerializer.Deserialize<TimeResponse>(responseContent);

break;
case "getPressure":
responseContent = jsonObject.RootElement.GetProperty("response");
IReponseObject = new PressureResponse() { Pressures = new List<Pressure>() };
if (responseContent.ValueKind == JsonValueKind.Object)
{
var pressure = JsonSerializer.Deserialize<PressureResponse>(responseContent);
(IReponseObject as PressureResponse).Pressure.Add(Pressure);
}
else
{
var pressure = JsonSerializer.Deserialize<List<PressureResponse>>(responseContent);
(IReponseObject as PressureResponses).Pressures.AddRange(Pressure);
}
break;
}
16 replies
CC#
Created by Pannekoekje on 12/25/2023 in #help
Casting, reflection or?
I'm trying to write a simple lib for consuming a few JSON API endpoints and processing their contents with callbacks. I'm trying to cast the "parsed" variable to its actual class (as it is known during runtime). Psuedo code as follows:
public interface IType
{
public string Type { get; set; }
}

public class TypeA : IType
{
public string Type { get; set; }
public string FancyA { get; set; }
}

public class TypeB : IType
{
public string Type { get; set; }
public string FancyB { get; set; }
}

void Get(json)
{
var x = GetJson;
IType returnObject = null;

switch(x.GetType)
{
"TypeA":
returnObject = JsonSerializer.Deserialize<TypeA>(x);
break;
"TypeB":
returnObject = JsonSerializer.Deserialize<TypeB>(x);
break;
}
return returnObject;
}

void DoParse()
{

var callbacks = Dictionary<string, Action<IType>> { ("TypeA", DoStuffTypeA) };

while(true)
{
var content = Retrieve();
var parsed = Get(content);

var callback = callbacks[parsed.Type]
callback.Invoke(parsed)
}
}

// Works
void DoStuffTypeA(IType content)
{
//...
}


// Doesn't work
void DoStuffTypeA(TypeA content)
{
//...
}
public interface IType
{
public string Type { get; set; }
}

public class TypeA : IType
{
public string Type { get; set; }
public string FancyA { get; set; }
}

public class TypeB : IType
{
public string Type { get; set; }
public string FancyB { get; set; }
}

void Get(json)
{
var x = GetJson;
IType returnObject = null;

switch(x.GetType)
{
"TypeA":
returnObject = JsonSerializer.Deserialize<TypeA>(x);
break;
"TypeB":
returnObject = JsonSerializer.Deserialize<TypeB>(x);
break;
}
return returnObject;
}

void DoParse()
{

var callbacks = Dictionary<string, Action<IType>> { ("TypeA", DoStuffTypeA) };

while(true)
{
var content = Retrieve();
var parsed = Get(content);

var callback = callbacks[parsed.Type]
callback.Invoke(parsed)
}
}

// Works
void DoStuffTypeA(IType content)
{
//...
}


// Doesn't work
void DoStuffTypeA(TypeA content)
{
//...
}
Is this just impossible or am I implenting this stuff wrong?
73 replies