Run a .Net Service as a Console App to Debug

G

Guest

Does any one now how to create a .net service thet when run from the command
line with say a -D argument will run as a console app and be able to output
to that console window. The same .exe would start as a normal service wihout
the -D argument and require use of the NET START command or other.
 
M

MJB

John,

Have you tried debugging your service by attaching to the process? I
understand your frustration in not being able to see what is going on
with your service.

One thing I like to do is to put this bit of code in my OnStart(string[]
args) method

//Sleep so that the process can be attached to
protected override void OnStart(string[] args)
{
//Sleep so that the process can be attached to
#if DEBUG
//Sleep 10 secs
Thread.Sleep(TimeSpan.FromSeconds(10).TotalMilliseconds);
#endif

//Place a break point here
DoWork();
}

Now, you have time to attach to the service process and can step through
it the way I think you want to.

p.s. you can also pass args through the OnStart(string[] args) if you so
choose.

HTH
Matt
 
A

Alan Pretre

JohnH said:
Does any one now how to create a .net service thet when run from the
command
line with say a -D argument will run as a console app and be able to
output
to that console window. The same .exe would start as a normal service
wihout
the -D argument and require use of the NET START command or other.

This may be enough to get you started. This would allow a service to start
up as a form as well. This is a lot easier to debug than attaching to a
running service, especially for the startup code. Haven't tried the console
one but have often used the WinForm one during development of services.


// Choose one or none.
//#define FORM
#define CONSOLE

public static void Main(string[] Args) {
#if FORM
// Service running as a form.
MyService Service = new MyService();
Service.OnStart(Args);
System.Windows.Forms.Application.Run(new
System.Windows.Forms.Form());
Service.OnStop();
#elseif CONSOLE
// Service running in the console.
MyService Service = new MyService();
Service.OnStart(Args);
// Presumably OnStart() has initialized _Stop and spun off a worker
thread or something.
while (!_Stop) Sleep(10);
Service.OnStop();
#else
// Normal service.
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] {new
MyService()};
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#endif

return;
}


-- Alan
 
N

Nick Malik [Microsoft]

JohnH said:
Does any one now how to create a .net service thet when run from the
command
line with say a -D argument will run as a console app and be able to
output
to that console window. The same .exe would start as a normal service
wihout
the -D argument and require use of the NET START command or other.

Hi John,

I usually seperate my app into two assemblies. The service harness simply
checks the config file to see which assembly to load and it runs that
assembly according to the config settings. The second assembly, which has
ALL of the real logic of what I'm trying to do, can be run from any harness.
For testing, I have a U/I harness that uses the same config file but calls
the assembly from a windows form. This allows for simple debugging in dev,
and ease of deployment in production.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top