The trick my team and I use to workaround this problem, makes use of the service Debug flag. If the Debug flag is on, we just start the service by using our own public Start method. When the OnStart event is fired in the service itself, we call the same public Start method.
This goes in your service.
1: protected override void OnStart(string args)
6: public void Start()
And this goes in Program.cs.
1: namespace UdpListener
3: static class Program
5: /// <summary>
6: /// The main entry point for the application.
7: /// </summary>
8: static void Main()
10: #if (!DEBUG)
11: ServiceBase ServicesToRun;
12: ServicesToRun = new ServiceBase
14: new UdpListener()
18: UdpListener listener = new UdpListener();
You can set the Debug flag of your service in your service properties.
The only problem with this solution is that you can't debug your OnStop event but this hasn't been an issue for us so far.
Other ways to solve this issue can be found here:
- MSDN: How to: Debug Windows Service Applications
- KB824344: How to debug Windows services
- MSDN: How to: Launch the Debugger Automatically