Combining a service and an application

K

K Viltersten

I've developped a service as follows. I'd like to have it
that way PLUS be able to run it as a normal application
(see the commentin Main). Any pointers/suggestions on
how to get there smoothly?

class MyService : ServiceBase {
public static void Main() {
// Should i detect service/application and go:
// if(isService)
// ServiceBase.Run(new MyService());
// else
// doExecuteAnOtherMethod();

ServiceBase.Run(new MyService());
}
public MyService() {
this.ServiceName = "";
this.CanStop = true;
this.AutoLog = true;
}
protected override void OnStart(string[] args) {
base.OnStart(args);
_mySoftware = new MySoftware();
}
protected override void OnStop() {
base.OnStop();
_mySoftware = null;
}
private MySoftware _mySoftware;
}
 
P

Peter Morris

In mine I just use

#if DEBUG
#else
#endif

At debug time I get a console app, but creating a RELEASE build gives me a
service to install.
 
K

K Viltersten

In mine I just use
#if DEBUG
#else
#endif

At debug time I get a console app, but creating a RELEASE build gives me a
service to install.

Ah, that will work. However, of pure curiosity
i wonder if it's possible to detect if it's a service
or application that is being run.

It could be useful in case i'd like to develop a
service that may be run "manually" as an
application. Could come in handy, some day.
 
J

Jeroen Mostert

K said:
I've developped a service as follows. I'd like to have it
that way PLUS be able to run it as a normal application
(see the commentin Main). Any pointers/suggestions on
how to get there smoothly?

class MyService : ServiceBase {
public static void Main() {
// Should i detect service/application and go:
// if(isService)
// ServiceBase.Run(new MyService());
// else
// doExecuteAnOtherMethod();
Use Environment.UserInteractive to distinguish between a regular run and a
service run. This is not 100% (technically you can have non-interactive
processes that are not services) but it's close enough to be practical.

If not running as a service, call .OnStart(), wait for some termination
signal (like a keypress) and call .OnStop().
 
D

Doug Forster

We just do it with a command line switch. We just set the switch permanently
in the VS debug project and in a desktop shortcut if needed there.

Cheers
Doug Forster
 
C

Charles Calvert

I've developped a service as follows. I'd like to have it
that way PLUS be able to run it as a normal application
(see the commentin Main). Any pointers/suggestions on
how to get there smoothly?

See <http://www.codeproject.com/KB/dotnet/DebugWinServices.aspx>,
which is the same suggestion that Peter Morris made, with a more
detailed example. Also, if you scroll down the page and look at the
comment by "sstreaker", they suggest another way that lets you do it
from the command-line or debugging via the IDE.
 

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