VeQox
VeQox
CC#
Created by VeQox on 11/19/2023 in #help
✅ using from another csproj
Currently i have 2 projects, an api app and a worker service app And both use the same models. Is there a way to put the models in a libary project and the 2 other projects include them? Without me needing to write the models twice per project?
10 replies
CC#
Created by VeQox on 11/15/2023 in #help
✅ asp.net background service with database
Im trying to make a background task which has a nats connection listening for incoming messages. When a message is received i want to add it to the database but with DI i cannot inject it. How can i work arround this?
13 replies
CC#
Created by VeQox on 8/2/2023 in #help
❔ Raw Websockets, ref alternative in async programming
public async Task HandleUpgrade()
{
if (!HttpContext.WebSockets.IsWebSocketRequest)
{
Logger.LogInformation("Request not a websocket request");
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
return;
}

var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
var connection = new WebSocketConnection(webSocket);
Logger.LogInformation("Connection established");

try
{
Room? room = null;
Client? client = null;

while (!webSocket.CloseStatus.HasValue)
{
var raw = await connection.ReceiveAsync();

Logger.LogInformation("Received {Message} from connection[{Guid}]", raw, connection.Guid);

if (raw is null) continue;

var (webSocketClientMessage, _) = JsonUtils.Deserialize<WebSocketClientMessage>(raw);
if (webSocketClientMessage is null) continue;

await OnMessage(webSocketClientMessage, raw, connection, room, client);
}
}
finally
{
await connection.CloseAsync();
Logger.LogInformation("Connection closed with connection[{Guid}]", connection.Guid);
}
}
public async Task HandleUpgrade()
{
if (!HttpContext.WebSockets.IsWebSocketRequest)
{
Logger.LogInformation("Request not a websocket request");
HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
return;
}

var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
var connection = new WebSocketConnection(webSocket);
Logger.LogInformation("Connection established");

try
{
Room? room = null;
Client? client = null;

while (!webSocket.CloseStatus.HasValue)
{
var raw = await connection.ReceiveAsync();

Logger.LogInformation("Received {Message} from connection[{Guid}]", raw, connection.Guid);

if (raw is null) continue;

var (webSocketClientMessage, _) = JsonUtils.Deserialize<WebSocketClientMessage>(raw);
if (webSocketClientMessage is null) continue;

await OnMessage(webSocketClientMessage, raw, connection, room, client);
}
}
finally
{
await connection.CloseAsync();
Logger.LogInformation("Connection closed with connection[{Guid}]", connection.Guid);
}
}
12 replies
CC#
Created by VeQox on 7/25/2023 in #help
✅ asp.net BodyReader
var reader = HttpContext.Request.BodyReader;
var rawBody = Encoding.UTF8.GetString((await reader.ReadAsync()).Buffer);
var ((name, capacity, isPublic), error) = JsonUtils.Deserialize<PostRoomRequestBody>(rawBody);
await reader.CompleteAsync();
var reader = HttpContext.Request.BodyReader;
var rawBody = Encoding.UTF8.GetString((await reader.ReadAsync()).Buffer);
var ((name, capacity, isPublic), error) = JsonUtils.Deserialize<PostRoomRequestBody>(rawBody);
await reader.CompleteAsync();
Im trying to read the body (which works) but then it throws an exception
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HMSD270MT6R7", Request id "0HMSD270MT6R7:00000001": An unhandled exception was thrown by the application.
Microsoft.AspNetCore.Connections.ConnectionAbortedException: The connection was aborted by the application.
---> System.InvalidOperationException: Reading is already in progress.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1ContentLengthMessageBody.TryReadInternal(ReadResult& readResult)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.OnConsumeAsync()
--- End of inner exception stack trace ---
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HMSD270MT6R7", Request id "0HMSD270MT6R7:00000001": An unhandled exception was thrown by the application.
Microsoft.AspNetCore.Connections.ConnectionAbortedException: The connection was aborted by the application.
---> System.InvalidOperationException: Reading is already in progress.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1ContentLengthMessageBody.TryReadInternal(ReadResult& readResult)
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.Http1MessageBody.OnConsumeAsync()
--- End of inner exception stack trace ---
after some digging i found out this is a bug? https://github.com/dotnet/runtime/issues/53914 Is there a known workaround for this?
1 replies
CC#
Created by VeQox on 7/23/2023 in #help
❔ ✅ asp.net websockets
Hi, i want to migrate my typescript webserver to c#. I basically have a room-repositoy that has an instance of all open rooms, a room can be created via a rest enpoint (shouldnt be the problem). The room has an id, and is its own websocketserver instance with the no server option. I dont really know how i can explain this further, the http-serverlistened to upgrade request and got corresponding room object from the repository, from the url params like ws://localhost:8080/rooms/{roomID}. The rooms handles the upgrade and the client sends messages to this instance. Is there a similar system like this or do i have to create one endpoint and match every message to the corresponding room. Thanks in advance
9 replies
CC#
Created by VeQox on 12/21/2022 in #help
✅ hi im experimenting with websocketsservers and clients.
Im currently running the server with nodejs and the client in c#. My problem is that the Websocket.ConnectAsync just crashes without an exeption so i dont really know whats wrong. The server is running on port 8080 on localhost.
7 replies
CC#
Created by VeQox on 10/15/2022 in #help
Whats the fastest way to write to the console [Answered]
[StructLayout(LayoutKind.Explicit)]
public struct CHAR_INFO
{
[FieldOffset(0)]
internal char UnicodeChar;
[FieldOffset(0)]
internal char AsciiChar;
FieldOffset(2)] //2 bytes seems to work properly
internal UInt16 Attributes;
}

