471,582 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,582 software developers and data experts.

Run a .Net Service as a Console App to Debug

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.
Jan 12 '06 #1
3 2221
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).TotalMillise conds);
#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

JohnH wrote:
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.

Jan 12 '06 #2
"JohnH" <Jo***@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.com...
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(ServicesToRu n);
#endif

return;
}
-- Alan
Jan 12 '06 #3
"JohnH" <Jo***@discussions.microsoft.com> wrote in message
news:71**********************************@microsof t.com...
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.
--
Jan 14 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Dmitri Shvetsov | last post: by
9 posts views Thread by Hardy Wang | last post: by
7 posts views Thread by BW | last post: by
4 posts views Thread by hoeriksen | last post: by
7 posts views Thread by Larry Bird | last post: by
2 posts views Thread by MATT | last post: by
12 posts views Thread by Anil Gupte | last post: by
4 posts views Thread by emanuelanechei | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Vinnie | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.