我希望我的 C# Windows 服务自动更新自己

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/106765/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 14:14:32  来源:igfitidea点击:

I want my C# Windows Service to automatically update itself

提问by Larry Smithmier

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.

是否有可用于启用 C# Windows 服务以自动检查更新版本并自行升级的框架?我当然可以编写代码来实现这一点,但我正在寻找一个已经实现并(最重要的是)经过测试的框架。

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

[编辑] 这是一个类似问题的链接,其中包含有助于实现此目的的现代项目的链接:Auto-update library for .NET?

采纳答案by stephbu

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.

可能更容易 a) 运行批处理任务或 b) 服务中检测更新然后启动一个单独的进程来停止服务、更新程序集等,然后重新启动它。

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

如果您对前者感兴趣,MSDN 模式和实践人员编写了一个应用程序更新程序块,您可以根据自己的服务进行调整。

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

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

回答by Esteban Araya

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.

你能澄清一下你的问题吗?我有点困惑,因为据我所知,您总是可以覆盖服务使用的 DLL。服务的复制和重新启动可以很容易地成为构建过程的一部分。

回答by ilitirit

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

您可以尝试将服务的业务逻辑与实际服务代码分离到不同的程序集中。让服务程序集定期检查业务逻辑程序集的更新,必要时从远程源复制它,卸载旧的 BL 程序集(也可能删除),然后动态加载新版本(卸载旧程序集不是一项微不足道的任务)。

回答by David Basarab

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.

另一种可能的解决方案是让一个单独的服务运行,如果有更新,则停止另一个服务,然后更新该服务。您无法自行更新服务,因为正在运行的 .dll 不会停止。

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.

我知道没有已知的框架可以做到这一点。我自己做了这个,但这不是一个公共框架。

回答by gillonba

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

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

回答by Omaer

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

我一直在使用 WyBuild 来更新我的应用程序(包括 Windows 服务),它非常棒。非常易于使用,并且非常容易与现有应用程序集成。这是一个非常棒的自动更新框架...

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

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)

请注意,它是一个付费框架(许可证是每个开发人员,包括免费试用)