【发布时间】:2022-11-03 20:02:25
【问题描述】:
我尝试基于 C# 启动我自己的 Windows 服务。 我尝试使用 sc.exe create Service.exe 运行,但是当我想运行此服务时,我遇到了错误 #1026(描述:进程因未处理的异常而终止。)和 #1000。 (故障应用程序名称:Timesync.exe,版本:1.0.0.0,时间戳:0xf1683f8e 错误模块名称:KERNELBASE.dll,版本:10.0.22621.674,时间戳:0x160a2aa8)现在我尝试使用 InstallUtil.exe 使用此服务,但我不能。因为我有这个错误:初始化安装时发生异常: System.BadImageFormatException:无法加载文件或程序集“file:Service.exe”或其依赖项之一。该模块应包含一个程序集清单。 这是我的代码:
Timer Schedular;
public Service1()
{
InitializeComponent();
if (!EventLog.SourceExists("Timesync"))
EventLog.CreateEventSource("Timesync", "TimesyncLog");
eventLog1.Source = "Timesync";
eventLog1.Log = "TimesyncLog";
}
protected override async void OnStart(string[] args)
{
System.Diagnostics.Debugger.Launch();
eventLog1.WriteEntry("Timesync was started", EventLogEntryType.Information);
await this.ScheduleService();
}
public void onDebug()
{
OnStart(null);
}
private async void SchedularCallback(object e)
{
await this.ScheduleService();
}
private async Task ScheduleService()
{
try
{
}
catch (Exception ex)
{
eventLog1.WriteEntry("Timesync was be here on catch state", EventLogEntryType.Information);
//Stop the Windows Service.
using (ServiceController serviceController = new ServiceController("Timesync"))
{
serviceController.Stop();
}
}
}
程序.cs
internal class Program
{
static void Main(string[] args)
{
//In Release this section is used. This is the "normal" way.
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
Console.WriteLine("The application was be here");
}
}
我不明白如何解决我的错误以及如何运行此服务。 如果可能的话,请帮忙。 :)
【问题讨论】:
-
请粘贴您的代码而不是屏幕截图,它会更容易理解(并在搜索中找到)。
-
什么是 Service.exe,什么是 Timesync.exe?
-
service.exe 是 Timesync.exe。 Timesync 和 service.exe 是一项服务 Time sync 是一项时间同步服务,必须每 3 秒向客户端发送一次信息。
标签: c# .net windows-services failed-installation