die statische Funktion Run() der Klasse Bot wird nur einmal ausgeführt, dann stoppt der Dienst automatisch.
Was mache ich falsch?
public class Program
{
public static void Main(string[] args)
{
Log.Logger = new LoggerConfiguration()
.MinimumLevel.Debug()
.MinimumLevel.Override("Microsoft", Serilog.Events.LogEventLevel.Warning)
.Enrich.FromLogContext()
.WriteTo.File(@"C:\SosaBot\LogFile.txt")
.CreateLogger();
try
{
Log.Information("Starting up the Service");
CreateHostBuilder(args).Build().Run();
return;
}
catch (Exception ex)
{
Log.Fatal(ex, "-ther was a problem, starting the service");
return;
}
finally
{
Log.CloseAndFlush();
}
}
public static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<Worker>();
})
.UseSerilog();
}
}
public class Worker : BackgroundService
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
await Bot.Run();
await Task.Delay(1000 * 60 * 3, stoppingToken);
}
}
}
public static class Bot
{
public static async Task Run()
{
Log.Information("Starting Bot");
var userClient = new TwitterClient(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
(await userClient.Search.SearchTweetsAsync("Sportsfreund Sosa"))
.ToList()
.ForEach(async tweet =>
{
Log.Information($"retweeting: {tweet.FullText}");
await userClient.Tweets.PublishRetweetAsync(tweet.Id);
});
Log.Information("Stopping Bot");
}
}
PS: Die Zugangsdaten sind da, habe sie aber hier heraus genommen.