If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Re: Run console application as windows service

If your console application is really simply and you don't want to trouble of recoding it as a service you can try using a command line application I wrote which can be found here: http://www.RunAsService.com it's a single executable no setup and a single command will have you up and running.

Re: Run console application as windows service

interesting program good work luisperezphd

I could see many good uses for such a program.

I would however like to say there has got to be a way to do this safely and effectively for any program that you make I find that this is a rather interesting question and I hope it gets answered in the manner which it was presented. After all of course it is very possible to run any program as a service with fire deamon or something like this ....

Of course it will work but there is really nothing to be learned from it it is simply using another persons work to accomplish a task so I can see why this may be an issue for someone who is trying to learn how to accomplish something by example. There has been a number of times I too have wanted to run an application as a service and have the service start when windows does.

It used to annoy me that my virus scan along with 10 other programs were running on the task bar but with a bigger screen and higher resolutions and grouped instances of a program introduced in windows xp then tabbed browsing introduced first to me by firefox (while it could have been opera that first had tabbed browsing IDK) This all helped keep less clutter and made stuff a bit easier to find and with the previews of whats running and first the logitech application switcher then the application switcher used by windows 7 10 programs running don't seem like too much clutter anymore not to mention the ability to hide unused icons or always hide specific icons. Windows has came a long way in increasing productivity which is why I have purchased so many copies of windows 7 to support its grand design.

Even still I could see the need for wanting to know how to run a program as a service I am guessing it would have to be done through an installer with the proper rights as an administrator for security reasons. While I may be wrong it is what makes logical since to me.

Re: Run console application as windows service

Hey infriger, you make a good point, my original post suggesting using runasservice is a valid solution if we assume the problem was that the original poster just wanted a quick a dirty way to run as console application as a service.

It doesn't help though if they were trying to learn how to write a service, or convert their application into a service.

I originally created the tool because I think the process of installing a service is not complicated, but more complicated and inflexible than it needs to be. I like that in .NET the applications use xcopy deploy, meaning you can just copy the files and run them. Services don't work that way, they require changes to the registry.

Even worse the default implementation of .NET services in Visual Studio require you to embed the service information like the name of the service into your application, but then require you to use an external application to install it (installutil I think).

Also to debug your service using Visual Studio you have to attach to it.

None of these are big hassles, but just needless, and they add up. Using the tool I created you can just write a console application, which is pretty straight forward, then install it with the ease you would use xcopy.

That said, writing a service isn't very different from any other application. In a console application the Main method gets called when the application is executed, the main method also gets called first on a service, BUT, that is not where you want to put your loop or bulk of your code.

Instead you want to create a separate class that extends from ServiceBase (I think), and override OnStart(). In your Main method you would call:

System.ServiceProcess.ServiceBase.Run(new MyService());

That would be your simplest conversion. A more advance conversion would take advantage of features available to services and redesign the application to take advantage of them. For example the pausing functionality.

There are also other considerations for example the fact that the same resources might not be available to the service that would have otherwise been available to the regular console application. For example access to the files on the desktop. Also there's the fact that the service might not be logged in as the current user.

More than likely you will uncover and resolve these issues in the process of trying to convert it. Note you would come across a lot of these same issues using runasservice.

Re: Run console application as windows service

Hi. While changing your program to explicitly support the Windows Services API is certainly an option, simply launching it from a wrapper may be a better choice in many circumstances (when you don't have control over the code, don't want to deal with the complexities of the API, etc.).

Re: Run console application as windows service

IMO its a pretty useful too though. you don't always have the ability to alter a command line app (say, like some 3rd party utility) to be a windows service, and in instances like that, or where its not cost effective I think a service wrapper app like this is a great idea. similar btw to daemontools in linux which is like a windows services in windows but lets you run command line apps (which windows does not).