473,233 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,233 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 2289
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dmitri Shvetsov | last post by:
Hi All, Did somebody see the situation when the VS refuses to debug the Web Service at all? I can't catch why, the initially created Web Service can be debugged very easy but after some changes...
9
by: Hardy Wang | last post by:
Hi all: I read an article from http://www.c-sharpcorner.com/Code/2003/Sept/InstallingWinServiceProgrammatically.asp about how to install a windows service programmatically. Based ont the code...
7
by: BW | last post by:
Hi all! I'm not sure where to post this so I'm giving these a try. If there is a better (more appropriate) place to post please let me know. I want to create a service (in C#). The service's...
4
by: hoeriksen | last post by:
I'm trying to create a Windows service which can also be run as a console app if it is started with some switch (eg -console). Is there any way to do this? Previously (in VC6) it's just been a...
7
by: Larry Bird | last post by:
I have a windows service that want to un-install. When I run "installutil /u serivcename" I get the error that the serivce is not installed on my machine. However, when view the serivce console I...
2
by: Joe | last post by:
In all of my C#/Net 1.1 services (VS2003), I always have dual mode functionality. By passing a specific command line argument, the service would run in Console mode as a Console App instead of as...
2
by: MATT | last post by:
I am trying to create a windows service. The part I am having trouble with is writing text to a log file. I am using a very basic StreamWriter function to try to test this. I have created a...
12
by: Anil Gupte | last post by:
I wrote my Windows Service first as a regular Windows Exe because it is easier to debug. In that I used AppDir = Application.ExecutablePath.Substring(0,...
4
by: emanuelanechei | last post by:
These days i'm creating a windows service in c# which reads some data from a file and saves it in a database. The data in the file changes every minute. I am using FileSystemWatcher object which...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.