xeso
xeso

Reputation: 61

Unable to start C# windows service from my PC

I try to start my own Windows service on C# based. I try to run with sc.exe create Service.exe but when I want to run this service I have bugs #1026 (Description: The process was terminated due to an unhandled exception.) and #1000. (Faulting application name: Timesync.exe, version: 1.0.0.0, time stamp: 0xf1683f8e Faulting module name: KERNELBASE.dll, version: 10.0.22621.674, time stamp: 0x160a2aa8) Now I try to this service with InstallUtil.exe but we I can't. Because I have this error: Exception occurred while initializing the installation: System.BadImageFormatException: Could not load file or assembly 'file:Service.exe' or one of its dependencies. The module was expected to contain an assembly manifest.. Here is my code:

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();
                }
            }
        }

Program.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");

      

        }
    }

I can't understand how to resolve my bug and how to run this service. If it possible, please help. :)

Upvotes: 0

Views: 456

Answers (1)

F34R
F34R

Reputation: 127

Any chance you are trying to use 32bit library in x64 solution or vice versa. I usually get badimageformat when I don't set Platform target in project build settings.

So if left on Any CPU and the system is x64, and if I use some dependent library which is 32bit. Service will try to run as x64 but fails because of this 32 library. So in our case solution is to set Platform target to x86.

Upvotes: 1

Related Questions