Previously, I followed the advice in this article by creating a setup program that easily allowed me to install and uninstall the service directly from the Solution Explorer in VS. However I still have to either call System.Diagnostics.Debugger.Break() in my code (and remember to remove it when I'm done!) or attach to the process in order to debug it.
There's an easier way. If you look in the Main entry point of the service you will see how the service is loaded. (In C# this is in the Program.cs file that's created for you and in VB.NET this code resides in the service's Designer.vb file.) You can modify this code by adding a DEBUG compiler directive to control how the process starts:
<MTAThread()> _ Shared Sub Main() #If DEBUG Then ' Start the process as a non-service for debugging only. ' Stop the debugger to stop the process. Dim service As New MyService service.Execute() System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite) #Else Dim ServicesToRun() As System.ServiceProcess.ServiceBase ' More than one NT Service may run within the same process. To add ' another service to this process, change the following line to ' create a second service object. For example, ' ' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService} ' ServicesToRun = New System.ServiceProcess.ServiceBase() {New MyService} System.ServiceProcess.ServiceBase.Run(ServicesToRun) #End If End SubNow when your configuration is set to debug, you can hit F5 to debug like normal. Stop the debugger to stop the process. Keep in mind that the OnStart and OnStop methods of your service will not run so you should break your functionality into a callable method on your service class (in this example, I called it "Execute()"). Have fun!
8 comments:
thanks good way
http://www.guideofnet.com/
Wow Impressive! Your blog is very informative. However, it is pretty hard task but your post and experience serve and teach me how to handle and make it more simple and manageable.Thanks for the tips… Best regards.
whm reseller hosting | reseller web hosting
Clean, clear and very elegant way to debug services. Excellent post. Thank you!!.
Hi! Nice post. But i am getting an error in the following line:
Dim service As New MyService
service.Execute()
the error is: Execute is not a member of Myservice.
So, can you please help me in solving this?
With regards,
satya
This site is amazing and it is helps for students.Thanks for sharing
.Net Online Training
thanks for all this information. You are including better information regarding this topic in an effective way. T hank you so much.
Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery
SSL Certificate are what enable websites to use HTTPS, which is more secure than HTTP.
Our Solusvm Server Management system are designed to deliver everything from installation to migration, optimization and troubleshooting.
Post a Comment