[英]Win Service works as Console Application but not as Windows Service
我使用 Topshelf 4.2.1 创建了一个控制台应用程序服务。 像 Windows 服务一样使用它。 我正在使用 Dapper 获取数据并更新 Microsoft SQL Server,我从 app.config 中获取连接字符串以及(文件夹路径、文件夹限制)中的常量控制台应用程序运行良好,但是当我作为服务安装时,它启动但是什么都不做。
enter code here class Program
{
static void Main(string[] args)
{
var exitCode = HostFactory.Run(x =>
{
x.Service<EmailMonitoring>(s =>
{
s.ConstructUsing(emailMonitoring => new EmailMonitoring());
s.WhenStarted(emailMonitoring => emailMonitoring.Start());
s.WhenStopped(emailMonitoring => emailMonitoring.Stop());
});
x.RunAsLocalSystem();
x.SetServiceName("EmailMonitoring");
x.SetDisplayName("Email Monitoring");
x.SetDescription("Description");
});
int exitCodeValue = (int)Convert.ChangeType(exitCode, exitCode.GetTypeCode());
Environment.ExitCode = exitCodeValue;
}
}
enter code here private readonly Timer _timer;
public EmailMonitoring()
{
_timer = new Timer(1000 * int.Parse(Helper.AppSetVal("intervalSeconds"))) { AutoReset = false };
// when testing, use smaller interval and autoreset = false like example below
// _timer = new Timer( 1000 ) { AutoReset = false };
_timer.Elapsed += TimerElapsed;
}
private void TimerElapsed(object sender, ElapsedEventArgs e)
{
ServiceHelper.ProcessNewRecords();
}
public void Start()
{
_timer.Start();
}
public void Stop()
{
_timer.Stop();
}
已解决:在 Microsoft Server SQL 中,将 NT AUTHORITY\\SYSTEM 的登录属性设置为 sysadmin。 点击截图
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.