int
int
CC#
Created by int on 1/28/2024 in #help
"Application started" after "Application is shutting down"
backgroundservice fixed it thx
8 replies
CC#
Created by int on 1/28/2024 in #help
"Application started" after "Application is shutting down"
using System.Net.Sockets;
using System.Text;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Kevin;

public class App : IHostedService
{
private ILogger<App> _logger;
private string _host;
private int _port;
private Database _database;
private UdpClient _listener;

public App(
ILogger<App> logger,
Database database,
string host = "localhost",
int port = 5371)
{
_logger = logger;
_host = host;
_port = port;
_database = database;
_listener = new UdpClient(host, port);
}

public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"Listening on port {_host}:{_port}");

while (!cancellationToken.IsCancellationRequested)
{
var receiveTask = _listener.ReceiveAsync();
var cancellationTask = Task.Delay(Timeout.Infinite, cancellationToken);

var completedTask = await Task.WhenAny(receiveTask, cancellationTask);

if (completedTask == receiveTask)
{
var data = await receiveTask;
var message = Encoding.ASCII.GetString(data.Buffer);
HandleMessage(message);
}
else if (completedTask == cancellationTask)
{
break;
}
}
}

public Task StopAsync(CancellationToken cancellationToken)
{
_listener.Close();
return Task.CompletedTask;
}

private void HandleMessage(string message)
{
Console.WriteLine($"Received: {message}");
}
}
using System.Net.Sockets;
using System.Text;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Kevin;

public class App : IHostedService
{
private ILogger<App> _logger;
private string _host;
private int _port;
private Database _database;
private UdpClient _listener;

public App(
ILogger<App> logger,
Database database,
string host = "localhost",
int port = 5371)
{
_logger = logger;
_host = host;
_port = port;
_database = database;
_listener = new UdpClient(host, port);
}

public async Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation($"Listening on port {_host}:{_port}");

while (!cancellationToken.IsCancellationRequested)
{
var receiveTask = _listener.ReceiveAsync();
var cancellationTask = Task.Delay(Timeout.Infinite, cancellationToken);

var completedTask = await Task.WhenAny(receiveTask, cancellationTask);

if (completedTask == receiveTask)
{
var data = await receiveTask;
var message = Encoding.ASCII.GetString(data.Buffer);
HandleMessage(message);
}
else if (completedTask == cancellationTask)
{
break;
}
}
}

public Task StopAsync(CancellationToken cancellationToken)
{
_listener.Close();
return Task.CompletedTask;
}

private void HandleMessage(string message)
{
Console.WriteLine($"Received: {message}");
}
}
8 replies
CC#
Created by int on 8/12/2023 in #help
✅ InitializeComponent not being found, but `dotnet run` works?
update: moved to avalonia, no more error 🦅🦅🦅🦅
13 replies
CC#
Created by int on 8/12/2023 in #help
✅ InitializeComponent not being found, but `dotnet run` works?
i'll have to use VS then
13 replies
CC#
Created by int on 8/12/2023 in #help
✅ InitializeComponent not being found, but `dotnet run` works?
aaaaaaaaahhhhhh
13 replies
CC#
Created by int on 8/12/2023 in #help
✅ InitializeComponent not being found, but `dotnet run` works?
vs is heavy , on my pc
13 replies
CC#
Created by int on 8/12/2023 in #help
✅ InitializeComponent not being found, but `dotnet run` works?
vsc
13 replies
CC#
Created by int on 8/12/2023 in #help
✅ InitializeComponent not being found, but `dotnet run` works?
quite simple
13 replies
CC#
Created by int on 8/12/2023 in #help
✅ InitializeComponent not being found, but `dotnet run` works?
using System.Windows;

namespace McPlug;

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
using System.Windows;

namespace McPlug;

public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}
13 replies
CC#
Created by int on 7/19/2023 in #help
✅ Respond with HTML instead of plaintext
can i set a default for it
8 replies
CC#
Created by int on 7/19/2023 in #help
✅ Respond with HTML instead of plaintext
wouldn't that be tiring to do for each one
8 replies
CC#
Created by int on 7/19/2023 in #help
✅ Respond with HTML instead of plaintext
yeah but most of my app will return HTML responses
8 replies
CC#
Created by int on 6/30/2023 in #help
❔ How to log using hosts?
but you said there was no point of it
15 replies
CC#
Created by int on 6/30/2023 in #help
❔ How to log using hosts?
so you suggest i remove the using?
15 replies
CC#
Created by int on 6/30/2023 in #help
❔ How to log using hosts?
also, another question, what's the use of using var in this code? it works fine without using
15 replies
CC#
Created by int on 6/30/2023 in #help
❔ How to log using hosts?
var logger = host.Services.GetRequiredService<ILogger<Program>>(); works thx
15 replies