c#: How to automatically update a service? - c#

This is probably a duplicate but I can't find recent posts and most sources recommend external tools.
We have a service that is installed via Microsoft Visual Studio 2017 Installer Projects. I assume for updating the service I need an additional tool that does the following:
Stop the service (or is it better to make the service itself check for updates and stop itself?)
Copy the new files to the install folder
Call installutil.exe MyUpdatedService.exe (or is this obsolete if the service is updated ?)
Start the service
We already have a tool for steps 1., 2. and 4. for regular applications. Do I need to implement a ServiceController Class to interact with the service or could any program start/stop the service?

If you want an external c# application, you need ServiceController.
You could stop the service from within the service, but you cannot really start it afterwards.
You don't need to update service registration once it's registered, you only need to replace the service binaries.
Rename old service binaries while service is still running
Not sure if this is possible on all versions of Windows, also it has to be a rename.
Copy new version under the original name
Restart the service
This would unload the old version and load the new one.
The service interruption is the shortest possible.
Clean-up the old version
Once the old version is stopped, you'll be able to remove old binaries.

Related

Update Windows Service on the background

I have a windows service on multiple clients that I constantly update manually. I'm trying to find a way of updating the service automatically either by a server call, or just checking for updates every day. The only thing that I really have to do is to download the new files, stop the service, replace the files and start the service again.
I found the Squirrel.Windows library that adds auto-updating for .Net apps, but I donĀ“t know how can I implement it for a Windows Service. What would be the best option to tackle this problem?
[...] service automatically either by a server call
I've used MSDeploy/WebDeploy using both the command line or from the Visual Studio publish UI command.
Check this other Q&A I've auto-answered it myself Generated *.SourceManifest doesn't include additional runCommand WebDeploy settings.
You can deploy the service executable and satellite assemblies and/or configuration files using regular MSDeploy and then execute a runCommand of some PowerShell or batch file somewhere in the server's file system (f.e. C:\deploy_service.cmd so you'll be able to update this and all servers running your Windows Services running a single MSDeploy command.

How to update windows service (Not auto update)

I inherited a couple c# windows service projects. I have fixed some bugs in the code and now I need to deploy the new executable. What is the normal process for updating / replacing a windows service? I have remote desktop access to the server that I am installing it on.
Just stop the service and replace the executable and possibli the additional dependency you have updated. Of course it could be helpful have a backup of the running service, just to be safe :)
If you need to know exactly where the service is running, open Service Control Manager, select your serice, and look in the properties/general: you will find the executable path.
It's enough to replace exe and dlls. Obviously you have to stop the service first. If dlls were registered in GAC they might be used by other software on that machine, in such case before compiling and deploying change their version.
For a .Net Windows Service you need to use the InstallUtil command line tool to uninstall the old service, then use it to install the new one.
See...
http://msdn.microsoft.com/en-us/library/50614e95(v=vs.80).aspx

install windows service in custom actions

I have created a c# 4.0 windows service. I have created an installer project (.msi) for it which installs the service to a particular folder.
To automate the process fully, I would like to install the service as part of the custom actions I have for my installer.
How can I code my custom actions to install or when uninstalling the msi, uninstall the windows service?
You can use the ServiceInstaller class. A quick solution would be to find installutil tool and execute it against your Service.exe, but you have to capture the output to see whether the installation succeeded or not and you don't have much control over Install, Commit, Rollback, and Uninstall phases.
Simple answer: don't. The proper way is to install it using the MSI database itself, i.e. ServiceInstall and ServiceControl tables. Every single "convenient" IDE for MSI creation and also WiX come with primitives to make use of this builtin facility.
The very reason that this is best practice, just like including the COM registration in your MSI instead of calling DllRegisterServer of the COM (DLL) to register is that your application may be defunct at the time the user attempts to remove it.
The database actions can still be executed even by a newer Windows Installer, say after an upgrade of Windows itself, while your code may refuse to run or may not run for other reasons.

install/uninstall of windows services during development

I would like to know if there is an easier way to install or uninstall windows services onto dev or test environments without creating setup packages? I am having issues with uninstalling visual studio packages sometimes (even after stopping the service and closing the service management console before uninstall forcing me to reboot the machine). this issue can be overcome by using tools like InstallShield or WIX to create setup packages, but during development it will be easier if I can install the service (along with all the dependent files) in dev or test environment without creating a setup package.
Use installutil, which is part of the .net framework. Now you still need to copy the bin directory to the desired machine and register any COM objects if you are using any.
If remember right,( no access to my dev machine at the moment). Stop the service - and replace the exes and dlls) restart the service. You can script this. You will need to do an initial install, or use the installutil.exe
I found this much easier that installing and uninstalling, all the time.
I find the SC.EXE command to be easy to use during development. Slap it into a NAnt/BuildBuild/Perl/BAT file and instant light weight automation.
I know this is an old question, but I feel this information is highly relevant to anyone struggling with this issue:
The problem comes from a change in the Installer included in Visual Studio. I think the change occurred between VS2005 and VS2008. Regardless, after the change, to have an update installer work properly without uninstalling the prior product, it was recommended that Services be installed via Custom Actions in the Install, Commit, and Rollback phase, but not the Uninstall phase. I believe this is because the change caused the Uninstall action to occur after the new version was installed, uninstalling your service upon update. A Condition of "Not PREVIOUSVERSIONSINSTALLED" is placed on the 3 Custom Actions and Check For Previous Versions must be TRUE. This results in essentially the same result as copying the files over the old ones (but also retains any other install activity such as registering objects, etc.)
This all works great for updates; a new version will install over an existing version, the service remains registered, all is well. However, if you uninstall, your service is left registered, and a fresh install will attempt to register it again, resulting in the 1001 error. I use SC.EXE to delete the service manually when I uninstall to avoid this. You can have a clean uninstall, but it will break update installs, your choice.

Need suggestion for how to manage the web application

I have developed a web application also that has been deployed in many client area.
I have created a installer to install this application at client area for first time.
If i done any bug fix or add any new functionality to this site, should i create new installer for site? or How to update the existing site without creating new installer with latest bug fix?
Or, Is there any other option to update existing application?
Or, we can create a installer only install the latest updates?
Please guide me
You can start your journey using the built in web installer (part of Visual Studio).
Important thing is it will only install to a Virtual Directory of an existing web site.
If virtual directories are fine, you then need to worry about bug fixing and updates.
here is a solution...
If you are at the client or have easy access to the web sites via FTP.
You can map drives on your development / test environment to the production FTP sites, then just create a batch to copy stuff over.... best part is - if you're hosting on IIS, if you make any changes to the web.config or any assembly in \Bin you'll trigger an IIS Reset, so this means you shouldn't need to have remote access to their servers.
Make sure that batch updates all web applications in the farm.
You can also fully rely on the application installer - IF YOU'VE FULLY TESTED THAT IT DOES INFACT UPDATE YOUR FILES, LIKE IT SAYS IT SHOULD. Don't just assume it will, test that installer, and retest it... because I have had lots of problems with upgrading a web site, and you can't expect your client to uninstall, reinstall every time.
I wouldn't worry about creating a "patch installer" Web apps are relatively small anyways - and copying unchanged files won't make a huge differance.
Just be sure when you create the installer, you add project outputs to the installer file... and work with project outputs, rather than having to copy stuff in manually to the installer every time you make a change.
Hope this is at least a good starting point.

Categories