我可以使用与服务运行时相同的帐户来调用可执行文件,从而直接从控制台窗口中运行该服务。但是,当我将其安装为服务并尝试运行时,它会显示以下消息:
在发布代码之前,这是我尝试过的操作:
完全删除服务名称,例如此处提到的这个人: "Error 1053 The Service did not respond" error when using TopShelf to create a Windows Service
在start方法中添加新线程,如下所示: https://github.com/Topshelf/Topshelf/issues/183
从true
和Start
方法中返回Stop
以下是完整的异常消息:
事务处理安装已完成。 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(字符串 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 */
}
}