Windows Service Debug process (Attach to process debugging, and boot time cannot exceed 30 seconds)

Source: Internet
Author: User

The first time I wrote a Windows service in C # in the last few days, the content of the implementation is relatively simple. is to start the remoting connection, but debugging relative to the first write Windws service for me, more annoying. No experience, and there is no way to set breakpoints like debugging other Windows programs, and you cannot see the running process. After reviewing some relevant information, there is a little bit of debugging experience. A pen is hereby reserved for future use.

Related Source:

static void Main ()
{
Servicebase[] ServicesToRun;

Multiple User services can be run in the same process. To add
Another service is added to this process, change the downstream to
Create another service object. For example
//
ServicesToRun = new servicebase[] {new Service1 (), New MySecondUserService ()};
//
ServicesToRun = new servicebase[] {new Teamworldservice ()};

Servicebase.run (ServicesToRun);
Teamworldservice obj = new Teamworldservice ();
Obj. OnStart ();
}



Teamworldservice:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Diagnostics;
Using System.ServiceProcess;
Using System.Text;
Using System.IO;
Using Tribal.TeamWorld.API;
Using Tribal.baseclasses;
Using Tribal.TeamWorld.Remoting;
Using Tribal.TeamWorld.Implementation;

Namespace Tribal.TeamWorld.Service
{
Partial class Teamworldservice:servicebase
{
Private Remotinginterface _remotinginterface = null;
Private Maincontroller _maincontroller = null;
Public Teamworldservice ()
{
InitializeComponent ();//

}

protected override void OnStart (string[] args)
{
This. Addtextline ("Starting server (" + DateTime.Now.ToString () + ")");

Try
{
This. Addtextline ("Initializing maincontroller");
This._maincontroller = Maincontroller.getinstance ();
}
catch (Exception ex)
{
This. Printexceptions (ex);
}

if (This._maincontroller! = null)
{
This. Addtextline ("Connecting UserInterface to Logcontroller");
}

Try
{
This. Addtextline ("Starting Maincontroller");
This._maincontroller.start ();
}
catch (Exception exc)
{
This. Printexceptions (EXC);
}

Try
{
This. Addtextline ("Starting. NET remotinginterface");
This._remotinginterface = new Remotinginterface ();
This._remotinginterface.startserving ();
}
catch (Exception exc)
{
This. Printexceptions (EXC);
}
}

protected override void OnStop ()
{

This._remotinginterface.stopserving ();
This._maincontroller.stop ();
This. Addtextline ("End. NET remotinginterface");

}

private void Printexceptions (Exception exc)
{
Exception current = exc;
while (current! = null)
{
This. Addtextline (current. Message);
This. Addtextline (current. StackTrace);

Current = current. innerexception;
}
}


private void Addtextline (string line)
{
Try
{
FileStream fs = new FileStream (@ "C:\TeamWorldServiceLog.txt", FileMode.OpenOrCreate, FileAccess.Write);

StreamWriter m_streamwriter = new StreamWriter (FS);

M_streamWriter.BaseStream.Seek (0, Seekorigin.end);

M_streamwriter.writeline (line+ "\ r \ n");

M_streamwriter.flush ();

M_streamwriter.close ();

Fs. Close ();
}
catch (Exception ex)
{

}
}
}
}


Method 1: Write the log
is the most traditional debugging Windows service method, but also the way you debug the service, but it is not very clear. You have to add a way to write the log in places where you think the error may have occurred. The code above uses this method, which is implemented by the Addtextline function.

Method 2: Attach the process
The method of attaching a process can set breakpoints to step through the same way as if you were debugging a normal widows program. However, I will not be able to attach this service process until the boot service is installed, and the OnStart function will be executed at the same time as the add-on, so the onstart cannot be debugged. But I can load debugging by setting the start service delay.
The steps are as follows:
1, set the start service delay,

Private System.Timers.Timer Timerdelay;

protected override void OnStart (string[] args)
{
Try
{

Delay Start the Syndata 30seconds
Timerdelay = new System.Timers.Timer (30000);
timerdelay.elapsed + = new System.Timers.ElapsedEventHandler (timerdelay_elapsed);
Timerdelay.start ();
}
catch (Exception ex)
{
This. Printexceptions (ex);
}
}

void Timerdelay_elapsed (object sender, System.Timers.ElapsedEventArgs e)
{
timerdelay.enabled = false;
Timerdelay.close ();
The code you want to add
//. To do.
}

Note:Normal service start time is about 30 seconds, when the service start time more than 30 seconds will be an error! , so do not do too much in the onstart, or you can start the service with this time-lapse method in case the service is started.
2, first to install the service, and then start the service.
3. Open vs2005 debugging, attach to process, select your service process (if you can't find a process that shows all users), you can.

Method 3:
I think this debugging is the most helpful to me.
In the main function, comment out the original automatically generated code, note that the red Word is to be based on their own service name to manually add
//servicebase[] ServicesToRun;

// Multiple User services can be run in the same process. To add
//Another service is added to this process, change the downstream to
// Create another service object. For example,
//
            //   ServicesToRun = new servicebase[] {new Service1 (), New MySecondUserService ()};
//
ServicesToRun = new servicebase[] {new teamworldservice ()};

//Servicebase.run (ServicesToRun);
//******************************************
 Teamworldservice obj = new Teamworldservice ();
Obj. OnStart ();
//******************************************
the protected override void OnStart (string[] args) is then changed to public void OnStart ().
, set your breakpoint, press F5 to run it and debug it.

These are some of the methods that I get after this commissioning widows service. To continue to accumulate in the future.

Http://www.cnblogs.com/peak-weng/archive/2008/05/30/1210538.html

Windows Service Debug process (Attach to process debugging, and boot time cannot exceed 30 seconds)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.