我按照本教程构建了应用程序。它启动,一切正常。
将应用程序移至服务器。由于我Publish
在 Visual Studio 中构建应用程序,表明我想要一个文件,所以我正在迁移三个文件.exe
, .pdb
, settings.json
.
通过sc
,如手册中所示,我创建了一个服务
.\sc.exe create "service1" binpath="C:\path\to\publish\service.exe"
并在服务器上运行
.\sc.exe start "service1"
我收到一个错误
[SC] StartService FAILED 1053:
The service did not respond to the start or control request in a timely fashion. windows server
当然,我搜索过,这是我尝试过的:
ServicesPipeTimeout
- 寄存器中的键操作- 检查版本
.NET
并Runtime
与他们一起 - 系统更新
给我一个提示如何捕捉阻止发射的东西?
适用于.NET 6.0
、C#、Windows Server 2019的应用程序
程序.cs
using CsvToDdWorker;
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Logging.EventLog;
using IHost host = Host.CreateDefaultBuilder(args)
.UseWindowsService(options =>
{
options.ServiceName = "service1";
})
.ConfigureServices(services =>
{
LoggerProviderOptions.RegisterProviderOptions<
EventLogSettings, EventLogLoggerProvider>(services);
services.AddSingleton<SendToDbService>();
services.AddHostedService<WindowsBackgroundService>();
})
.ConfigureLogging((context, logging) =>
{
logging.AddConfiguration(
context.Configuration.GetSection("Logging"));
})
.Build();
await host.RunAsync();
WindowsBackgroundService.cs
namespace CsvToDdWorker
{
public class WindowsBackgroundService : BackgroundService
{
private readonly SendToDbService _sendService;
private readonly ILogger<WindowsBackgroundService> _logger;
public WindowsBackgroundService(
SendToDbService sendService,
ILogger<WindowsBackgroundService> logger) =>
(_sendService, _logger) = (sendService, logger);
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
try
{
while (!stoppingToken.IsCancellationRequested)
{
_sendService.SendToDb(_logger);
await Task.Delay(TimeSpan.FromMinutes(20), stoppingToken);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "{Message}", ex.Message);
Environment.Exit(1);
}
}
}
}
UPD
我还尝试过使用应用程序启动期。改为秒、分、小时。这不是关于那个。
UPD1
在没有服务功能的服务器上开始执行SendToDb(_logger)
。直通dotnet run
。她工作没有问题。
事实上,问题中指出的错误非常广泛。附加到异常的描述根本没有任何意义,这是
我们设法发现:
Publish
所有设置窗口中的-routineDeployment mode
->Self-contained
忘记指定。