36

Is there a framework that can be used to enable a C# Windows Service to automatically check for a newer version and upgrade itself? I can certainly write code to accomplish this, but I am looking for a framework that has already been implemented and (most importantly) tested.

[edit] Here is a link to a similar question with links to modern projects that help accomplish this: Auto-update library for .NET?

Community
  • 1
  • 1
Larry Smithmier
  • 2,711
  • 2
  • 23
  • 30
  • I just found a blog post "Creating an Auto-updating Application in .NET/C#" (http://www.nerdparadise.com/tech/csharp/autoupdatingapp/) with a link to code. It is for an application, rather than a service, but I bet it could easily be updated to work. – Larry Smithmier Aug 08 '12 at 18:26

9 Answers9

20

The only way to unload types is to destroy the appdomain. To do this would require separation of your hosting layer from your executing service code - this is pretty complex. (sort of like doing keyhole surgery)

May be easier to either a) run a batch task or b) in-service detect updates then launch a seperate process that stops the service, updates assemblies etc. then restarts it.

If you're interested in the former, the MSDN patterns and practices folk wrote an app updater block that you adapt to your service.

https://web.archive.org/web/20080506103749/http://msdn.microsoft.com/en-us/library/ms978574.aspx

Jesse Hufstetler
  • 583
  • 7
  • 13
stephbu
  • 5,072
  • 26
  • 42
  • 1
    I'm surprised that as today, there is no easy solution after 3 years. Thanks stepbu. – Sauleil Nov 01 '11 at 20:05
  • Unloading a type is very hard. It would involve unwinding all instances of that type in memory, plus any reference to that instance e.g. members and properties in other instances. A side-effect that could be very unpredictable moderately complex application. The unload appdomain solution side-steps that problem by instead invalidating all memory in that appdomain. Leaving you to deal with only one (albeit larger, but deterministic) problem. – stephbu Nov 05 '11 at 02:45
  • 2
    I know this is an old question, but I wanted to also add the point that at current companies that have more than enough resources to do things "the right way," like Adobe and Google, have also settled on the separate-service-just-for-checking-updates pattern. They may not be using C# and their projects may not be similar to your projects, but I think it is at least useful to acknowledge that this is a real solution with real benefits, not just a good-enough-for-now hack. – GrandOpener Jan 24 '14 at 17:48
  • I'd hardly call Adobe or Google's update approaches as bastions of right. Most typically they fail in fast-switch/multi-user logins in home environments. Some examples of failures: Installing one service per user. Cycle-sucking shell extensions. Installing tasks in task scheduler for each user. None of these are great behaviours. A better example IMHO is Spotifies background check and UI optionally "restart to update" approach. – stephbu Jan 24 '14 at 20:31
  • Spotify doesn't apply to windows service though does it? – mrmillsy May 19 '15 at 20:50
  • 2
    The service story is install at restart - either forced (by the app) or at the next reboot. Pick your poison. – stephbu May 20 '15 at 17:37
  • @stephbu can you walk me through that? what does that look like? – pomeroy Feb 01 '17 at 01:17
  • @pomeroy it's pretty ugly - first split service into two - bootstrap system that loads your service in a separate assembly. Lifecycle is two phases - startup/update, then run. On startup phase the bootstrap checks for updates to the service assembly, grabs and downloads them. Run phase finds and loads the service assembly, then runs your code. Makes testing painful for sure. You could consider interesting things like automation to restart the service if an update is discovered. – stephbu Feb 23 '17 at 19:41
4

I've had good experiences with Google Omaha for updating Windows Services. It can generally be used to update any Windows application but there is a tutorial for using it to update a Windows Service.

In comparison to other update frameworks, Omaha is more complex, but also the most powerful. This is because Google (and now also Microsoft) use it to update their browsers on billions of PCs. However, once it's set up and you understand the basics, it is a very nice experience.

Michael Herrmann
  • 4,832
  • 3
  • 38
  • 53
3

I'm not aware of any Frameworks that facilitate solutions to this specific problem.

What you could try though is separating the business logic of the service from the actual service code into different assemblies. Have the service assembly check for updates to the business logic assembly at regular intervals, copy it from a remote source if necessary, unload the old BL assembly (and perhaps delete), and then dynamically load the new version (unloading the old assembly is not a trivial task).

ilitirit
  • 16,016
  • 18
  • 72
  • 111
3

Another possible solution is to have a seperate service that runs, stops the other one, if there is an update, and then updates the service. You can't have a service update itself because the .dll that is running will not stop.

Seperating the business logic layer would be a good option. You could also rewrite the main service to run under reflection by a master or control service. This is similar to seperating the business logic, and it would just require stopping a thread and the starting it again.

I know of no known framework that does this. I have done this myself, but that is not a public framework.

David Basarab
  • 72,212
  • 42
  • 129
  • 156
  • This is exactly what Google Omaha does. It runs as a separate service and updates other applications "externally". See https://omaha-consulting.com/auto-update-your-windows-service-with-google-omaha. – Michael Herrmann Feb 02 '21 at 18:42
3

I've been using WyBuild to update my applications (including Windows services) and it's pretty awesome. Really easy to use, and really easy to integrate with existing applications. It's a pretty great Automatic Updating framework...

http://wyday.com/wybuild/help/automatic-updates/windows-services-console-apps.php http://wyday.com/wybuild/help/silent-update-windows-service.php

Note that it is a paid framework (the licence is per developer, a free trial is included)

Omaer
  • 817
  • 1
  • 7
  • 22
1

In case anyone else is searching for this; I found this link interesting. I have not implemented this solution, but it looks like it might work for me

http://www.eggheadcafe.com/articles/20041204.asp

gillonba
  • 897
  • 9
  • 24
1

You can use NetSparke, it supports .NET Core 3+ (.NET 5+) and .NET Framework 4.5.2+. It comes with built-in UI or without any UI at all. You can handle all the events by yourself. It works with an appcast.xml (and also have a utility to generate that), is platform independent and just works out of the box.

Just work trough the documentation on their repository and check the example app ;-)

jebbie
  • 1,418
  • 3
  • 17
  • 27
1

I was looking into NetSparkle as others have suggested and came across these alternatives. AutoUpdater.NET is my pick due to ease of use and feature set.

datchung
  • 3,778
  • 1
  • 28
  • 29
-3

Could you clarify your question a bit? I'm a bit confused, because as far as I know, you can always overwrite the DLLs the service uses. The copy and restart of the service can easily be made part of you build process.

Esteban Araya
  • 29,284
  • 24
  • 107
  • 141
  • Sorry for the confusion. I would like a way for my service to poll for updates and self install them when found. I have many services on many production (access controlled) servers and am very tired of the hastle and risk of manual interventions. – Larry Smithmier Sep 20 '08 at 05:44