Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以静默方式更新Windows服务_C#_.net_Windows Services_Auto Update - Fatal编程技术网

C# 以静默方式更新Windows服务

C# 以静默方式更新Windows服务,c#,.net,windows-services,auto-update,C#,.net,Windows Services,Auto Update,我已经建立了一个Windows服务,现在我想让它自动更新。我读过一篇关于创建第二个服务或其他程序的文章,不能使用click one,那怎么办?有人知道吗?最好的方法是什么?我能换个组件吗 下载新的exe和任何附加程序集 重命名现有程序集的 复制新程序集的 重新启动服务。您可以将服务重启功能构建到主服务exe中 当服务启动时,从步骤2检查重命名的文件并删除它们以进行清理 要重新启动服务,请执行以下操作: System.Diagnostics.Process.Start (System.Re

我已经建立了一个Windows服务,现在我想让它自动更新。我读过一篇关于创建第二个服务或其他程序的文章,不能使用click one,那怎么办?有人知道吗?最好的方法是什么?我能换个组件吗

  • 下载新的exe和任何附加程序集
  • 重命名现有程序集的
  • 复制新程序集的
  • 重新启动服务。您可以将服务重启功能构建到主服务exe中
  • 当服务启动时,从步骤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();
        }
    
    那么在你的服务中呢

    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();
        }
    
    private const string\u mutexId=“MyUniqueId”;
    专用静态互斥锁(Mutex);;
    /// 
    ///应用程序的主要入口点。
    /// 
    静态void Main()
    {
    尝试
    {
    bool alreadyRunning=false;
    尝试
    {
    Mutex.OpenExisting(_mutexId);
    alreadyRunning=true;
    }
    捕获(WaitHandleCannotBeOpenedException)
    {
    alreadyRunning=false;
    }
    抓住
    {
    alreadyRunning=true;
    }
    如果(已运行)
    {
    使用(ServiceController sc=newServiceController(“MyServiceName”))
    {
    sc.停止();
    sc.WaitForStatus(ServiceControllerStatus.Stopped,新的时间跨度(0,0,120));
    sc.Start();
    }
    返回;
    }
    }
    抓住
    {
    }
    _互斥量=新互斥量(true,_mutexId);
    ServiceBase[]ServicesToRun;
    ServicesToRun=新的ServiceBase[]
    { 
    新MyService()
    };
    //将服务加载到内存中。
    ServiceBase.Run(ServicesToRun);
    _mutex.Close();
    }
    
    您可以修改Windows服务,使其成为主应用程序的运行程序,并具有更新主应用程序的功能

    所以你会:

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

    • Application.exe:以前是您的Service.exe。接收启动/停止事件


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

  • 将updateble逻辑放入单独的DLL中
  • 在服务中创建AppDomain
  • 创建在复制文件时触发事件的文件监视器(可以使用MSFT Ent Lib更新)
  • 在阻塞(排队)从该dll执行内容的线程时卸载旧dll
  • 将新dll文件加载到应用程序域中
  • 让线程知道如何继续处理

  • …对不起,可能解释得不太清楚。在凌晨3点写答案可能不是最好的主意;)