"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();
[2;45minfo:[0m Kevin.App[0]
Listening on port localhost:5371
[2;45m[2;40m[2;45minfo:[0m[2;40m[0m[2;45m[0m Microsoft.Hosting.Lifetime[0]
Application is shutting down...
[2;45minfo:[0m Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
[2;45minfo:[0m Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
[2;45minfo:[0m Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\ \Desktop\Dev\Projects\Kevin
[2;45minfo:[0m Kevin.App[0]
Listening on port localhost:5371
[2;45m[2;40m[2;45minfo:[0m[2;40m[0m[2;45m[0m Microsoft.Hosting.Lifetime[0]
Application is shutting down...
[2;45minfo:[0m Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
[2;45minfo:[0m Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
[2;45minfo:[0m Microsoft.Hosting.Lifetime[0]
Content root path: C:\Users\ \Desktop\Dev\Projects\Kevin
4 Replies
probably just logging out of order
what's your App service look like?
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}");
}
}
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 directlybackgroundservice fixed it thx