I want my C# Windows Service to automatically upda

2020-02-08 23:56发布

问题:

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?

回答1:

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



回答2:

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).



回答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.



回答4:

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)



回答5:

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



回答6:

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.