A basic console host which is similar to the AspNet host. Includes:
- DI
- Running as a Service/Console
- Logging
- Config
install-package ClickView.Extensions.Hosting
private static async Task Main(string[] args)
{
var isService = !(Debugger.IsAttached || args.Contains("--console"));
var builder = ConsoleHost.CreateDefaultBuilder(args)
.ConfigureServices((services =>
{
}));
if (isService)
{
await builder.RunAsServiceAsync();
}
else
{
await builder.RunConsoleAsync();
}
}
Workers need to be registerd in DI as Hosted Services
services.AddHostedService<TimedPrinter>();
Runs ExecuteAsync
once.
public class ExampleWorker : Worker
{
private readonly ILogger _logger;
private readonly IQueueClient _queueClient;
public ExampleWorker(ILogger logger, IQueueClient queueClient) : base(logger)
{
_logger = logger;
_queueClient = queueClient;
}
protected override Task ExecuteAsync(CancellationToken cancellationToken)
{
cancellationToken.Register(() => _queueClient.Unsubscribe());
return _queueClient.SubscribeAsync(OnEvent);
}
private Task OnEvent(Event evt)
{
_logger.LogInformation("New event! " + evt.Name);
return Task.CompletedTask;
}
}
Runs LoopAsync
every time defined by Interval
public class TimedPrinter : IntervalWorker
{
private readonly ILogger _logger;
private int _count;
public TimedPrinter(ILogger logger) : base(logger)
{
_logger = logger;
}
protected override TimeSpan Interval => TimeSpan.FromSeconds(1);
protected override Task LoopAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Hello! " + ++_count);
return Task.CompletedTask;
}
}