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)
2: {
3: Start();
4: }
5:
6: public void Start()
7: {
8: //Start!
9: }
And this goes in Program.cs.
1: namespace UdpListener
2: {
3: static class Program
4: {
5: /// <summary>
6: /// The main entry point for the application.
7: /// </summary>
8: static void Main()
9: {
10: #if (!DEBUG)
11: ServiceBase[] ServicesToRun;
12: ServicesToRun = new ServiceBase[]
13: {
14: new UdpListener()
15: };
16: ServiceBase.Run(ServicesToRun);
17: #else
18: UdpListener listener = new UdpListener();
19: listener.Start();
20: #endif
21: }
22: }
23: }
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
It helped me. Thanks.
ReplyDeleteGoood one short and useful!
ReplyDeleteSuper.. It's useful for me..
ReplyDeleteI have window 7 ,if i am starting a setup same error is occuring ...please help ,,,
ReplyDeleteAwesome... Thanks!
ReplyDeleteAnyone know how to do this in VB?
ReplyDeleteWhich parts are hard to translate?
DeleteError on .Start()
ReplyDeleteMy Code:
HKWFService listener = new HKWFService(); //my service file name
listener.Start();
What's the error?
DeleteThis comment has been removed by the author.
ReplyDeletevery Useful
ReplyDeleteI try to call my application through a scheduler.
ReplyDeleteAnd I still get this error.
Cisco Tidal Scheduler to be precise.
Delete