[StructLayout(LayoutKind.Sequential)]
public struct SMALL_RECT
{
public short Left;
public short Top;
public short Right;
public short Bottom;
}

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteConsoleOutput(
IntPtr hConsoleOutput,
CHAR_INFO[] lpBuffer,
COORD dwBufferSize,
COORD dwBufferCoord,
ref SMALL_RECT lpWriteRegion
);

public static void WriteBuffer(CHAR_INFO[] buffer, short rows, short columns)
{
IntPtr hnd = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT lpBuffer = new SMALL_RECT() { Left = 0, Top = 0, Right = (short)(Console.BufferWidth - 1), Bottom = (short) (Console.BufferHeight - 1) };
if (hnd != INVALID_HANDLE_VALUE)
{
WriteConsoleOutput(hnd, buffer, new COORD() { X = columns, Y = rows }, new COORD() { X = 0, Y = 0 }, ref lpBuffer);
}
}
[StructLayout(LayoutKind.Explicit)]
public struct CHAR_INFO
{
[FieldOffset(0)]
internal char UnicodeChar;
[FieldOffset(0)]
internal char AsciiChar;
FieldOffset(2)] //2 bytes seems to work properly
internal UInt16 Attributes;
}

[StructLayout(LayoutKind.Sequential)]
public struct SMALL_RECT
{
public short Left;
public short Top;
public short Right;
public short Bottom;
}

[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool WriteConsoleOutput(
IntPtr hConsoleOutput,
CHAR_INFO[] lpBuffer,
COORD dwBufferSize,
COORD dwBufferCoord,
ref SMALL_RECT lpWriteRegion
);

public static void WriteBuffer(CHAR_INFO[] buffer, short rows, short columns)
{
IntPtr hnd = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT lpBuffer = new SMALL_RECT() { Left = 0, Top = 0, Right = (short)(Console.BufferWidth - 1), Bottom = (short) (Console.BufferHeight - 1) };
if (hnd != INVALID_HANDLE_VALUE)
{
WriteConsoleOutput(hnd, buffer, new COORD() { X = columns, Y = rows }, new COORD() { X = 0, Y = 0 }, ref lpBuffer);
}
}
This is what i currently have and it still caps at arround 30fps with 283x480 characters Now it caps and i cant figure out a more efficient way of doing this OhNoOhNoOhNo
74 replies
CC#
Created by VeQox on 9/20/2022 in #help
yield return out of a thread
Is it possible to yield return a value out of a thread, ik a thread only takes void functions, but with () => i can avoid that.
41 replies
CC#
Created by VeQox on 9/14/2022 in #help
await threads to finish their tasks [Answered]
I know this isnt really what threads should be used for, but ye... Hmm , Basically im copying files mulitithreaded and want to await (doesnt need to be async await would be cool tho) so i can finish the task with some logs etc
35 replies
CC#
Created by VeQox on 8/24/2022 in #help
Outlook Interopt
Unhandled exception. System.IO.FileNotFoundException: Could not load file or assembly 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c'. The system cannot find the file specified. File name: 'office, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' at Program.<Main>$(String[] args)
4 replies
CC#
Created by VeQox on 8/20/2022 in #help
UnauthorizedAccessException [Answered]
So im trying to change a folders icon, setting it manually sometimes removes it again 🤷‍♂️ , so i hope this works for the time beeing.
60 replies