首页 新闻 搜索 专区 学院

Windows服务第二次安装启动出问题

0
悬赏园豆:10 [已关闭问题] 关闭于 2020-09-17 17:04

先放界面

using System;
using System.Windows.Forms;
using System.ServiceProcess;
using System.Configuration.Install;
using System.Collections;

namespace WindowsForms
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private string servicePath = $"{Application.StartupPath}/WindowsService.exe";
        private string serviceName = "MyService";

        /// <summary>
        /// 事件:安装服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void installBtn_Click(object sender, EventArgs e)
        {
            if (IsServiceExisted(serviceName))
            {
                ServiceStop(serviceName);
                UninstallService(serviceName);
            }
            InstallService(servicePath);
        }

        /// <summary>
        /// 事件:卸载服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uninstallBtn_Click(object sender, EventArgs e)
        {
            if (IsServiceExisted(serviceName))
            {
                ServiceStop(serviceName);
                UninstallService(servicePath);
            }
        }

        /// <summary>
        /// 事件:开启服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void startBtn_Click(object sender, EventArgs e)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Stopped)
                {
                    control.Start();
                }
            }
        }

        /// <summary>
        /// 事件:停止服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void stopBtn_Click(object sender, EventArgs e)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Running)
                {
                    control.Stop();
                }
            }
        }

        /// <summary>
        /// 服务是否存在
        /// </summary>
        /// <param name="serviceName"></param>
        /// <returns></returns>
        private bool IsServiceExisted(string serviceName)
        {
            ServiceController[] services = ServiceController.GetServices();
            foreach (ServiceController sc in services)
            {
                if (sc.ServiceName.ToLower() == serviceName.ToLower())
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 服务安装
        /// </summary>
        /// <param name="servicePath"></param>
        private void InstallService(string servicePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = servicePath;
                IDictionary savedState = new Hashtable();
                installer.Install(savedState);
                installer.Commit(savedState);
            }
        }

        /// <summary>
        /// 服务卸载
        /// </summary>
        /// <param name="serviceName"></param>
        private void UninstallService(string servicePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = servicePath;
                installer.Uninstall(null);
            }
        }

        /// <summary>
        /// 服务启动
        /// </summary>
        /// <param name="serviceName"></param>
        private void ServiceStart(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Stopped)
                {
                    control.Start();
                }
            }
        }

        /// <summary>
        /// 服务停止
        /// </summary>
        /// <param name="serviceName"></param>
        private void ServiceStop(string serviceName)
        {
            using (ServiceController control = new ServiceController(serviceName))
            {
                if (control.Status == ServiceControllerStatus.Running)
                {
                    control.Stop();
                }
            }
        }
    }
}

一段学习WindowsService的代码,我遇到的问题:

第一次运行,安装服务,启动,停止,都没问题,就是没有卸载
第二次运行时,安装服务的时候提示

WS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration.Install\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.Install.dll”。已跳过加载符号。模块进行了优化,并且调试器选项“仅我的代码”已启用。
引发的异常:“System.IO.FileNotFoundException”(位于 mscorlib.dll 中)
“System.IO.FileNotFoundException”类型的未经处理的异常在 mscorlib.dll 中发生 
未能加载文件或程序集“file:///D:\visual studio\WindowsForms\bin\Debug\MyService”或它的某一个依赖项。系统找不到指定的文件。

这个问题的错误在

private void UninstallService(string servicePath)
        {
            using (AssemblyInstaller installer = new AssemblyInstaller())
            {
                installer.UseNewContext = true;
                installer.Path = servicePath;//错误在这里呀呀呀呀呀呀
                installer.Uninstall(null);
            }
        }

但是,第二次你换个顺序,先卸载服务,再安装服务,就不会有问题。
喵喵喵?这段代码(摘自上面可长那段代码)???

/// <summary>
        /// 事件:安装服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void installBtn_Click(object sender, EventArgs e)
        {
            if (IsServiceExisted(serviceName))
            {
                ServiceStop(serviceName);
                UninstallService(serviceName);
            }
            InstallService(servicePath);
        }

        /// <summary>
        /// 事件:卸载服务
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void uninstallBtn_Click(object sender, EventArgs e)
        {
            if (IsServiceExisted(serviceName))
            {
                ServiceStop(serviceName);
                UninstallService(servicePath);
            }
        }
echo_lovely的主页 echo_lovely | 小虾三级 | 园豆:911
提问于:2020-09-17 16:19
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册