RedEagle
RedEagle

Reputation: 4560

Windows Service Debug tedious

I'm building my first windows service... what an adventure.

To get me started I followed the following walktrough:

http://msdn.microsoft.com/en-us/library/zt39148a.aspx#Y902

It's very well structured and it works!... Meaning that I get the In onStart and In onStop log messages on the created Event Log.

Now, my question is this:

Every time I change the service code I need to rebuild the Setup Project, add the Service project as the Primary Project Output and reinstall the service.

This is quite tedious so I ask if there isn't a better way to test and debug the service.

Best Regards

Upvotes: 0

Views: 211

Answers (2)

Web
Web

Reputation: 1745

Sometimes what I do if I just want to test some code in a windows service without needing it to run as a service I will modify the Main().

So instead of my Main being like this :

ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] 
{ 
    new Service1() 
};
ServiceBase.Run( ServicesToRun );   

I modify it just to

Service1 service = new Service1();
service.Run();
// Put a breakpoint on the following line to always catch
// your service when it has finished its work
System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

And lets say that my OnStart method looks something like this:

protected override void OnStart(string[] args)
 {
     // code to start your service.
     ThreadStart ts = new ThreadStart(Run);
     Thread t = new Thread(ts);
     t.Start();
}

So the Run() is just the method that your OnStart method starts.

Remember that this not a guarantee that your service will work correctly once installed as a service. But sometimes its useful to quick test some code.

Upvotes: 1

Larry Osterman
Larry Osterman

Reputation: 16142

Once your service is stopped, you should be able to update the binary simply by copying the new version over the old version.

Upvotes: 5

Related Questions