1

is there a tool or a method to test my Windows Service? it work compiled fine with Visual Studio 2010.

I use Advanced Installed to create an install package (MSI) but it's fails to start!

cheers

Data-Base
  • 8,418
  • 36
  • 74
  • 98

4 Answers4

4

See this answer by lubos hasko to ease debugging and also ease service installations (which I've done with great success). Adapting log4net and having it log to console in interactive mode is also recommended.

class TheService : ServiceBase
{
   static void Main(string[] args)
        {
            if (!Environment.UserInteractive)
            {
                Run(new TheService());
            }
            else
            {
                // If interactive, start up as a console app for easy debugging and/or installing/uninstalling the service
                switch (string.Concat(args))
                {
                    case "/i":
                        ManagedInstallerClass.InstallHelper(new[] { Assembly.GetExecutingAssembly().Location });
                        break;
                    case "/u":
                        ManagedInstallerClass.InstallHelper(new[] { "/u", Assembly.GetExecutingAssembly().Location });
                        break;
                    default:
                        Console.WriteLine("Running service in console debug mode (use /i or /u to install or uninstall the service)");
                        var service = new TheService();
                        service.OnStart(null);
                        Thread.Sleep(Timeout.Infinite);
                        break;
                }
            }
        }
   }
Community
  • 1
  • 1
Oskar Duveborn
  • 2,189
  • 16
  • 20
1

Do you have any logging in your application? This would probably be the first place to check for a way to fix it. It's pretty difficult to have 'a tool' to test 'some windows service'. If you have more details from logging and you can't figure out what's wrong, let it know so we might be able to help.

1

You cannot directly run a Windows service: you have to install the service and start it.

As installing a service tends to be inconvenient when developing it'll be worth modifying the bootstrap code for your service to detect whether it is being run as a service or interactively and, in the latter case, show a Windows form.

ServiceBase service = ...;

if (Environment.UserInteractive)
{
    // run as application
    Application.EnableVisualStyles();
    Application.Run(new SomeForm()); // the form should call OnStart on the service
}
else
{
    // run as service
    ServiceBase.Run(service);
}
Paul Ruane
  • 37,459
  • 12
  • 63
  • 82
1

I'm assuming you are using an installer class to create and install the service (derived from System.Configuration.Install.Installer)? If so, put this line of code in the ctr of the installer or OnBeforeInstall override. You can then attach a debugger, and debug the installation process:

System.Diagnostics.Debugger.Break()
dashton
  • 2,684
  • 1
  • 18
  • 15