c# - 安装后无法启动 Windows 服务
问题描述
我按照这篇文章构建了我的 Windows 服务应用程序。
现在我的项目目录有MyService.cs
和ProjectInstaller.cs
. 两者都包含一个Designer.cs
和一个.resx
文件。
构建项目后,我将 /bin/Debug 下的所有内容复制到服务器,并运行InstallUtil
命令进行安装。
我可以在服务列表中看到我的服务,但是当我单击开始时,需要很长时间才能启动,直到它超时。
我的 Program.cs 文件很简单
public static void Main()
{
#if DEBUG
MyService mySvc = new MyService();
mySvc.OnDebug();
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new MyService()
};
ServiceBase.Run(ServicesToRun);
#endif
}
在MyService.cs
:
public partial class MyService : ServiceBase
{
private bool stopping = false;
private Int32 timeInterval = 0;
private ManualResetEvent stoppedEvent;
public static IServiceProvider svcProvider = null;
public MyService()
{
InitializeComponent();
stopping = false;
stoppedEvent = new ManualResetEvent(false);
LoadDIStartup();
}
public void OnDebug()
{
StartServiceWorkerMainProcess();
}
protected override void OnStart(string[] args)
{
stopping = false;
StartServiceWorkerMainProcess();
}
protected override void OnStop()
{
stopping = true;
StopServiceWorkerMainProcess();
stoppedEvent.WaitOne(); //Wait for the finish of the ServiceWorkerThread
}
/// <summary>
/// The function called in the start event of the the service. And when in Visual Studio Debug Mode run.
/// </summary>
public void StartServiceWorkerMainProcess()
{
try
{
timeInterval = AppConfig.TimeInterval;
// Queue the SubWorker for execution in a worker thread.
ThreadPool.QueueUserWorkItem(new WaitCallback(ServiceWorkerSub));
}
catch (Exception e)
{
AppLogger.LogError(" Error while launching the WorkerSub thread. " + "\n" + e.Message + "\n" + e.InnerException + " \n" + e.StackTrace + "\n" + e.Source + "\n");
}
}
/// <summary>
/// The function called in the stop event of the the service
/// </summary>
public void StopServiceWorkerMainProcess()
{
AppLogger.LogInfo(" Service Stopped at " + DateTime.Now.ToShortDateString() + " " + DateTime.Now.ToShortTimeString() + "\n");
}
private async void ServiceWorkerSub(object state)
{
try
{
// Periodically check if the service is stopping
while (!stopping)
{
try
{
//Do my Stuff (FYI, Mutex is used here.)
AppLogger.LogInfo("DONE. About to sleep.");
}
catch (Exception e)
{
AppLogger.LogError(" Error. " + "\n" + e.Message + "\n" + e.InnerException + " \n" + e.StackTrace + "\n" + e.Source + "\n");
}
Thread.Sleep(timeInterval);
}
// Signal the stopped event
stoppedEvent.Set();
}
catch (Exception e)
{
AppLogger.LogError(" Error in ServiceWorkerSub. " + "\n" + e.Message + "\n" + e.InnerException + " \n" + e.StackTrace + "\n" + e.Source + "\n");
}
}
private static void LoadDIStartup()
{
//Dependency Injection Setup Start
// blah blah. DI using json files.
//Dependency Injection Setup End
}
}
在设计器视图中, MyService.cs 有一个serviceController1
,我设置了 serviceName = TestService
。ProjectInstaller.cs 有一个serviceProcessInstaller1
和serviceInstaller1
。serviceInstaller1 中的 serviceName 属性也是TestService
如此。
该项目内部有几个属性文件,并且在本地运行良好。
我的设置有问题吗?
解决方案
当您构建安装程序时,请确保您处于发布模式,否则您的#if DEBUG
逻辑就会启动。
推荐阅读
- nativescript - 无法更改 iOS NativeScript 中的状态栏颜色
- jenkins - Jenkins 无法与声纳交互
- python - 为什么点子会引发不相关的警告?
- r - R使用变异循环nleqslv
- qt - QComboBox 窃取对弹出窗口的关注
- ember.js - 从 Ember 模型计算的 Promise 中解析字符串
- python - 使用 Python 进行文本替换
- javascript - 按钮的事件侦听器不会更改 Angular 中的类字段
- typescript - Angular 5 将未定义的 @Input() 变量值获取到从 ngOnInit() 调用的函数中
- javascript - 来自json的Morris.js多折线图