简体   繁体   English

Windows服务无法正常运行

[英]Windows service not working properly

I have created a service using visual studio and installed it using visual studio command prompt. 我已经使用Visual Studio创建了服务,并使用Visual Studio命令提示符安装了该服务。 It works fine 工作正常

But now I want to install the same service using a visual studio installer. 但是现在我想使用Visual Studio安装程序安装相同的服务。 I have created a installer by following this link . 我通过以下链接创建了一个安装程序。 The installer does install the service properly as it does not throw any error or exception. 安装程序会正确安装服务,因为它不会引发任何错误或异常。 But when I start the service from the service list, the service does start but it does not write into the log file. 但是,当我从服务列表中启动该服务时,该服务会启动,但不会写入日志文件。 So can anyone help me and tell why the service performs OK while installed using command prompt but causing issues when installed through installer. 所以任何人都可以帮助我,并说明为什么使用命令提示符安装该服务时执行OK的原因,但是通过安装程序安装时却引起问题。

protected override void OnStart(string[] args)
{
    timer1 = new Timer();
    this.timer1.Interval = 5000;
    this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_tick);
    timer1.Enabled = true;
    Library.WriteErrorLog("service started!");
}

private void timer1_tick(object sender, ElapsedEventArgs e) 
{ 
    Library.WriteErrorLog("Timer ticked succesfully"); 
}

protected override void OnStop()
{
    timer1.Enabled = false;
    Library.WriteErrorLog("Service Stopped!");
}

Have you checked that the service has permission to write to the log file? 您是否检查过该服务是否有权写入日志文件?

Check the permissions for the directory, go to log folder - Properties and the Security tab. 检查目录的权限,转到日志文件夹-“属性”和“安全性”选项卡。

Services are usually run without write access to the Windows file system. 服务通常在没有Windows文件系统写访问权限的情况下运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM