Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I will be create the Windows Service to reading the data from the OPC server and update the data to the MySql database.

It will be connect to the OPC server, but it will not write anything to the database.
Any one known how to write the database through Windows Service? In this service the data change handler callback method used to catch the data changed value from the OPC server is it works.

Tell me how to debug the Windows Service?
Posted
Updated 16-May-11 0:30am
v2
Comments
Manfred Rudolf Bihy 16-May-11 6:18am    
Please clarify what you're on about.

1 solution

Yes, debugging Windows Service is hard. I use two approaches.

1) You can use "Attach Debugger" feature. You can run the Service in a regular way and later attach the debugger to existing process. See Debug menu under main Visual Studio menu. It's hard to debug the very start of the service (OnStart). One trick is to insert big delay using System.Threading.Thread.Sleep in the very beginning, some time needed to attach the debugger.

2) I developed dual-mode application (library) which can run as interactive application or as Windows Service. Here is the key: during run-time, you can read the property System.Environment.UserInteractive (http://msdn.microsoft.com/en-us/library/system.environment.userinteractive.aspx[^]) to act accordingly. In this way, most of the debugging can be done using interactive mode. The problem is: the mechanism of dual application can be well debugged in the very beginning. You can develop as a separate .NET library and debug it on some most simple service code.

Additional advice: use logging extensively. Use System.EvenLog to log in the Windows System Log. As I use dual-mode application, I also use dual-mode logging (second mode is logging into some UI control). However EventLog using System log is much more reliable.

See my past answers for useful code samples:

Custom logger:
MsBuild OutPut to the TextBox on the fly in Windows Application[^].

Logging with System Log structure:
How to create event log under a folder[^].

—SA
 
Share this answer
 
v4
Comments
Albin Abel 16-May-11 6:24am    
Very good points. Point 2 should be a better option than the first point. Event log is another better pulse meter. My 5
Sergey Alexandrovich Kryukov 16-May-11 6:31am    
Thank you, Albin.

Frankly, I recently developed a most complex service I've ever did before (also, pretty bad client of my partner's company was involved), but... we debugged everything using just interactive mode (interesting dual-mode concept, I always wanted to developed it but never got a good chance before). So, only interactive-mode debugging but of course good deal of logging as well. Without logging it would be too hard.

--SA
vrushali katkade 16-May-11 6:34am    
which process i will be attach because there is no name of my service in process list
Sergey Alexandrovich Kryukov 16-May-11 6:42am    
There is a name. You need to start process. Did you do it? Successfully?
--SA
vrushali katkade 16-May-11 6:57am    
i will be run the service successfully, but it will not display the process name ,how to start the process

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900