[英]Topshelf service wont start as a service: Error 1053 The service did not respond.."
I can run this service directly from a console window by invoking the executable using the same account that the service is supposed to run as.我可以直接从控制台 window 运行此服务,方法是使用服务应该运行的相同帐户调用可执行文件。 But when I install it as service and when I try to run it it displays following message:但是当我将它安装为服务并尝试运行它时,它会显示以下消息:
Before I post my code this is what I've tried:在我发布我的代码之前,这是我尝试过的:
removing service name at all like this guy mentioned here: "Error 1053 The Service did not respond" error when using TopShelf to create a Windows Service像这里提到的这个人一样删除服务名称: “使用 TopShelf 创建 Windows 服务时出现“错误 1053 服务未响应”错误
adding new Thread in start method as its said here: https://github.com/Topshelf/Topshelf/issues/183在启动方法中添加新线程,如此处所述: https://github.com/Topshelf/Topshelf/issues/183
returning true
from Start
and Stop
methods从Start
和Stop
方法返回true
Here is full exception message:这是完整的异常消息:
The transacted install has completed.事务安装已完成。 Topshelf.Hosts.StartHost Error: 0: The service failed to start., System.InvalidOperationException: Cannot start service MyCabinet on computer '.'. Topshelf.Hosts.StartHost 错误:0:服务无法启动。,System.InvalidOperationException:无法在计算机“。”上启动服务 MyCabinet。 ---> System.ComponentModel.Win32Exception: The service did not respond to the start or control request in a timely fashion --- End of inner exception stack trace --- at System.ServiceProcess.ServiceController.Start(String[] args) at System.ServiceProcess.ServiceController.Start() at Topshelf.Runtime.Windows.WindowsHostEnvironment.StartService(String serviceName, TimeSpan startTimeOut) at Topshelf.Hosts.StartHost.Run() ---> 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()
Here is the code:这是代码:
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
below: 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 */
}
}
Try to put this line of code as a first-line in your Main method.尝试将这行代码作为 Main 方法的第一行。
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.