【发布时间】:2022-04-27 02:48:06
【问题描述】:
所以。我创建了一个工作服务并希望将其作为 Windows 服务运行。它在本地工作。我通过 powershell new-service 命令将它安装到 Windows 服务器。当我通过服务启动它时,它会尝试启动,等待 30 秒(加载栏正在进行)并失败。在事件查看器中,我看到一般错误:
- 等待 MyService 服务连接时超时(30000 毫秒)。
- MyService 服务因以下错误无法启动: 服务未及时响应启动或控制请求。
现在,奇怪的是,我在服务逻辑中添加了一些日志记录,它实际上确实需要一些东西,windows 只是无法启动它,即服务没有响应启动,但工作(30 秒,在那台服务器之后杀死它,因为它没有响应开始)。 我如何解决它? 我的 Program.cs:
public class Program
{
public static void Main(string[] args)
{
try
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
CreateHostBuilder(args).Build().Run();
}
catch (Exception ex)
{
File.AppendAllText("templogs.txt", ex.Message + "\r\n");
}
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.UseWindowsService()
.ConfigureServices((hostContext, services) =>
{
IConfiguration configuration = hostContext.Configuration;
services.AddHostedService<Worker>();
services.AddScoped<IActionHandler, ActionHandler>();
services.AddScoped<IHttpHandler, HttpHandler>();
services.AddDbContext<DbContext>(options =>
{
options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
});
File.AppendAllText("templogs.txt", "context registered\r\n");
});
}
工人服务:
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
private readonly List<string> _inactiveStatuses;
private readonly IServiceProvider _services;
private readonly string _connectionString;
public Worker(ILogger<Worker> logger, IConfiguration config, IServiceProvider services)
{
try
{
_logger = logger;
_inactiveStatuses = config.GetSection("InactiveStatuses").GetChildren().Select(a => a.Value).ToList();
_services = services;
_connectionString = config.GetConnectionString("DefaultConnection");
}
catch (Exception ex)
{
File.AppendAllText("templogs.txt", ex.Message + "\r\n");
}
}
public override async Task StartAsync(CancellationToken cancellationToken)
{
await base.StartAsync(cancellationToken);
}
public override async Task StopAsync(CancellationToken cancellationToken)
{
await base.StopAsync(cancellationToken);
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
using (var scope = _services.CreateScope())
{
File.AppendAllText("templogs.txt", "scope created\r\n");
var actionHandler =
scope.ServiceProvider
.GetRequiredService<IActionHandler>();
var dbContext = scope.ServiceProvider.GetRequiredService<DbContext>();
var activeStatuses = dbContext.Statuses.Where(a => !_inactiveStatuses.Contains(a.Name)).ToDictionary(a => a.Id, a => a.Name);
List<Guid> activeStatusGuids = activeStatuses.Keys.ToList();
while (!stoppingToken.IsCancellationRequested)
{
File.AppendAllText("templogs.txt", "while strated\r\n");
//some logic
File.AppendAllText("templogs.txt", "first cycle\r\n");
await Task.Delay(1000, stoppingToken);
}
}
}
catch (Exception ex)
{
File.AppendAllText("templogs.txt", ex.Message + "\r\n");
}
}
}
【问题讨论】:
-
你确定你发布的代码有这个问题吗?
-
是的,我确定。
标签: c# asp.net-core windows-services backgroundworker windows-server