我编写了一个在 Win10 上运行的 Windows 服务,它运行得非常好,直到我决定对其进行一些更改。我重写了一些逻辑,在调试和发布配置中进行了测试,一切都很好。然后我使用
installutil.exe /u %servicename.exe%
卸载了当前版本的服务,并使用 installutil.exe %servicename.exe%
重新安装了它。
由于某种原因,这个新版本无法启动,并且崩溃并出现错误 1064。这是完整的错误文本:
Windows could not start %servicename% service on Local Computer. Error 1064: An exception occurred in the service when handling the control request.
上次安装此服务时,我遇到了一些困难,但通过更改
Log On
属性很快就解决了这些问题。这一次,它不起作用了。请帮忙解决这个问题。
谢谢。
更新1
这是我的
Main()
和OnStart()
服务方法:
Main()
static void Main()
{
#if DEBUG
var service = new SalesforceToJiraService();
service.OnDebug();
Thread.Sleep(Timeout.Infinite);
#else
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new SalesforceToJiraService()
};
ServiceBase.Run(ServicesToRun);
#endif
}
OnStart()
protected override void OnStart(string[] args)
{
this.ConfigureServices();
this.timer.Start();
this.logger.Information("SalesforceToJira service started.");
}
更新2
更多代码:
ConfigureServices()
protected void ConfigureServices()
{
this.configuration = ConfigurationHelper.LoadConfiguration(ConfigurationPath);
this.logger = ConfigurationHelper.ConfigureLogger(this.configuration.Logs.LogsPath);
this.timer = ConfigurationHelper.ConfigureTimer(this.configuration.ProcessInterval.TotalMilliseconds,
(sender, eventArgs) => this.ProcessCasesAsync(sender, eventArgs).GetAwaiter().GetResult());
this.salesforceClient = new SalesforceCliClient(this.configuration.Salesforce.CliPath);
this.jiraClient = Jira.CreateRestClient(
this.configuration.Jira.Url,
this.configuration.Jira.Username,
this.configuration.Jira.Password);
}
我使用
Newtonsoft.JSON
反序列化 JSON 配置文件,Serilog
用于日志记录,System.Timers.Timer
用于定期事件,AtlassianSDK
用于 Jira API,以及 Salesforce CLI
上的一些包装器用于 Salesforce。
感谢 @Siderite Zackwehdex 的评论,我能够在 EventViewer 中找到底层异常的完整堆栈跟踪,如下:
Windows 日志\应用程序
在我的例子中,我的服务名为“HttpDispatcher”,它出现在顶部窗格的“源”列中。
我立即发现这是由于依赖问题造成的,我的 .NET 4.7.2 项目没有跨过我的 .NET Standard 引用。 (那个老栗子)。
我也遇到了同样的问题。原因是我忘记在配置中正确设置数据库连接。
我在启动服务时遇到了完全相同的错误 1064。对于我来说,我注册服务的用户不是数据库中的有效用户。添加后,效果非常好。
我的 Windows 服务也出现同样的错误。 原因是无法读取配置参数,所以崩溃了。
添加一些验证(错误修复),Windows 服务可以正确启动它。
我也遇到过这个问题。就我而言,这是由于数据库连接失败。我认为这是由于代码抛出异常。
我的错误: Windows 无法在本地计算机上启动 service1 服务。
错误1064:处理控制请求时服务发生异常
我通过更新第三方 DLL 纠正了我的问题。
我遇到了同样的问题,这是我在故障排除后解决的方法。
如果您在具有多个用户的服务器上运行服务,请 确保以管理员用户身份运行该服务。点击服务 属性,然后在登录选项卡上单击此帐户并提供 管理员用户名和密码。
如果您的服务正在访问某些共享驱动器,请确保 您在所有服务器上都有一个通用用户来访问共享 驱动器并将用户添加为本地管理员。
对我来说,当我尝试重新启动进程时,就会发生这种情况。结果发现该进程挂在“正在停止”状态,所以我必须通过命令行和 PID 手动终止它。
如果您使用.net 7.0,那么您需要添加 Microsoft.Extensions.Hosting.WindowsServices.dll 和program.cs文件中的.UseWindowsService()