[英]"Error 1053 The Service did not respond" error when using TopShelf to create a Windows Service
[英]Topshelf service wont start as a service: Error 1053 The service did not respond.."
我可以直接从控制台 window 运行此服务,方法是使用服务应该运行的相同帐户调用可执行文件。 但是当我将它安装为服务并尝试运行它时,它会显示以下消息:
在我发布我的代码之前,这是我尝试过的:
像这里提到的这个人一样删除服务名称: “使用 TopShelf 创建 Windows 服务时出现“错误 1053 服务未响应”错误
在启动方法中添加新线程,如此处所述: https://github.com/Topshelf/Topshelf/issues/183
从Start
和Stop
方法返回true
这是完整的异常消息:
事务安装已完成。 Topshelf.Hosts.StartHost 错误:0:服务无法启动。,System.InvalidOperationException:无法在计算机“。”上启动服务 MyCabinet。 ---> System.ComponentModel.Win32Exception:服务没有及时响应启动或控制请求---内部异常堆栈跟踪结束---在 System.ServiceProcess.ServiceController.Start(String[] args ) 在 System.ServiceProcess.ServiceController.Start() 在 Topshelf.Runtime.Windows.WindowsHostEnvironment.StartService(String serviceName, TimeSpan startTimeOut) 在 Topshelf.Hosts.StartHost.Run()
这是代码:
Program.cs below:
using SmartCabinet.Core.Domain.Entities;
using SmartCabinet.Infrastructure.Database;
using System;
using System.IO;
using Topshelf;
namespace SmartCabinet
{
class Program
{
static void Main(string[] args)
{
try
{
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
HostFactory.Run(serviceConfig =>
{
serviceConfig.Service<FilesProcessor>(s =>
{
s.ConstructUsing(name => new FilesProcessor());
s.WhenStarted(execute => execute.Start()); //.BeforeStartingService(a => a.RequestAdditionalTime(TimeSpan.FromSeconds(120)));
s.WhenStopped(execute => execute.Stop());
});
//serviceConfig.SetServiceName("MyCabinet");
serviceConfig.SetDisplayName("Files Processor.");
serviceConfig.SetDescription("Windows service for files processing.");
serviceConfig.StartAutomatically();
});
}
catch(Exception ex)
{
var error = new ErrorLog()
{
ExceptionTitle = ex.Message,
ExceptionDescription = ex.InnerException?.Message,
CreatedDate = DateTime.Now
};
using (var context = new SmartCabinetDBContext())
{
context.ErrorLogs.Add(error);
context.SaveChanges();
}
}
}
}
}
FilesProcessor.cs
如下:
namespace MyCabinet
{
class FileProcessor
{
readonly System.Timers.Timer _timer;
public FileProcessor()
{
ProcessAndImportData();
_timer = new System.Timers.Timer(120000) { AutoReset = true };
_timer.Elapsed += (sender, eventArgs) => ProcessAndImportData();
//_timer.Elapsed += Timer_Elapsed;
//_timer.Enabled = true;
}
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
ProcessAndImportData();
}
public void ProcessAndImportData()
{
Console.WriteLine("It is {0} and processing has started", DateTime.Now);
// ... rest of the code
}
private void Download(string site, string itemPath, string file)
{
// .. some code
}
private void Upload(byte[] buffer, string pathToUpload, string site)
{
// .. some code
}
public bool Start() { _timer.Start(); return true; }
public bool Stop() { _timer.Stop(); return true; }
/* PREVIOUS UNUSED CODE:
public void foreverWhile()
{
while (true)
{
// to do something forever
}
}
public bool Start()
{
try
{
var myThread = new Thread(new ThreadStart(foreverWhile));
myThread.IsBackground = true; // This line will prevent thread from working after service stop.
myThread.Start();
return true;
//_timer.Start();
//return true;
}
catch(Exception ex)
{
var error = new ErrorLog()
{
ExceptionTitle = ex.Message,
ExceptionDescription = ex.InnerException?.Message,
CreatedDate = DateTime.Now
};
using (var context = new SmartCabinetDBContext())
{
context.ErrorLogs.Add(error);
context.SaveChanges();
}
}
return true;
}
public bool Stop() { _timer.Stop(); return true; }END OF PREVIOUS UNUSED CODE */
}
}
尝试将这行代码作为 Main 方法的第一行。
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.