【发布时间】:2011-12-08 06:05:11
【问题描述】:
自动更新桌面应用程序似乎是 .NET 中的一个难题。有很多解决方案,从微软的 clickonce 到各种开源和付费解决方案。所有这些的模式是:
1 检查远程服务器的版本以运行
2 下载新版本
3 解压到临时文件夹
4 启动 shim 进程,在应用关闭后用新文件替换旧文件
5 终止正在运行的应用程序,以便 shim 进程可以开始工作
6 重启主应用
这个过程似乎有很多活动部件和故障点。但是,您可以将应用程序编写为一个简单的循环,下载并执行远程 dll。这似乎避免了自动更新应用程序所涉及的许多复杂性,例如用户权限、uac、防病毒程序等。
对我来说,这似乎是可靠且面向未来的。我的应用程序不需要大量的大型支持文件,那么我是否有理由不使用这种方法?有没有办法会出错?如果是这样,这些复杂性与其他自动更新解决方案的复杂性相比如何?
例如:
Thread helper;
int currentVersion=0;
static void Main()
{
//dumb loop, functions as a loader
while(true)
{
var remoteVersion = GetRemoteVersion();
if(remoteVersion > currentVersion)
{
if(helper != null)
{
//kill thread gracefully :)
}
currentVersion = remoteVersion;
byte[] bytes = GetRemoteDllAsByteArray();
var asm = Assembly.Load(bytes);
var t = asm.GetType("a known type with a static method called Start()");
var mi = t.GetMethod("Start");
//run this on a helper thread outside the main loop:
// the start method is the entry point to your application
helper= new Thread(() => mi.Invoke(null,null));
helper.SetApartmentState(ApartmentState.STA); //for winforms/wpf
helper.Start();
}
Thread.Sleep(1 day);//only check for new versions once a day
}
}
在我的示例中,额外的复杂性是您必须将所有依赖项打包为下载的 dll 中的资源,但这可以设置为构建步骤,并且可以避免弄乱用户的文件系统。
【问题讨论】:
-
使用任务计划程序代替
Thread.Sleep(1 day);。 -
@dan:好的。这比代码更伪代码,否则我会迁移到Code Review。
标签: c# deployment