xriba
xriba
CC#
Created by xriba on 5/7/2024 in #help
✅ Hosted Service running as Windows Service
Documenting for further reference: 1. Windows Services do not exit/shutdown gracefully and therefore StopAsync is never called. Here is the open issue: https://github.com/dotnet/runtime/issues/83093 2. The reason why StartAsync runs into a Socket exception during system/platform startup is because the method is executed before the network is available. Here's my workaround:
public async Task StartAsync(CancellationToken cancellationToken)
{
NetworkIsAvailable = NetworkInterface.GetIsNetworkAvailable();
NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
var uri = new Uri("wss://echo.websocket.org");

while (!cancellationToken.IsCancellationRequested)
{
if (NetworkIsAvailable)
{
await _webSocket.ConnectAsync(uri, cancellationToken);
Task.Run(ProcessDataAsync, cancellationToken);
break;
}

await Task.Delay(1000);
}
}

private void NetworkChange_NetworkAvailabilityChanged(object? sender, NetworkAvailabilityEventArgs e)
=> NetworkIsAvailable = e.IsAvailable;
public async Task StartAsync(CancellationToken cancellationToken)
{
NetworkIsAvailable = NetworkInterface.GetIsNetworkAvailable();
NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
var uri = new Uri("wss://echo.websocket.org");

while (!cancellationToken.IsCancellationRequested)
{
if (NetworkIsAvailable)
{
await _webSocket.ConnectAsync(uri, cancellationToken);
Task.Run(ProcessDataAsync, cancellationToken);
break;
}

await Task.Delay(1000);
}
}

private void NetworkChange_NetworkAvailabilityChanged(object? sender, NetworkAvailabilityEventArgs e)
=> NetworkIsAvailable = e.IsAvailable;
19 replies
CC#
Created by xriba on 5/7/2024 in #help
✅ Hosted Service running as Windows Service
Update on StartAsync while not the most elegant, if I delay the execution, the service works which leads me to believe it's related to the DNS not being resolved at first but I'm not sure how to properly ensure it's running Not Working
public async Task StartAsync(CancellationToken cancellationToken)
{
var uri = new Uri("wss://echo.websocket.org");
await _webSocket.ConnectAsync(uri, cancellationToken);
Task.Run(ProcessDataAsync, cancellationToken);
}

Exception:
System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server
---> System.Net.Http.HttpRequestException: No such host is known. (echo.websocket.org:443)
---> System.Net.Sockets.SocketException (11001): No such host is known.
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|285_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
public async Task StartAsync(CancellationToken cancellationToken)
{
var uri = new Uri("wss://echo.websocket.org");
await _webSocket.ConnectAsync(uri, cancellationToken);
Task.Run(ProcessDataAsync, cancellationToken);
}

Exception:
System.Net.WebSockets.WebSocketException (0x80004005): Unable to connect to the remote server
---> System.Net.Http.HttpRequestException: No such host is known. (echo.websocket.org:443)
---> System.Net.Sockets.SocketException (11001): No such host is known.
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.ThrowException(SocketError error, CancellationToken cancellationToken)
at System.Net.Sockets.Socket.AwaitableSocketAsyncEventArgs.System.Threading.Tasks.Sources.IValueTaskSource.GetResult(Int16 token)
at System.Net.Sockets.Socket.<ConnectAsync>g__WaitForConnectWithCancellation|285_0(AwaitableSocketAsyncEventArgs saea, ValueTask connectTask, CancellationToken cancellationToken)
at System.Net.Http.HttpConnectionPool.ConnectToTcpHostAsync(String host, Int32 port, HttpRequestMessage initialRequest, Boolean async, CancellationToken cancellationToken)
--- End of inner exception stack trace ---
Working
public async Task StartAsync(CancellationToken cancellationToken)
{
var uri = new Uri("wss://echo.websocket.org");

await Task.Delay(5000, cancellationToken);
await _webSocket.ConnectAsync(uri, cancellationToken);
Task.Run(ProcessDataAsync, cancellationToken);
}
public async Task StartAsync(CancellationToken cancellationToken)
{
var uri = new Uri("wss://echo.websocket.org");

await Task.Delay(5000, cancellationToken);
await _webSocket.ConnectAsync(uri, cancellationToken);
Task.Run(ProcessDataAsync, cancellationToken);
}
19 replies
CC#
Created by xriba on 5/7/2024 in #help
✅ Hosted Service running as Windows Service
I believe it's related to this open issue https://github.com/dotnet/runtime/issues/83093
19 replies
CC#
Created by xriba on 5/7/2024 in #help
✅ Hosted Service running as Windows Service
19 replies
CC#
Created by xriba on 5/7/2024 in #help
✅ Hosted Service running as Windows Service
StopAsync executes when I manually stop the service with ctrl-c. It does not seem to execute when restarting the machine
19 replies
CC#
Created by xriba on 5/7/2024 in #help
✅ Hosted Service running as Windows Service
It does work, I'm using this endpoint as a sample but even this sample works fine when I start/stop the service manually. https://websocket.org/tools/websocket-echo-server/
19 replies
CC#
Created by xriba on 5/7/2024 in #help
✅ Hosted Service running as Windows Service
I'm thinking StartAsync is executing too early while the system is booting up. Maybe I could delay it by polling Dns until it starts resolving or registering an application started event where I connect the websocket client, but it doesn't feel right to me. Basically, I want to connect the websocket client while the underlying platform is starting the service. Also, I want to execute some code and dispose the client when the platform restarts or shuts down.
19 replies
CC#
Created by xriba on 5/24/2023 in #help
❔ EF Core Fluent API - Unidirectional one to many relationship
I see, thank you.
6 replies
CC#
Created by xriba on 5/24/2023 in #help
❔ EF Core Fluent API - Unidirectional one to many relationship
Yes, it could very well be a value object. I'm just wondering how to, if possible, implement this relationship. In the example above, I'd like to generate the relationship in Square and not in Coordinate as Coordinate does not need to know anything about the shape. However if you look at the second image, you can see that there's a foreign key on Coordinate.
6 replies