C
C#10mo ago
int

"Application started" after "Application is shutting down"

c#
var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole();
builder.Services.AddSingleton<IDatabaseSerializer, EqualsDatabaseSerializer>();
builder.Services.AddSingleton<Database>();
builder.Services.AddHostedService<App>();

var host = builder.Build();
await host.RunAsync();
c#
var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddConsole();
builder.Services.AddSingleton<IDatabaseSerializer, EqualsDatabaseSerializer>();
builder.Services.AddSingleton<Database>();
builder.Services.AddHostedService<App>();

var host = builder.Build();
await host.RunAsync();
info: Kevin.App[0]
Listening on port localhost:5371
info: Microsoft.Hosting.Lifetime[0]
Application is shutting down...
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\ \Desktop\Dev\Projects\Kevin
info: Kevin.App[0]
Listening on port localhost:5371
info: Microsoft.Hosting.Lifetime[0]
Application is shutting down...
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\ \Desktop\Dev\Projects\Kevin
4 Replies
Jimmacle
Jimmacle10mo ago
probably just logging out of order what's your App service look like?
int
intOP10mo ago
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}");
}
}
Jimmacle
Jimmacle10mo ago
i'm not sure your StartAsync is supposed to block like that based on the example in the dotnet new worker project it should return when the startup stuff is done there is BackgroundService which is an IHostedService to wrap a simple async loop, that may be what you want instead of implementing it directly
int
intOP10mo ago
backgroundservice fixed it thx
Want results from more Discord servers?
Add your server