.net服务使用笔记(原创)

最近有个项目需要使用windows 服务 来做

(其实原来也有很多项目有这个需求 只是偷懒用windows应用程序来做了   必须登录才能运行)

这一块 一直 心存遗憾 这一次 决心要用 真正的windows 服务 来做

第一步: 添加windows 服务

第二步: 在windows 服务  设计页面上  点击右键 (安装服务)  将会生成一个新的安装文件(ProjectInstaller.cs)

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Linq;
using System.ServiceProcess;


namespace heartbeat
{
    [RunInstaller(true)]
    public partial class ProjectInstaller : Installer
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        private ServiceInstaller sInstall;
        private ServiceProcessInstaller sProcessInstall;
        public ProjectInstaller()
        {
            sInstall = new ServiceInstaller();
            sProcessInstall = new ServiceProcessInstaller();
            sProcessInstall.Account = ServiceAccount.LocalSystem;
            sInstall.StartType = ServiceStartMode.Automatic;
            sInstall.ServiceName = "Service1"; //这个服务名必须和步骤1中的服务名相同。 
            sInstall.DisplayName = "移动采编接口服务";
            sInstall.Description = "移动采编接口的心跳服务";
            Installers.Add(sInstall);
            Installers.Add(sProcessInstall);
        }

    }
}

第三步: 在ProjectInstaller.cs 文件的 设计界面上 有2个图标 可以分别点击右键 设置属性

第四步: 定时器的添加

          private System.Timers.Timer timer1;

          protected override void OnStart(string[] args)
        {
            this.timer1 = new System.Timers.Timer(1000 * 5);
            this.timer1.Enabled = true;
            this.timer1.Elapsed += this.timer_Main_Tick;
            this.timer1.Start();
        }

第五步: 将服务进行部署

          将 InstallUtil.exe 复制到您的debug目录下面

        在工程的debug目录下面 建立   注册.bat (installutil  heartbeat.exe) 和 取消注册.bat (installutil  heartbeat.exe /u)

第六步: 调试

      首先将服务启动

      在开发环境里面  菜单/调试/附加到进程 (显示所有用户的进程) 选择您的进程

      (例如 暂停里面有断点)

     点击服务的暂停 会进行调试

       

posted @   郑文亮  阅读(456)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· [AI/GPT/综述] AI Agent的设计模式综述
历史上的今天:
2011-03-27 推荐60个jQuery插件(转)
点击右上角即可分享
微信分享提示