zoukankan      html  css  js  c++  java
  • C#开发Windows服务详细流程

    1.Windows服务简单介绍

      Windows服务程序是在Windows操作系统下能完成特定功能的可执行的应用程序,主要用于长时间运行的功能或者执行定时任务。一般情况下,用户不能通过用户界面来安装和启动Windows服务程序,服务的启动方式有自动启动和手动启动两种。我们可以在运行中输入services.msc 命令打开服务管理界面,在这里可以进行启动或者停止服务:

    2.C#开发windows服务

    1.windows服务开发流程

    1.1 windows的基本开发流程

      使用.Net可以很方便地开发windows服务,下边通过一个栗子演示下开发windows服务的简单流程。

      首先创建一个Windows服务项目,如下图我们创建了一个叫MyService的服务项目

      然后打开自动生成的Service1.cs,该文件默认有两个方法: OnStart()和OnStop() ,OnStart()指定当服务启动时要执行的操作,OnStop()指定当服务停止时要执行的操作。 我们重写OnStart()方法,让服务开启的时候,每隔一秒在一个文本文件中记录当前时间,代码如下图

    然后在右键Service1.cs文件,选择查看设计器,在界面上右键选择添加安装程序,如下图

    修改serviceInstallerserviceProcessInstaller,如下图所示,到这里服务的创建工作就完成了

    1.2 安装和卸载Windows服务项目

       我们先通过下边命令来安装和卸载服务,其中第二行命令用于安装服务,命令的地址是我们服务项目生成的exe文件所在路径,第三行命令用于卸载服务。

    cd C:WindowsMicrosoft.NETFrameworkv4.0.30319
    
    InstallUtil C:UsersDZNsource
    eposWindowsServiceDemoWindowsServiceInstallinDebugMyService.exe    ----------安装服务
    InstallUtil -u C:UsersDZNsource
    eposWindowsServiceDemoWindowsServiceInstallinDebugMyService.exe ----------卸载服务

    执行成功后在服务管理界面可以看到我们刚才安装的服务,这说明服务已经安装成功了:

    我们可以在命令行中通过以下命令控制服务的开启和停止:

    net start MyService //开启服务
    net stop MyService //停止服务
    sc delete MyService //删除服务

    如果程序没有错误的话,服务启动后会在F盘中生成一个hello.txt文件,内容如下:

    2.通过Winform控制windows服务

      虽然通过命令行可以实现Windows服务的安装、卸载、启动、停止等操作,但是这样对于用户来说还是太过麻烦。如果能通过Winform来实现这些功能,用户使用起来就方便多了。怎么让Winform控制windows服务呢?

    首先要知道的一些知识:使用AssemblyInstaller可以进行Windows服务的安装和卸载,ServiceController可以控制windows服务的启动,停止,暂停,继续等,知道了这些就容易实现了。

    添加一个winform应用程序,引用我们的服务项目,程序代码如下,这里的命名都很明显就不详细说明:

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            //设置服务地址和服务名
            string serviceFilePath = $"{Application.StartupPath}/MyService.exe";
            string serviceName = "MyService";
    
            private void btnInstall_Click(object sender, EventArgs e)
            {
                Task.Run(() =>
                {
                    ShowMessage("开始安装服务...");
                    if (IsServiceExisted(serviceName))
                    {
                        UninstallService(serviceFilePath);
                    }
                    InstallService(serviceFilePath);
                });
            }
    
            private void btnStart_Click(object sender, EventArgs e)
            {
                Task.Run(() =>
                {
                    ShowMessage("服务正在启动...");
                    if (IsServiceExisted(serviceName))
                    {
                        ServiceStart(serviceName);
                        ShowMessage("服务已启动");
                    }
    
                });
            }
    
            private void btnStop_Click(object sender, EventArgs e)
            {
                Task.Run(() =>
                {
                    ShowMessage("服务正在停止...");
                    if (IsServiceExisted(serviceName))
                    {
                        ServiceStop(serviceName);
                        ShowMessage("服务已停止");
                    }
                });
            }
    
    
    
            private void btnUninstall_Click(object sender, EventArgs e)
            {
                Task.Run(() =>
                {
                    ShowMessage("开始卸载服务...");
                    if (this.IsServiceExisted(serviceName))
                    {
                        this.ServiceStop(serviceName);
                    }
                    this.UninstallService(serviceFilePath);
                });
            }
    
            #region 方法封装
            private void ShowMessage(string s)
            {
                Action setState = () =>
                {
                    myTxtBox.AppendText(DateTime.Now.ToString("f") + "");
                    myTxtBox.AppendText(Environment.NewLine);
                    myTxtBox.AppendText(s);
                    myTxtBox.AppendText(Environment.NewLine);
    
                };
                myTxtBox.Invoke(setState);
            }
    
            //判断服务是否存在
            private bool IsServiceExisted(string serviceName)
            {
                try
                {
                    ServiceController[] services = ServiceController.GetServices();
                    foreach (ServiceController sc in services)
                    {
                        if (sc.ServiceName.ToLower() == serviceName.ToLower())
                        {
                            return true;
                        }
                    }
                    return false;
                }
                catch (Exception ex)
                {
                    ShowMessage(ex.Message);
                    return false;
                }
            }
    
            //安装服务
            private void InstallService(string serviceFilePath)
            {
                try
                {
                    using (AssemblyInstaller installer = new AssemblyInstaller())
                    {
                        installer.UseNewContext = true;
                        installer.Path = serviceFilePath;
                        IDictionary savedState = new Hashtable();
                        installer.Install(savedState);
                        installer.Commit(savedState);
                        ShowMessage("服务安装完成");
                    }
                }
                catch (Exception ex)
                {
                    ShowMessage(ex.Message);
                }
            }
    
            //卸载服务
            private void UninstallService(string serviceFilePath)
            {
                try
                {
                    using (AssemblyInstaller installer = new AssemblyInstaller())
                    {
                        installer.UseNewContext = true;
                        installer.Path = serviceFilePath;
                        installer.Uninstall(null);
                        ShowMessage("服务卸载完成");
                    }
                }
                catch (Exception ex)
                {
                    ShowMessage(ex.Message);
                }
            }
    
            //启动服务
            private void ServiceStart(string serviceName)
            {
                try
                {
                    using (ServiceController control = new ServiceController(serviceName))
                    {
                        if (control.Status == ServiceControllerStatus.Stopped)
                        {
                            control.Start();
                        }
                    }
                }
                catch (Exception ex)
                {
                    ShowMessage(ex.Message);
                }
            }
    
    
            //停止服务
            private void ServiceStop(string serviceName)
            {
                try
                {
                    using (ServiceController control = new ServiceController(serviceName))
                    {
                        if (control.Status == ServiceControllerStatus.Running)
                        {
                            control.Stop();
                        }
                    }
                }
                catch (Exception ex)
                {
                    ShowMessage(ex.Message);
                }
            }
            #endregion
    
            #region 页面元素
    
            private void Form1_Load(object sender, EventArgs e)
            {
                Control.CheckForIllegalCrossThreadCalls = false;
    
                timer1.Interval = 100;
                timer1.Enabled = true;
            }
    
            //btn状态
            private void ButtonState()
            {
                if (IsServiceExisted(serviceName))
                {
                    btnInstall.Enabled = false;
                    btnUninstall.Enabled = true;
                    using (var service = new ServiceController(serviceName))
                    {
                        if (service.Status == ServiceControllerStatus.Running || service.Status == ServiceControllerStatus.StartPending)
                        {
                            btnUninstall.Enabled = false;
                            btnStart.Enabled = false;
                            btnStop.Enabled = true;
                        }
                        else
                        {
                            btnUninstall.Enabled = true;
                            btnStart.Enabled = true;
                            btnStop.Enabled = false;
                        }
                    }
                }
                else
                {
                    btnInstall.Enabled = true;
                    btnUninstall.Enabled = false;
                    btnStart.Enabled = false;
                    btnStop.Enabled = false;
                }
            }
            //底部label状态
            private void LabelState()
            {
                if (!IsServiceExisted(serviceName))
                {
                    this.labState.Text = $"【{serviceName}】服务未安装";
                    return;
                }
                using (var service = new ServiceController(serviceName))
                {
                    switch (service.Status)
                    {
                        case ServiceControllerStatus.Running:
                            this.labState.Text = $"【{serviceName}】服务已启动";
                            break;
                        case ServiceControllerStatus.StartPending:
                            this.labState.Text = $"【{serviceName}】服务正在启动...";
                            break;
                        case ServiceControllerStatus.Stopped:
                            this.labState.Text = $"【{serviceName}】服务已停止";
                            break;
                        case ServiceControllerStatus.StopPending:
                            this.labState.Text = $"【{serviceName}】服务正在停止...";
                            break;
                        default:
                            break;
                    }
                }
            }
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                ButtonState();
                LabelState();
            }
            #endregion
        }
    View Code

    完成这一步后,我们就可以通过可视化界面对服务的安装,卸载,运行,停止进行控制了。

    补充:使用TopShelf也可以实现Windows服务的快速开发,参考文档 http://docs.topshelf-project.com/en/latest/index.html

  • 相关阅读:
    Docker和k8s的区别与介绍
    NFS网络文件系统详解
    NFS文件系统及搭建NFS共享服务
    Tomcat 端口配置,及原理详解
    svn使用教程
    buff/cache内存占用过多
    关于xcode:如何在Objective-C中使用符号断点获取参数
    iOS开发消除编译警告
    建模的能力才是一个人和核心能力
    android sutdio 环境搭建
  • 原文地址:https://www.cnblogs.com/wyy1234/p/10191958.html
Copyright © 2011-2022 走看看