zoukankan      html  css  js  c++  java
  • windows服务开发

    学然后知不足 教然后知困。

    没做过windows service开发时,感觉很难,无从下手。再网看了些例子,加上自己的理解,写下开发步骤。

    开发步骤

    1. 新建windows service

    2. 添加服务,发表后将再自己的电脑的 服务管理 查看到

    	partial class MyService : ServiceBase
    	{
    		Timer timer1;
    
    		public MyService()
    		{
    			InitializeComponent();
    		}
    
    		protected override void OnStart(string[] args)
    		{
    			// TODO: Add code here to start your service.
    
    			using (StreamWriter sw = File.AppendText(@"D:\log2.txt"))
    			{
    				sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Start.");
    			}
    
    			timer1 = new Timer();
    			timer1.Interval = 2000;
    			timer1.Elapsed += (sender, e) =>
    			{
    				using (StreamWriter sw = File.AppendText(@"D:\log2.txt"))
    				{
    					sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Countue...");
    				}
    			};
    			timer1.Start();
    		}
    
    		protected override void OnStop()
    		{
    			// TODO: Add code here to perform any tear-down necessary to stop your service.
    			using (StreamWriter sw = File.AppendText(@"D:\log2.txt"))
    			{
    				sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " Stop.");
    			}
    		}
    	}
    
    1. 添加服务安装程序(这是vs给提供的方便操作),可以发表多个服务。

    右击,点击Add Installer,自动生成类ProjectInstaller。大家可以看下vs自动生成的类ProjectInstaller.Designer.cs文件

    private void InitializeComponent()
    		{
    			this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
    			this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
    			//设置服务安装信息
    			this.serviceProcessInstaller1.Account = System.ServiceProcess.ServiceAccount.LocalService;
    			this.serviceProcessInstaller1.Password = null;
    			this.serviceProcessInstaller1.Username = null;
    			//要安装的服务(保证与你的服务名一致,可添加多个)
    			this.serviceInstaller1.ServiceName = "MyService";
    			//服务加入
    			this.Installers.AddRange(new System.Configuration.Install.Installer[] {
                this.serviceProcessInstaller1,
                this.serviceInstaller1});
    
    		}
    

    注意将服务安装设置为:手动安装
    4. 服务安装卸载脚本
    Install.bat 文件:

    %SystemRoot%Microsoft.NETFrameworkv4.0.30319InstallUtil.exe "%~dp0%WindowsServiceTest.exe"
    pause
    

    Uninstall.bat文件:

    %SystemRoot%Microsoft.NETFrameworkv4.0.30319InstallUtil.exe -u "%~dp0%WindowsServiceTest.exe"
    pause
    
  • 相关阅读:
    Abp Zero 演示(链接)
    阿里中台战略是个伪命题(转)
    AlphaFlow智能BPM专家的博客
    绵绵用力方能久久为功 --《工程建设企业管理信息化实用案例精选》前言 -- 鲁贵卿
    业务梳理优化(政府、企业)---- 收集网上资料链接
    .NET for Apache® Spark™ 开源大数据分析工具
    Net Core 3.0 及 AspNet 3.0
    统一身份访问管理平台 (收集)-- Identity Access management platform
    SciSharp .Net 平台的人工智能,Net 如何调用 Python
    Identity Server4 及 其它 OpenId 服务器 学习
  • 原文地址:https://www.cnblogs.com/LoveTomato/p/8584026.html
Copyright © 2011-2022 走看看