occluder
occluder
CC#
Created by occluder on 11/3/2024 in #help
✅ Trouble porting to AOT JSON
I'm having some trouble making my json parsing aot-friendly. I have a lot of types to deserialize into, this is what I'm doing right now:
[JsonSerializable(typeof(A))]
[JsonSerializable(typeof(B))]
[JsonSerializable(typeof(C))]
public partial class ExampleSerializationContext : JsonSerializerContext
{
}
[JsonSerializable(typeof(A))]
[JsonSerializable(typeof(B))]
[JsonSerializable(typeof(C))]
public partial class ExampleSerializationContext : JsonSerializerContext
{
}
My issue lies with the way this is handled, there are many types nested within that have conflicting names. e.g:
public class A
{
public class Nested
{ }
}

public class B
{
public class Nested
{ }
}

public class C
{
public class Nested
{ }
}
public class A
{
public class Nested
{ }
}

public class B
{
public class Nested
{ }
}

public class C
{
public class Nested
{ }
}
The serialization context above complains about having multiple types with the name 'Nested'. This nested class is just an example; In my original code, all of the nested classes have different properties/fields Is there an easy way to handle this? I have a lot of types, with nested type names conflicting all over the place, so renaming everything is somewhat impractical. Any advice?
49 replies
CC#
Created by occluder on 10/1/2024 in #help
✅ Is there a reason to still be using EventArgs and EventHandler<T> for events?
I see the usage of both aforementioned types in the msdocs for events. Even in new-ish blogs and packages, I see some people still using this approach. However, I fail to see a point to it. Event declarations can be of any delegate, like Action<T> or Func<T, T>. And EventArgs seems utterly pointless. So, I just want to make sure: Is this just an outdated convention? Or are there secret runtime benefits of doing events this way?
3 replies
CC#
Created by occluder on 12/8/2023 in #help
✅ Blazor's @onclick isn't firing
I have a simple blazor server page with a couple of buttons. All the buttons have the same callback, where I put a Console.WriteLine to check if it does anything, but upon clicking them nothing happens. Anyone able to explain to me what's wrong? The console doesn't show any errors either. I'm using net8
@page "/"
@inject IJSRuntime JSRuntime

<PageTitle>Home</PageTitle>

<div class="grid-container">
@foreach (var x in secrets)
{
<button class="grid-item" @onclick="() => CopySecret(x)" @onclick:preventDefault>
<h3>asdf</h3>
<p>@x</p>
</button>
}

<button class="add-grid-item">
<h4>Add new</h4>
</button>
</div>

@code
{
private string[] secrets = ["xD", "kimne", "gp7xd", "lol", "kimne", "gp7xd", "lol", "kimne", "gp7xd", "lol"];


private async Task CopySecret(string secret)
{
Console.WriteLine("test"); // This is never executed, even if the line below is commented out
await JSRuntime.InvokeVoidAsync("clipboardCopy.copyText", secret);
}
}
@page "/"
@inject IJSRuntime JSRuntime

<PageTitle>Home</PageTitle>

<div class="grid-container">
@foreach (var x in secrets)
{
<button class="grid-item" @onclick="() => CopySecret(x)" @onclick:preventDefault>
<h3>asdf</h3>
<p>@x</p>
</button>
}

<button class="add-grid-item">
<h4>Add new</h4>
</button>
</div>

@code
{
private string[] secrets = ["xD", "kimne", "gp7xd", "lol", "kimne", "gp7xd", "lol", "kimne", "gp7xd", "lol"];


private async Task CopySecret(string secret)
{
Console.WriteLine("test"); // This is never executed, even if the line below is commented out
await JSRuntime.InvokeVoidAsync("clipboardCopy.copyText", secret);
}
}
21 replies
CC#
Created by occluder on 3/29/2023 in #help
Assert.Raises<> in xUnit
2 replies
CC#
Created by occluder on 3/6/2023 in #help
❔ Confusing ClientWebSocket.ReceiveAsync() behavior
Picture the following class:
private ClientWebSocket _ws = new();
private Memory<byte> _buffer = new(new byte[4096]);

public Task ReceiveTask()
{
return Task.Run(async () =>
{
while (true)
{
ValueWebSocketReceiveResult received = await _ws.ReceiveAsync(_buffer, CancellationToken.None);
Memory<byte> message = _buffer[..received.Count];
// do something with message
}
});
}
private ClientWebSocket _ws = new();
private Memory<byte> _buffer = new(new byte[4096]);

public Task ReceiveTask()
{
return Task.Run(async () =>
{
while (true)
{
ValueWebSocketReceiveResult received = await _ws.ReceiveAsync(_buffer, CancellationToken.None);
Memory<byte> message = _buffer[..received.Count];
// do something with message
}
});
}
In this scenario, one would expect that the client can receive up to 4kb of data. However, the behavior I'm encountering is that it's only using the first 2048 bytes of the buffer. With a bit of googling I found out that I'm supposed to use the boolean ValueWebSocketReceiveResult.EndOfMessage to check if the data was split into multiple messages. This made me a little confused, why would it split the data into multiple messages if my buffer is big enough? No matter, I changed my code to the following, hoping I could get around this issue without creating a new buffer:
public Task ReceiveTask()
{
return Task.Run(async () =>
{
while (true)
{
int bufferRegion = 0;
ValueWebSocketReceiveResult received;
while (!(received = await _ws.ReceiveAsync(_buffer[bufferRegion * 2048], CancellationToken.None)).EndOfMessage)
{
bufferRegion++;
continue;
}
Memory<byte> message = _buffer[..received.Count];
// do something with message
}
});
}
public Task ReceiveTask()
{
return Task.Run(async () =>
{
while (true)
{
int bufferRegion = 0;
ValueWebSocketReceiveResult received;
while (!(received = await _ws.ReceiveAsync(_buffer[bufferRegion * 2048], CancellationToken.None)).EndOfMessage)
{
bufferRegion++;
continue;
}
Memory<byte> message = _buffer[..received.Count];
// do something with message
}
});
}
But my efforts were in vain, as each time the inner while loop iterates it overrides the previous result, instead of appending to it. So looking back at all of it, something definitely feels off. Am I using the buffer wrong? Am I supposed to handle split messages differently? Or what's going on here?
3 replies