473,395 Members | 2,713 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,395 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 2298
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.