CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2006
    Location
    California, U.S.
    Posts
    13

    Question Debugging a Service

    Hi Folks,

    I'm writing my first windows service, and am having some difficulty debugging. Based on this article...

    http://msdn.microsoft.com/en-us/libr...b3(VS.80).aspx

    ...I understand I can't debug directly from the IDE, but even after installing the service and attaching to the process my breakpoints don't hit. I know the service is running and going through my functions because I'm writing a log and can see things happening there. It would be nice, though to take advantage of the IDE's debugging features. I'm using Visual Studio 2008 Standard, .NET 3.5.

    Thanks!

  2. #2
    Join Date
    Jun 2010
    Posts
    85

    Re: Debugging a Service

    If you get clever with partial classes and some compiler directives you can make it so that in debug mode you can run as a process not a service, it makes it much easier to debug, you just have to remember to follow the rules of a service (no ui's etc)...

    Here is an example

    Code:
    #if DEBUG
        public partial class MyService
        {
            [DllImport("Kernel32.dll")]
            static extern void AllocConsole();
            static MyService()
            {
                AllocConsole();
            }
    
            public void Start(string[] args)
            {
                try
                {
                    SafeStart();
                    Console.WriteLine("Press any key to exit");
                    Console.ReadKey();
                    Stop();
                }
                catch (Exception ex)
                {
                    //Do something with exception...
                }
            }
    
            public void Stop()
            {
                try
                {
                    SafeStop();
                }
                catch (Exception ex)
                {
                    //Do something with exception...
                }
            }
        }
    #else
        public partial class MyService
            : ServiceBase
        {
            protected override void OnStart(string[] args)
            {
                base.OnStart(args);
                try
                {
                    SafeStart();
                }
                catch (Exception ex)
                {
                    //Do something with exception...
                }
    
            }
    
            protected override void OnStop()
            {
                base.OnStop();
                try
                {
                    SafeStop();
                }
                catch (Exception ex)
                {
                    //Do something with exception...
                }
            }
        }
    #endif
    
        public partial class MyService
        {
            private void SafeStop()
            {
                throw new NotImplementedException();
            }
    
            private void SafeStart()
            {
                throw new NotImplementedException();
            }
        }
    
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main(string[] args)
            {
    #if DEBUG
                (new MyService()).Start(args) ;
    #else
                ServiceBase.Run(new ServiceBase[] { new  MyService() });
    #endif
            }
        }
    Last edited by [email protected]; July 23rd, 2010 at 04:58 PM.
    --------------------------------------------------------------------------------------------------------------------------
    Disclaimer - Most likely any code I have posted as an answer was most likely written free hand and may have some minor compile errors, and is merely intended to give you the idea.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Debugging a Service

    To help debugging, I find that you just moving the implementation out of the ServiceBase derived class is a simple approach.

    Code:
    public partial class MyService : ServiceBase
    {
      // MyServiceImpl implements actual service functionality
      private MyServiceImpl _myServiceImpl = new MyServiceImpl( );
    
      public MyService ( )
      {
        InitializeComponent();
      }
    
      // Delegate all calls to MyServiceImpl
    
      protected override void OnStart(string[] args)
      {
        _myServiceImpl.OnStart( args );
      }
    
      protected override void OnStop( )
      {
        _myServiceImpl.OnStop( );
      }
    
      //
      // Other services methods delegated
      //
    }
    }
    Then the implementation class can be instantiated directly in debug mode:

    Code:
    private static void Main(string[] args)
    {
      // If 'debug' or '/debug' command line switch specified, start in debug mode.
      if (Utility.IsDebugMode(args))
      {
         // Run as a Console-hosted process
         var myServiceImpl = new MyServiceImpl( );
         myServiceImpl.OnStart( args );
      }
      else
      {
        // Run as a Windows Service
        ServiceBase.Run(new ServiceBase[] {new MyService()});
      }
    }
    Note: the IsDebugMode method just checks the command line for the /debug switch.

    This approach works well for debugging. About the only thing it can't debug is security errors due to a service running under a different account. It's pretty easy to track these errors down though. If the code works in debug mode, but fails when started as a service, then change the service startup account to use the same account you debugged with and restart the service. If the service works under this account, then you know you have a permissions issue. When debugging services I like to separate the code issues from security issues related to different accounts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured