1

I am learning basics of windows service. I have created a very simple one.

using System.ServiceProcess;

namespace WindowsServiceBasic
{
    public partial class OmerService : ServiceBase
    {
        public OmerService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            System.Diagnostics.Debugger.Launch();
            WriteLog("START");
        }

        protected override void OnStop()
        {
            System.Diagnostics.Debugger.Launch();
            WriteLog("STOP");
        }

        private void WriteLog(string durum)
        {
            eventLog1.WriteEntry(performanceCounter1.RawValue.ToString());
        }
    }
}

using System;
using System.IO;
using System.ServiceProcess;

namespace WindowsServiceBasic
{
    internal static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        private static void Main()
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new OmerService()
            };
            ServiceBase.Run(ServicesToRun);
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            if (e != null && e.ExceptionObject != null)
            {
                string createText = e.ToString();
                File.WriteAllText(@"c:\omerlog.txt", createText);
            }
        }
    }
}

The first time my service (AServis) starts successfully but when I click the restart it crashes. Since my service is very simple It should have been worked properly. I try to log the error, put try catch but I could not find anything. I am trying to attach process, it debugs stop event but after stop debug suddenly finishes and start process crashes. Could you please help me what is the reason and how can I debug and log error.

Thanks in advance

enter image description here

Omer K
  • 5,196
  • 10
  • 53
  • 73

2 Answers2

1

I saw that it was stuck in

public OmerService()
{
    InitializeComponent();
}

I could see the issue adding System.Diagnostics.Debugger.Launch(); statement.

public OmerService()
{
    System.Diagnostics.Debugger.Launch();
    InitializeComponent();
}
Omer K
  • 5,196
  • 10
  • 53
  • 73
0

The standard trick I use in this situation is to add a call to System.Diagnostics.Debugger.Break in my start up code. Now, when you start the service as normal (through the Service Control Manager (SCM)), the call to the Break will cause Windows to launch the JIT debugger, which should prompt you to choose the debugger you wish to attach to the process (e.g., Visual Studio), which will then enable you to debug your code as normal.

Also see this: Easier way to debug a Windows service.

Community
  • 1
  • 1
Polyfun
  • 9,089
  • 4
  • 29
  • 39
  • I added System.Diagnostics.Debugger.Launch(); But again it breaks on first start but after stopping and retrying start it is crashing and even don't breaks System.Diagnostics.Debugger.Launch(); Could you help further please. I suspect performanceCounter1 causes the error. But how can I debug this? – Omer K Jul 16 '15 at 09:29
  • Have you put the Launch call in your service's OnStart method? – Polyfun Jul 16 '15 at 09:39
  • Yes, You can find my edited post. It enters the onstart on the first call but doesn't enter after stop and start. – Omer K Jul 16 '15 at 09:49