【问题标题】:Unable to start C# windows service from my PC无法从我的 PC 启动 C# windows 服务
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

  • 我尝试使用 x32 和 x64 版本,但我又遇到了这个错误。
猜你喜欢
  • 2015-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多