• c#开发windows服务


    1.首先添加一个windows服务程序

     2.在  protected override void OnStart(string[] args)中加入我们的业务逻辑代码

    3.在我们的GateService中的设计页面,添加安装程序

     4.配置安装程序serviceProcessInstaller1和serviceInstaller1

    serviceInstaller1中 StartType设置为Manual,ServiceName设置为我们的服务名

      serviceProcessInstaller1 中设置服务类型,Account设置为LocalService

    ---------------------以上我们的服务就开发完成了,但是服务不能直接启动,我们需要一个应用程序来启动我们的服务

    1.建立一个winform应用程序来启动服务

     2.ui页面设计四个按钮:安装、启动、停止、卸载

     3.对我们的服务进行引用

    4.winform代码:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Configuration.Install;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.ServiceProcess;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace GateServiceClientManager
    {
        public partial class FrmClient : Form
        {
            string serviceFilePath = $"{Application.StartupPath}\GateServiceManager.exe";//这里是我们服务的exe
            string serviceName = "GateService";
            public FrmClient()
            {
                InitializeComponent();
            }
    
            private void btnset_Click(object sender, EventArgs e)
            {
                this.BeginInvoke(new Action(() =>
                {
                    if (this.IsServiceExisted(serviceName)) this.UninstallService(serviceName);
                    this.InstallService(serviceFilePath);
                    MessageBox.Show("服务安装成功!");
                }));
                
            }
    
            private void btnstart_Click(object sender, EventArgs e)
            {
                this.BeginInvoke(new Action(() =>
                {
                    if (this.IsServiceExisted(serviceName)) this.ServiceStart(serviceName);
                    MessageBox.Show("服务启动成功!");
                }));
              
            }
    
            private void btnstop_Click(object sender, EventArgs e)
            {
                this.BeginInvoke(new Action(() =>
                {
                    if (this.IsServiceExisted(serviceName)) this.ServiceStop(serviceName);
                    MessageBox.Show("服务停止成功!");
                }));
              
            }
    
            private void btnunset_Click(object sender, EventArgs e)
            {
                this.BeginInvoke(new Action(() =>
                {
                    if (this.IsServiceExisted(serviceName))
                    {
                        this.ServiceStop(serviceName);
                        this.UninstallService(serviceFilePath);
                        MessageBox.Show("服务卸载成功!");
                    }
                }));
               
            }
            //判断服务是否存在
            private bool IsServiceExisted(string serviceName)
            {
                ServiceController[] services = ServiceController.GetServices();
                foreach (ServiceController sc in services)
                {
                    if (sc.ServiceName.ToLower() == serviceName.ToLower())
                    {
                        return true;
                    }
                }
                return false;
            }
    
            //安装服务
            private void InstallService(string serviceFilePath)
            {
                using (AssemblyInstaller installer = new AssemblyInstaller())
                {
                    installer.UseNewContext = true;
                    installer.Path = serviceFilePath;
                    IDictionary savedState = new Hashtable();
                    installer.Install(savedState);
                    installer.Commit(savedState);
                }
            }
    
            //卸载服务
            private void UninstallService(string serviceFilePath)
            {
                using (AssemblyInstaller installer = new AssemblyInstaller())
                {
                    installer.UseNewContext = true;
                    installer.Path = serviceFilePath;
                    installer.Uninstall(null);
                }
            }
            //启动服务
            private void ServiceStart(string serviceName)
            {
                using (ServiceController control = new ServiceController(serviceName))
                {
                    if (control.Status == ServiceControllerStatus.Stopped)
                    {
                        control.Start();
                    }
                }
            }
    
            //停止服务
            private void ServiceStop(string serviceName)
            {
                using (ServiceController control = new ServiceController(serviceName))
                {
                    if (control.Status == ServiceControllerStatus.Running)
                    {
                        control.Stop();
                    }
                }
            }
        }
    }

    以上这样我们就可以启动winform程序来打开我们的服务啦

    遇到的问题:

    1.如果需要在GateServiceManager(也就是我们的服务)加入配置文件的信息

    比如:

     在服务中的app.config是读取不到的,在winform中加也是读取不到的。

    解决办法:我们需要在服务中心加入app.config,然后再把服务的app.config复制到winform程序中这样才可以读取到我们的配置信息

     2.如果用winform程序开启服务时,出现:其他信息: 无法启动计算机“.”上的服务 zjService。那一定是你的服务代码写的有问题。不用怀疑直接去改就好了

    --------------------关于服务调试

    首先我们要把我们的服务启动

    2.用vs把服务添加到进程中,进行调试

    找到我们的服务,然后就可以调试了

    注意:在调试OnStart时,一定要加一个延迟Thread.Sleep(30000); ,要不然总是调试不到。真的有点坑!!!

    至此,你就掌握了如何开发windows服务啦~

  • 相关阅读:
    SpringCloud微服务初步认识
    SpringCloud-Hystrix:服务熔断与降级
    List接口下重要集合源码分析
    高频面试题:手写一个LRU
    Java基础面试题面经整理(持续更新)
    Redis高可用之主从复制
    Redis过期键删除和内存淘汰
    Redis持久化(RDB与AOF)
    了解Redis事务
    Redis入门与安装
  • 原文地址:https://www.cnblogs.com/daimaxuejia/p/12916467.html
Copyright © 2020-2023  润新知