我已经在.net核心中创建了一个工人,该工人可以计算斐波那契数列。但是我需要能够阅读本系列文章,作为这位工作人员的回应。我对此很陌生,因此经过数小时的研究,我找不到解决方案。
这是我的代码:
public class FibonacciCalculateWorker : BackgroundService
{
private readonly ILogger<FibonacciCalculateWorker> _logger;
public FibonacciCalculateWorker(ILogger<FibonacciCalculateWorker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
stoppingToken.ThrowIfCancellationRequested();
await CreateFibonacciSeries();
}
public Task<string> CreateFibonacciSeries()
{
int a = 0, b = 1, c = 0;
StringBuilder fiboSeries = new StringBuilder();
fiboSeries.Append(a + "," + b);
for (int i = 2; i < 50; i++)
{
c = a + b;
fiboSeries.Append("," + c);
a = b;
b = c;
}
_logger.LogInformation(fiboSeries.ToString());
return Task.FromResult(fiboSeries.ToString());
}
}
和program.cs:
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{
services.AddHostedService<FibonacciCalculateWorker>();
});
任何建议将不胜感激。