简体   繁体   中英

What's the best way to create .NET Windows service installer?

I just created a simple .NET Windows service. My users are supposed to download it from my site and install it on their computers. So, from various forum posts (including this site) I know that in order to do this I need an installer. Or I can just give them service files and instruct how to install it. I begun with creating a setup project and was able to compile my own .msi installer. But one user complained that he cannot uninstall it now. I researched it and found that .msi format has some issues, mostly related to the way it counts references in GAC. It was very easy to help him to fix his computer, though. From my conversations with users I know that it would be close to impossible to train them to use sc.exe or installutil.exe. I also found a service at http://installer.codeeffects.com that can build an installer for my service without any code but I'm not sure if my installer should be msi or exe. So, clearly I'm in a total confusion here :) Please help guys, any general or detailed advise will be highly appreciated.

Have your service accept an argument to register itself as a service:

MyService.exe /i

In your service, you check for this argument, and if present get the service to register itself:

ManagedInstallerClass.InstallHelper(args);

See my answer to this question for more details. This way, you can get the user to register the service themselves in a simple manner via the command line.

If this is still too complicated for your users, you could use Environment.UserInteractive to check whether or not the user double-clicked on the service. If so, install/uninstall the service. If not, start the service (this will be the route taken when the service starts via the services applet and at machine startup).

Why don;t you use the VS built-in installer? I've been using that for years.

The problem (and the reason why you might think it does not work) is that for a Windows service you have to add some custom actions in order for the service to register.

Have a look at this, and to to the To add a custom action to the setup project section:

http://www.codeproject.com/KB/dotnet/simplewindowsservice.aspx

My suggestion would be the MSI to install the files in the required location, including batch files that execute the installutil.exe. Then Post Installation steps that execute the batch files.

And for uninstall, the same approach - batch files are executed as pre-uninstall step and then the uninstall goes. I had to do something similar before. And I know it is a pain.

Heres a really cool step-by-step guide of how to create a service installer using Visual Studio 2005. (is virtually identical in Visual Studio 2008).

http://aspalliance.com/1316_Working_with_Windows_Service_Using_Visual_Studio_2005.all

It helped me a lot :D

I'm thinking you may want to decouple the service from your application. Have the service call an executable with the actual application logic, and you can maintain your executable re-installing it multiple times without ever having to uninstall/re-install your service portion of the app.

This is coming from a person tired of dealing with the microsoft installer. You may get a much better answer from a seasoned installer guru. I'm not sure I wait for it. I got a similar problem and that is likely to be my approach related question .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM