How to make windows service application so it can run as a standalone program as well?

In C#, an easy way to do it is to require a command line argument to run it as a service. If the argument isn't there, then run your form/console app. Then just have your installer include the argument in the executable path when installing the service so it looks like so.

In C#, an easy way to do it is to require a command line argument to run it as a service. If the argument isn't there, then run your form/console app. Then just have your installer include the argument in the executable path when installing the service so it looks like so: C:\MyApp\MyApp.

Exe -service It would look something like this: static void Main(string args) { foreach (string arg in args) { //Run as a service if our argument is there if (arg.ToLower() == "-service") { ServiceBase servicesToRun = new ServiceBase { new Service1() }; ServiceBase. Run(servicesToRun); return; } } //Run the main form if the argument isn't present, like when a user opens the app from Explorer. Application.

Run(new Form1()); } This is just an example to give you an idea, there are probably cleaner ways to write this code.

That's what I was looking into before, but I couldn't find a way to pass an argument to installutil so that service would start with some argument. Another way to do it would be to require argument to run interactively, but that's not so handy. See my own answer to see how I did it in the end.

Very simple. – mr.b Jun 28 '10 at 1:00 Anyhow, can you tell me how to pass argument to installutil, so that I know in future? – mr.b Jun 28 '10 at 1:00.

After some digging, I have finally looked under . NET hood (System.ServiceProcess.ServiceBase. Run method), only to find that it checks Environment.

UserInteractive bool to make sure that executable is NOT run interactively. Oversimplified solution that works for me: class Program { static void Main(string args) { if (!Environment. UserInteractive) { ServiceBase ServicesToRun; ServicesToRun = new ServiceBase { // Service.OnStart() creates instance of MainLib() // and then calls its MainLib.Start() method new Service() }; ServiceBase.

Run(ServicesToRun); return; } // Run in a console window MainLib lib = new MainLib(); lib.Start(); // ... } }.

Beware: Environment. UserInteractive is only used to check if there is a graphical desktop session and the result is only used to determine whether to show a dialog or a console message. UserInteractive will also return true when running as a service that is allowed to interact with the desktop.

The real check is done within native methods. – Ishmaeel Oct 13 '10 at 12:17 @Ishmaeel: thanks, good point. Didn't know these two modes were related.Do you have an example of native methods that can check for that?

– mr.b Oct 13 '10 at 22:57 Apparently, ServiceBase just calls StartServiceCtrlDispatcher function (msdn.microsoft. Com/en-us/library/ms686324(VS.85). Aspx) and a nonzero return value (probably) means you are not launched as a service.

Of course, it's not useful as a check because it is the very operation that you want to decide whether to execute or not. I haven't been able to find a proper check function yet. – Ishmaeel Oct 14 '10 at 10:22.

You should really have all of your functionality abstracted in a library. The fact that it happens to be run from a Windows Service should not matter. In fact, if you had a faced class called ServiceFrontEnd that had a Start() and Stop() - the Windows Service application could call that, and so could a command-line app, a windows app, or whatever.

What you're describing here just needs more abstraction. The functionality of "the service" doesn't need to be tightly-coupled to how a Windows Service happens to operate. Hope that helps.

What I have right now is a library that contains all logic. I invoke same library from standalone console application and from service application. What I am looking for is a way to merge two executables into one.So, Console.

Exe and Service. Exe both call Library. Dll, and I want to have single ConsoleAndService.

Exe, that calls Library. Dll :) – mr.b Jun 27 '10 at 23:50 The entry-point for a console application is a static Main() method. The entry-point for the service is a class that inherits from ServiceBase - and the OnStart() and OnStop() are called.

There should be no conflict. But empirically if you find that there is, I don't think it's unreasonable to have separate executables. After all, they will run in entirely different ways.

Hope that helps – Robert Seder Jun 27 '10 at 23:57 Of course, it's perfectly acceptable to have separate executables. I was just looking for a way to merge them if possible. If not, well, it will continue to work as it already does.

– mr.b Jun 28 '10 at 0:18.

In the example you site, I'm pretty confident the Apache app is written in C or C++. For that, you would need a ServiceMain function. If you execute it like a normal program, main gets called.

If you point the service control manager at it, ServiceMain gets called instead. Regarding C#, can't say I know about that. If I had to write a service in c#, I suppose I would start here - msdn.microsoft.com/en-us/library/bb48306....

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions