0

The code below builds without any errors and ran fine as console application. However now that I've made it into a WindowsService and used the C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil to successfully install it. It stops as soon as I start it up. I feel like I'm missing something simple that I'm going to forever hit myself over the head once another set of eyes looks at it.

Please forgive the mess you are about see. I will clean it up after its working properly. Thanks in advance for any help that can be provided.

Service1.cs

 namespace LGCDialHome
    {
        public partial class Service1 : ServiceBase
        {
            private System.Timers.Timer _timer = null;

            public Service1()
            {
                InitializeComponent();

                System.Timers.Timer stateTimer = new System.Timers.Timer(60000);
            }

            protected override void OnStart(string[] args)
            {
                try
                {
                    EventLog.WriteEntry("Dial Home service started : " + DateTime.Now);
                    _timer = new System.Timers.Timer();
                    _timer.Interval = 10000; //in milliseconds

                    EventLog.WriteEntry("Timer Interval is : " + _timer.Interval);

                    _timer.Elapsed += timer_Elapsed;
                    _timer.Start();
                }

                catch (Exception ex)
                {
                    EventLog.WriteEntry("Dial Home service error : " + ex);
                }

            }

            protected override void OnStop()
            {
                EventLog.WriteEntry("Dial Home service Stopped : " + DateTime.Now);
            }

            protected void timer_Elapsed(object sender, ElapsedEventArgs e)
            {
                int invokeCount = 0;
                int maxCount = 10;
                string host = Environment.MachineName;
                string user = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                string uname = Environment.UserName;

                try
                {
                    _timer.Stop();
                    Console.WriteLine("{0} Checking status {1,2}.",
                    DateTime.Now.ToString("h:mm:ss.fff"),(++invokeCount).ToString());
                    Console.WriteLine("Host -> {0} \r\nNTID -> {1}", host, user);
                    if (invokeCount == maxCount)
                    {
                        invokeCount = 0;
                    }
                }
                catch (Exception ex)
                {
                    EventLog.WriteEntry("Dial Home service error : " + ex);
                }

                finally
                {
                    _timer.Start();
                }
            }

        }
    }
user3308131
  • 149
  • 1
  • 4
  • 14
  • Also check [this](http://stackoverflow.com/questions/125964/easier-way-to-start-debugging-a-windows-service-in-c-sharp) using Debugger.Break() – jeoffman Sep 18 '14 at 15:40

1 Answers1

1

Well, you will need to debug your code, but attaching to Windows Services is a little different than debugging other types of projects.

Try the following:

1- At the very beginning of your OnStart() method, add the following line:

System.Threading.Thread.Sleep(10000);

2- Set a breakpoint on the next line immediately after the line above. Build and reinstall your service using InstallUtil.

3- Start your service.

4- In Visual Studio, click Debug -> Attach to Process.

5- Find your service in the Available Processes list and attach to it. This will cause the breakpoint to be hit, and will let you debug your code to see if there's any exception thrown (by looking at your code, I feel like the problem might be security related. The user that the service is running as might not have access to the EventLog or something).

Note: After starting your service, you have 10 seconds to perform steps 4 and 5. If you think you need more time, change the sleep value to something like 20000(20 seconds).

PoweredByOrange
  • 6,525
  • 9
  • 39
  • 77
  • The local service account was the problem. I changed it and the service started up without issues. Thanks again for pointing me in the right direction. – user3308131 Sep 18 '14 at 16:01