c# - 使用 app.config 配置卷影复制

标签 c# service appdomain appdomainsetup

让我先解释一下场景。

我从一个安装库安装了多个(比如 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 主机。您将必须实现自己的示例,例如

  1. 创建一个子 AppDomain 来托管您的服务代码,并配置卷影复制。
  2. 使用类似 FileSystemWatcher 的东西来监控原始 bin 目录。
  3. 当文件更改时,拆除您的 AppDomain 并创建一个新的并重新加载。

ASP.Net 主机按照这些思路做一些事情(但也有能力管理现有请求,在进行时对新请求进行排队等)。

关于c# - 使用 app.config 配置卷影复制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7609019/

相关文章:

c# - 处理 XML 更改的好方法

windows - 如何在意外但正常关机时重新启动tomcat

c# - 如何从 .NET Framework 调用 .NET Core 程序集中的方法

c# - 默认应用程序域的卸载事件?

c# - 多个 AppDomains : Keeping the Console Open

c# - SELECT 从程序超时但在 SQL Server Management Studio 中工作

c# - 通用 Windows .NET Native 和 winmd 组件库

c# - 从 html 字符串中提取数据的库

c++ - 服务崩溃加载dll

Android Camera - 从服务在后台运行