无法从我的电脑启动C#窗口服务



我尝试在基于C#的平台上启动自己的Windows服务。我尝试使用sc.exe create Service.exe运行,但当我想运行此服务时,我有错误#1026(描述:由于未处理的异常,进程终止。(和#1000。(出现故障的应用程序名称:Timesync.exe,版本:1.0.0.0,时间戳:0xf1683f8e出现故障的模块名称:KERNELBASE.dll,版本:10.0.222621.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");

}
}

我不知道如何解决我的错误以及如何运行此服务。如果可能的话,请帮忙。:(

您尝试在x64解决方案中使用32位库的任何机会,反之亦然。当我没有在项目构建设置中设置平台目标时,我通常会得到糟糕的图像格式。

所以如果留在任何CPU上,系统是x64,如果我使用一些32位的依赖库。服务将尝试以x64运行,但由于此32库而失败。因此,在我们的案例中,解决方案是将平台目标设置为x86。

最新更新