[英]1053 Windows Service Error using .NET Core 3.1 Worker Service
I've create a Worker Service that works as intended on both debug and release in Visual Studio 2019. The service watches for.csv to be created, and rewrites it with proper encoding (UTF-8) to another directory.我创建了一个 Worker 服务,该服务在 Visual Studio 2019 中的调试和发布中均按预期工作。该服务监视要创建的.csv,并使用正确的编码 (UTF-8) 将其重写到另一个目录。 When I've published it, and a Windows Service is created, on starting the Windows Service I am receiving Error 1053: The service did not respond to the start or control request in a timely fashion
.当我发布它并创建 Windows 服务时,在启动 Windows 服务时,我收到Error 1053: The service did not respond to the start or control request in a timely fashion
。 From what I understand, my OnStart
is not returning quick enough.据我了解,我的OnStart
没有足够快地返回。 But I am unsure of how to debug.但我不确定如何调试。
Program class程序 class
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using System;
namespace SupplyProUTF8service
{
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(@"\\REP-APP\temp\workerservice\log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
try
{
Log.Information("Application Started.");
CreateHostBuilder(args).Build().Run();
}
catch (Exception e)
{
Log.Fatal(e, "Application terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args).UseWindowsService().ConfigureServices((hostContext, services)
=> { services.AddHostedService<Worker>(); }).UseSerilog();
}
}
New Worker Class Still get 1053 error新工人 Class仍然得到 1053 错误
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace SupplyProUTF8service
{
public class Worker : BackgroundService
{
private readonly string ordrstkPath;
private readonly string conrstkPath;
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
ordrstkPath = @"\\Rep-app\sftp_root\supplypro\ordrstk";
conrstkPath = @"\\Rep-app\sftp_root\supplypro\Conrstk";
_logger = logger;
}
public override Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("SupplyProRewrite Service started");
return base.StartAsync(cancellationToken);
}
private FileSystemWatcher Watch(string path)
{
//initialize
FileSystemWatcher watcher = new FileSystemWatcher
{
//assign paramater path
Path = path,
//don't watch subdirectories
IncludeSubdirectories = false
};
//file created event
watcher.Created += FileSystemWatcher_Created;
//filters
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size | NotifyFilters.Attributes;
//only look for csv
watcher.Filter = "*.csv";
// Begin watching.
watcher.EnableRaisingEvents = true;
return watcher;
}
private void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
_logger.LogInformation("{FullPath} has been created", e.FullPath);
Thread.Sleep(10000);
while (!IsFileLocked(e.FullPath))
{
ReadWriteStream(e.FullPath, e.Name);
break;
}
}
private static bool IsFileLocked(string filePath)
{
try
{
using FileStream originalFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
originalFileStream.Close();
}
catch (Exception)
{
return true;
}
return false;
}
private void ReadWriteStream(string path, string fileName)
{
string originalPath = path;
//destination path by replacing SFTP user directory
string destinationPath = path.Replace(@"\supplypro\", @"\ftpuser\");
string currentLine;
using FileStream originalFileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
using FileStream destinationFileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write);
using StreamReader streamReader = new StreamReader(originalFileStream);
using StreamWriter streamWriter = new StreamWriter(destinationFileStream);
try
{
currentLine = streamReader.ReadLine();
while (currentLine != null)
{
streamWriter.WriteLine(currentLine);
currentLine = streamReader.ReadLine();
}
streamReader.Close();
streamWriter.Close();
//archive path
string archivePath = path.Replace(fileName, @"archive\" + fileName);
//move to archive path
while (!IsFileLocked(originalPath))
{
try
{
File.Move(originalPath, archivePath, true);
_logger.LogInformation("{FileName} moved to archive", fileName);
break;
}
catch (Exception e)
{
_logger.LogError("Unable to move {fileName} to archive", fileName, e);
break;
}
}
}
catch (Exception e)
{
//error path
string errorPath = path.Replace(fileName, @"error\" + fileName);
//move to error path
while (!IsFileLocked(originalPath))
{
File.Move(path, errorPath);
_logger.LogError("{FullPath} file was moved to error", originalPath, e);
break;
}
}
finally
{
destinationFileStream.Close();
originalFileStream.Close();
}
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using (Watch(ordrstkPath))
{
_logger.LogInformation("ordrstk being watched");
await Task.Delay(Timeout.Infinite, stoppingToken);
}
using(Watch(conrstkPath))
{
_logger.LogInformation("conrstk being watched");
await Task.Delay(Timeout.Infinite, stoppingToken);
}
}
}
}
I've create a Worker Service that works as intended on both debug and release in Visual Studio 2019. The service watches for .csv to be created, and rewrites it with proper encoding (UTF-8) to another directory.我已经创建了一个工人服务,该服务可以在Visual Studio 2019中的调试和发布中按预期工作。该服务监视要创建的.csv,并使用正确的编码(UTF-8)将其重写到另一个目录。 When I've published it, and a Windows Service is created, on starting the Windows Service I am receiving Error 1053: The service did not respond to the start or control request in a timely fashion
.发布它并创建Windows服务后,在启动Windows服务时收到Error 1053: The service did not respond to the start or control request in a timely fashion
。 From what I understand, my OnStart
is not returning quick enough.据我了解,我的OnStart
返回速度不够快。 But I am unsure of how to debug.但是我不确定如何调试。
Program class程序类
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using System;
namespace SupplyProUTF8service
{
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(@"\\REP-APP\temp\workerservice\log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
try
{
Log.Information("Application Started.");
CreateHostBuilder(args).Build().Run();
}
catch (Exception e)
{
Log.Fatal(e, "Application terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args).UseWindowsService().ConfigureServices((hostContext, services)
=> { services.AddHostedService<Worker>(); }).UseSerilog();
}
}
New Worker Class Still get 1053 error新工人阶级仍然得到1053错误
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace SupplyProUTF8service
{
public class Worker : BackgroundService
{
private readonly string ordrstkPath;
private readonly string conrstkPath;
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
ordrstkPath = @"\\Rep-app\sftp_root\supplypro\ordrstk";
conrstkPath = @"\\Rep-app\sftp_root\supplypro\Conrstk";
_logger = logger;
}
public override Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("SupplyProRewrite Service started");
return base.StartAsync(cancellationToken);
}
private FileSystemWatcher Watch(string path)
{
//initialize
FileSystemWatcher watcher = new FileSystemWatcher
{
//assign paramater path
Path = path,
//don't watch subdirectories
IncludeSubdirectories = false
};
//file created event
watcher.Created += FileSystemWatcher_Created;
//filters
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size | NotifyFilters.Attributes;
//only look for csv
watcher.Filter = "*.csv";
// Begin watching.
watcher.EnableRaisingEvents = true;
return watcher;
}
private void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
_logger.LogInformation("{FullPath} has been created", e.FullPath);
Thread.Sleep(10000);
while (!IsFileLocked(e.FullPath))
{
ReadWriteStream(e.FullPath, e.Name);
break;
}
}
private static bool IsFileLocked(string filePath)
{
try
{
using FileStream originalFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
originalFileStream.Close();
}
catch (Exception)
{
return true;
}
return false;
}
private void ReadWriteStream(string path, string fileName)
{
string originalPath = path;
//destination path by replacing SFTP user directory
string destinationPath = path.Replace(@"\supplypro\", @"\ftpuser\");
string currentLine;
using FileStream originalFileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
using FileStream destinationFileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write);
using StreamReader streamReader = new StreamReader(originalFileStream);
using StreamWriter streamWriter = new StreamWriter(destinationFileStream);
try
{
currentLine = streamReader.ReadLine();
while (currentLine != null)
{
streamWriter.WriteLine(currentLine);
currentLine = streamReader.ReadLine();
}
streamReader.Close();
streamWriter.Close();
//archive path
string archivePath = path.Replace(fileName, @"archive\" + fileName);
//move to archive path
while (!IsFileLocked(originalPath))
{
try
{
File.Move(originalPath, archivePath, true);
_logger.LogInformation("{FileName} moved to archive", fileName);
break;
}
catch (Exception e)
{
_logger.LogError("Unable to move {fileName} to archive", fileName, e);
break;
}
}
}
catch (Exception e)
{
//error path
string errorPath = path.Replace(fileName, @"error\" + fileName);
//move to error path
while (!IsFileLocked(originalPath))
{
File.Move(path, errorPath);
_logger.LogError("{FullPath} file was moved to error", originalPath, e);
break;
}
}
finally
{
destinationFileStream.Close();
originalFileStream.Close();
}
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using (Watch(ordrstkPath))
{
_logger.LogInformation("ordrstk being watched");
await Task.Delay(Timeout.Infinite, stoppingToken);
}
using(Watch(conrstkPath))
{
_logger.LogInformation("conrstk being watched");
await Task.Delay(Timeout.Infinite, stoppingToken);
}
}
}
}
I've create a Worker Service that works as intended on both debug and release in Visual Studio 2019. The service watches for .csv to be created, and rewrites it with proper encoding (UTF-8) to another directory.我已经创建了一个工人服务,该服务可以在Visual Studio 2019中的调试和发布中按预期工作。该服务监视要创建的.csv,并使用正确的编码(UTF-8)将其重写到另一个目录。 When I've published it, and a Windows Service is created, on starting the Windows Service I am receiving Error 1053: The service did not respond to the start or control request in a timely fashion
.发布它并创建Windows服务后,在启动Windows服务时收到Error 1053: The service did not respond to the start or control request in a timely fashion
。 From what I understand, my OnStart
is not returning quick enough.据我了解,我的OnStart
返回速度不够快。 But I am unsure of how to debug.但是我不确定如何调试。
Program class程序类
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Serilog;
using System;
namespace SupplyProUTF8service
{
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(@"\\REP-APP\temp\workerservice\log.txt", rollingInterval: RollingInterval.Day)
.CreateLogger();
try
{
Log.Information("Application Started.");
CreateHostBuilder(args).Build().Run();
}
catch (Exception e)
{
Log.Fatal(e, "Application terminated unexpectedly");
}
finally
{
Log.CloseAndFlush();
}
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args)
=> Host.CreateDefaultBuilder(args).UseWindowsService().ConfigureServices((hostContext, services)
=> { services.AddHostedService<Worker>(); }).UseSerilog();
}
}
New Worker Class Still get 1053 error新工人阶级仍然得到1053错误
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace SupplyProUTF8service
{
public class Worker : BackgroundService
{
private readonly string ordrstkPath;
private readonly string conrstkPath;
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
ordrstkPath = @"\\Rep-app\sftp_root\supplypro\ordrstk";
conrstkPath = @"\\Rep-app\sftp_root\supplypro\Conrstk";
_logger = logger;
}
public override Task StartAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("SupplyProRewrite Service started");
return base.StartAsync(cancellationToken);
}
private FileSystemWatcher Watch(string path)
{
//initialize
FileSystemWatcher watcher = new FileSystemWatcher
{
//assign paramater path
Path = path,
//don't watch subdirectories
IncludeSubdirectories = false
};
//file created event
watcher.Created += FileSystemWatcher_Created;
//filters
watcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.Size | NotifyFilters.Attributes;
//only look for csv
watcher.Filter = "*.csv";
// Begin watching.
watcher.EnableRaisingEvents = true;
return watcher;
}
private void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
_logger.LogInformation("{FullPath} has been created", e.FullPath);
Thread.Sleep(10000);
while (!IsFileLocked(e.FullPath))
{
ReadWriteStream(e.FullPath, e.Name);
break;
}
}
private static bool IsFileLocked(string filePath)
{
try
{
using FileStream originalFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
originalFileStream.Close();
}
catch (Exception)
{
return true;
}
return false;
}
private void ReadWriteStream(string path, string fileName)
{
string originalPath = path;
//destination path by replacing SFTP user directory
string destinationPath = path.Replace(@"\supplypro\", @"\ftpuser\");
string currentLine;
using FileStream originalFileStream = new FileStream(path, FileMode.Open, FileAccess.Read);
using FileStream destinationFileStream = new FileStream(destinationPath, FileMode.Create, FileAccess.Write);
using StreamReader streamReader = new StreamReader(originalFileStream);
using StreamWriter streamWriter = new StreamWriter(destinationFileStream);
try
{
currentLine = streamReader.ReadLine();
while (currentLine != null)
{
streamWriter.WriteLine(currentLine);
currentLine = streamReader.ReadLine();
}
streamReader.Close();
streamWriter.Close();
//archive path
string archivePath = path.Replace(fileName, @"archive\" + fileName);
//move to archive path
while (!IsFileLocked(originalPath))
{
try
{
File.Move(originalPath, archivePath, true);
_logger.LogInformation("{FileName} moved to archive", fileName);
break;
}
catch (Exception e)
{
_logger.LogError("Unable to move {fileName} to archive", fileName, e);
break;
}
}
}
catch (Exception e)
{
//error path
string errorPath = path.Replace(fileName, @"error\" + fileName);
//move to error path
while (!IsFileLocked(originalPath))
{
File.Move(path, errorPath);
_logger.LogError("{FullPath} file was moved to error", originalPath, e);
break;
}
}
finally
{
destinationFileStream.Close();
originalFileStream.Close();
}
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
using (Watch(ordrstkPath))
{
_logger.LogInformation("ordrstk being watched");
await Task.Delay(Timeout.Infinite, stoppingToken);
}
using(Watch(conrstkPath))
{
_logger.LogInformation("conrstk being watched");
await Task.Delay(Timeout.Infinite, stoppingToken);
}
}
}
}
The same issue happened in the Dot Net core 6 service project(Default Project template from visual studio 2022) on the windows 11 machine. windows 11 机器上的 Dot Net core 6 服务项目(Visual Studio 2022 的默认项目模板)中也发生了同样的问题。 Use UseWindowsService() after the host declartion.在主机声明后使用 UseWindowsService () 。
IHost host = Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
}).UseWindowsService()
.Build();await host.RunAsync();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.