![](/img/trans.png)
[英]"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.