views:

778

answers:

3

Hi,

I'm trying to deploy a windows service but not quite sure how to do it right. I built it as a console app to start with, I've now turned it into a windows service project and just call my class from the OnStart method in the service.

I now need to install this on a server which doesn't have Visual Studio on it, which if I've understood it correctly means I can't use the InstallUtil.exe and have to create an installer class instead. Is this correct?

I did have a look at a previous question, http://stackoverflow.com/questions/255056/install-a-net-windows-service-without-installutil-exe, but I just want to make sure I've understood it correctly.

If I create the class that question's accepted answer links to, what is the next step? Upload MyService.exe and MyService.exe.config to the server, double click the exe file and Bob's my uncle?

The service will only ever be installed on one server.

Thanks,

Annelie

A: 

Not double click, you run it with the correct command line parameters, so type something like MyService -i and then MyService -u to uninstall it`.

You could otherwise use sc.exe to install and uninstall it (or copy along InstallUtil.exe).

ho1
Thanks, I'll try to use sc.exe if I can locate it, I'll update when I know if it works.
annelie
+2  A: 

Why not just create a setup project? It's really easy.

  1. Add a service installer to the service (you do it on the seemingly useless service "design" surface)
  2. Create a setup project and add the Service output to the setup app folder
  3. Most importantly add the Service project output to all the custom actions

Voila, and you're done.

See here for more: http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx

There is also a way to prompt the user for credentials (or supply your own).

Doobi
Thanks, I'll have a look at this if I can't get the other suggestions to work. Not sure what credentials you mean?
annelie
With credentials he means the account which the service runs under.
ho1
@ho Which account should the service run under? Basically the service watches some folders for new files, uploads files to a database if there's new files then moves the files to a different folder.
annelie
+1  A: 

The InstallUtil.exe tool is simply a wrapper around some reflection calls against the installer component(s) in your service. As such, it really doesn't do much but exercise the functionality these installer components provide. Marc Gravell's solution simply provides a means to do this from the command line so that you no longer have to rely on having InstallUtil.exe on the target machine.

Here's my step-by-step that based on Marc Gravell's solution.

http://stackoverflow.com/questions/1195478/how-to-make-a-net-windows-service-start-right-after-the-installation/1195621#1195621

Matt Davis
Thanks, I'm almost there with this. I get a security exception saying 'The source was not found, but some or all event logs could not be searched. Inaccessible logs: Security'. I'm guessing this might have to do with the account it's running under as @ho is talking about in his comment. I've set it to LocalService, but I'll change that and try again. By the way, tiny spelling mistake in your code, for the InstallService() it says 'IDictionary state = new Hasttable();' rather than Hashtable.
annelie
Fixed it. Thanks.
Matt Davis
No probs. Right, I've tried with all the different accounts, all except User gives the same exception, and for User I have to enter username and password. Which username and password should that be? For the user I'm logging in to the server with?
annelie
I managed to install it! I use the LocalSystem account, had to run as administrator when opening the command prompt for it to work. Now I need to figure out why the service is stopping as soon as it starts, but that's another question. :) Thanks!
annelie
In the constructor of your Windows service class (or in the `OnStart()` event handler), put a call to `System.Diagnostics.Debugger.Break()`. When you start your service, you'll be prompted to enter a debug session. You can debug from there. I think you have to have admin rights on your machine for this to work, though.
Matt Davis
Thanks, I've debugged and there's a timeout exception being thrown in the StartService method. Any ideas why that could be?
annelie
Found the problem. Those damn infinite loops... :)
annelie