Skip to content

Instantly share code, notes, and snippets.

@ferventcoder
Created August 29, 2011 19:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferventcoder/1179173 to your computer and use it in GitHub Desktop.
Save ferventcoder/1179173 to your computer and use it in GitHub Desktop.
Debugging Windows Services
public partial class CustomService : ServiceBase
{
protected override void OnStop()
{
//normal shutdown code here
}
protected override void OnStart(string[] args)
{
//normal startup code here
}
public void RunConsole(string[] args)
{
OnStart(args);
if ((args.Length > 0) && (Array.IndexOf(args, "/console") != -1))
{
Console.WriteLine("Please press enter to continue...");
Console.ReadLine();
}
OnStop();
}
}
public class ServiceStartRedirect
{
#region Methods
private static void Main(string[] args)
{
if ((args.Length > 0) && (Array.IndexOf(args, "/console") != -1))
{
var service = new CustomService();
service.RunConsole(args);
}
else
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new CustomService()
};
ServiceBase.Run(ServicesToRun);
}
}
#endregion
}
@ferventcoder
Copy link
Author

In the properties of the project, under Debug -> Start Options -> Command Line Arguments: add

/console

@ferventcoder
Copy link
Author

Also make sure in Properties -> Application -> Output type: is set to

Console Application   

and
Startup object: is set to

ServiceStartRedirect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment