让我先解释一下场景。
我从一个安装库安装了多个(比如 10 个)服务副本。现在我想更新其中一个 dll。我需要停止所有服务,更新 dll 并重新启动服务。
为了避免这种情况,我在代码中使用了ShadowCopying。这样就可以在不停止所有服务的情况下更新 dll。具体如下。
static void Main(string[] args)
{
AppDomain.CurrentDomain.SetCachePath(@"C:\Cache");
AppDomain.CurrentDomain.SetShadowCopyPath(AppDomain.CurrentDomain.BaseDirectory);
AppDomain.CurrentDomain.SetShadowCopyFiles();
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new SampleService(serviceName)
};
ServiceBase.Run(ServicesToRun);
}
现在我正尝试通过 app.config 文件实现相同的目的,如下所示,来自 Asp.Net
<hostingEnvironment
idleTimeout="Infinite"
shutdownTimeout="30"
shadowCopyBinAssemblies="true" />
有什么建议吗?
最佳答案
ASP.Net 托管环境内置了对管理应用程序回收的支持。
Windows .Net 服务使用不支持此功能的标准 CLR 主机。您将必须实现自己的示例,例如
- 创建一个子
AppDomain
来托管您的服务代码,并配置卷影复制。 - 使用类似
FileSystemWatcher
的东西来监控原始 bin 目录。 - 当文件更改时,拆除您的
AppDomain
并创建一个新的并重新加载。
ASP.Net 主机按照这些思路做一些事情(但也有能力管理现有请求,在进行时对新请求进行排队等)。
关于c# - 使用 app.config 配置卷影复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7609019/