静默更新 Windows 服务

发布于 2024-10-10 22:19:50 字数 198 浏览 7 评论 0原文

我已经构建了一个 Windows 服务,现在我希望它自动更新。我读过有关创建第二个服务来执行此操作或不同的程序的内容,无法使用单击一个,myBuild?有人知道吗?最好的方法是什么?我可以只更换组件吗?

I have built a Windows service, now I want it to auto-update. I have read about a creating a second service to do that or different program , cant use click one, what about myBuild? Does anyone know it? What is the best way? Can I just change assemblies?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

虚拟世界 2024-10-17 22:19:50

如果您希望服务在执行更新时运行,我之前为实现此目的所做的操作如下:

  1. 将可更新逻辑放入单独的 DLL 中。
  2. 在您的服务中创建一个 AppDomain。
  3. 创建文件监视器,每当您复制该文件时都会触发一个事件(您可以使用 MSFT Ent Lib 更新)
  4. 卸载旧 dll,同时阻止(排队)执行该 dll 内容的线程
  5. 将新 dll 文件加载到应用程序域中。
  6. 让您的线程知道继续处理。

If you want your service to run while you are performing an update, here is what I had done before to achieve this:

  1. Put your updateble logic into a separate DLL.
  2. Create an AppDomain within your service.
  3. Create file monitor that fires an event whenever you copy that file (you can use MSFT Ent Lib Updates)
  4. Unload the old dll while blocking (queue) the threads that execute stuff from that dll
  5. Load in the new dll file into the app domain.
  6. Let your threads know to continue processing.
樱花坊 2024-10-17 22:19:50
  1. 下载新的 exe 和任何其他程序集。
  2. 重命名现有程序集。
  3. 复制您的新程序集。
  4. 重新启动服务。您可以将服务重启功能构建到您的主服务exe中。
  5. 当服务启动时,检查步骤 2 中的重命名文件并删除它们以进行清理。

要重新启动您的服务,请执行以下操作

System.Diagnostics.Process.Start
    (System.Reflection.Assembly.GetEntryAssembly().Location)

然后在您的服务中执行以下操作

    private const string _mutexId = "MyUniqueId";
    private static Mutex _mutex;
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        try
        {
            bool alreadyRunning = false;
            try
            {
                Mutex.OpenExisting(_mutexId);
                alreadyRunning = true;
            }
            catch (WaitHandleCannotBeOpenedException)
            {
                alreadyRunning = false;
            }
            catch
            {
                alreadyRunning = true;                   
            }
            if (alreadyRunning)
            {
                using (ServiceController sc = new ServiceController("MyServiceName"))
                {
                    sc.Stop();
                    sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 120));
                    sc.Start();
                }
                return;
            }
        }
        catch
        {
        }
        _mutex = new Mutex(true, _mutexId);

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new MyService() 
        };
        // Load the service into memory.
        ServiceBase.Run(ServicesToRun);
        _mutex.Close();
    }
  1. Download the new exe and any additional assembly's.
  2. Rename your existing assembly's.
  3. Copy in your new assembly's.
  4. Restart Service. You can build the service restart function into your main service exe.
  5. When service starts check for renamed files from step 2 and delete them to clean up.

To restart your service do

System.Diagnostics.Process.Start
    (System.Reflection.Assembly.GetEntryAssembly().Location)

Then in your service do

    private const string _mutexId = "MyUniqueId";
    private static Mutex _mutex;
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        try
        {
            bool alreadyRunning = false;
            try
            {
                Mutex.OpenExisting(_mutexId);
                alreadyRunning = true;
            }
            catch (WaitHandleCannotBeOpenedException)
            {
                alreadyRunning = false;
            }
            catch
            {
                alreadyRunning = true;                   
            }
            if (alreadyRunning)
            {
                using (ServiceController sc = new ServiceController("MyServiceName"))
                {
                    sc.Stop();
                    sc.WaitForStatus(ServiceControllerStatus.Stopped, new TimeSpan(0, 0, 120));
                    sc.Start();
                }
                return;
            }
        }
        catch
        {
        }
        _mutex = new Mutex(true, _mutexId);

        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new MyService() 
        };
        // Load the service into memory.
        ServiceBase.Run(ServicesToRun);
        _mutex.Close();
    }
染柒℉ 2024-10-17 22:19:50

您可以修改 Windows 服务,使其只是主应用程序的运行程序,并具有更新主应用程序的功能。

因此,您将拥有:

  • Service.exe:运行 Application.exe,监视远程位置以获取 Application.exe 的更新。将启动/停止事件发送到 Application.exe

  • Application.exe :曾经是您的 Service.exe。接收开始/停止事件。

You could modify your Windows Service so that it is simply a runner for your main application, and has the functionality to update your main application.

So you would have:

  • Service.exe: Runs Application.exe, monitors remote location for updates to Application.exe. Sends start/stop events to Application.exe

  • Application.exe : What used to be your Service.exe. Recieves start/stop events.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文