Wednesday, March 30, 2011

This easiest way to debug a window service

Problem: Cannot start service from command liner or debugger. A window service must first in stalled (using install.exe) and then start with the Server Explorer, Windows Service Administrative tool or the Net START command.

Solution:

1. Create a public method and call OnStart method of service in this as shown.

public partial class Service2 : ServiceBase

{

public Service2()

{

InitializeComponent();

CanPauseAndContinue = true;

}

public void myMethod()

{

string[] myArt = new string[] { "one", "two", "three" };

this.OnStart(myArt);

}

//protected override void OnStart(string[] args)

protected override void OnStart(string[] args)

{

// TODO: Add code here to start your service.

}

}

2. Call this public method in Main.

static void Main()

{

ServiceBase[] ServicesToRun;

(new Service2()).myMethod(); // allows easy debugging of OnStart()

}

No comments:

Post a Comment