VS2013创建Windows服务与调试服务的图文方法
作者:Pete-Jones 发布时间:2022-01-15 02:23:14
标签:调试服务,VS2013,Windows服务
1、创建Windows服务
说明:
a)Description 服务描述,直接显示到Windows服务列表中的描述;
b)DisplayName 服务显示名称,直接显示到Windows服务列表中的名称;
c)ServiceName 服务进程名称,安装与卸载服务时的唯一标识。
单击“serviceProcessInstaller1”,在其属性窗口中设置Account帐号方式,建议为LocalService(当然也可以Account属性改为 LocalSystem,这样,不论是以哪个用户登录的系统,服务总会启动)。
编写安装和卸载脚本,并将放在bin/debug或bin/Release文件夹下。
安装脚本
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe %~dp0exe程序的名称.exe
Net Start 服务名称
sc config 服务名称 start= auto
pause
这里注意,在exe程序的名称前面有 %~dp0 这是代表当前位置
服务名称 对应 上面我们创建服务时ServerName的名称
卸载脚本
%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe /u %~dp0exe程序的名称.exe
pause
同时还要注意一下,本人用的.NET4.0的版本,所以\Microsoft.NET\Framework\v4.0.30319\installutil.exe 这一段要根据你机器安装.NET的版本来定。
其实脚本主要是通过installutil.exe 来进行安装和卸载服务的,同时此处涉及的批处理命令不多。
2、调试windows服务
在项目中不用启动windows服务项目,而是直接附加进程来进行调试。
在可用进程中,查找到你刚才通过脚本安装的服务就可以了。
再发一个写入服务代码的Demo
public partial class MMSServer : ServiceBase
{
private Timer time = new Timer();
public MMSServer()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
#if DEBUG
if (!Debugger.IsAttached)
Debugger.Launch();
Debugger.Break();
#endif
WriteLog("服务启动,时间:" + DateTime.Now.ToString("HH:mm:ss") + "\r\n");
time.Elapsed += new ElapsedEventHandler(MethodEvent);
time.Interval = 3 * 1000;
time.Start();
}
protected override void OnPause()
{
#if DEBUG
if (!Debugger.IsAttached)
Debugger.Launch();
Debugger.Break();
#endif
WriteLog("服务暂停,时间:" + DateTime.Now.ToString("HH:mm:ss") + "\r\n");
base.OnPause();
}
protected override void OnContinue()
{
#if DEBUG
if (!Debugger.IsAttached)
Debugger.Launch();
Debugger.Break();
#endif
WriteLog("服务恢复,时间:" + DateTime.Now.ToString("HH:mm:ss") + "\r\n");
base.OnContinue();
}
protected override void OnShutdown()
{
WriteLog("计算机关闭,时间:" + DateTime.Now.ToString("HH:mm:ss") + "\r\n");
base.OnShutdown();
}
private void MethodEvent(object source, System.Timers.ElapsedEventArgs e)
{
time.Enabled = false;
string result = string.Empty;
try
{
//.........
result = "执行成功,时间:" + DateTime.Now.ToString("HH:mm:ss") + "\r\n";
}
catch (Exception ex)
{
result = "执行失败,原因:" + ex.Message + "\r\n";
}
finally
{
WriteLog(result);
time.Enabled = true;
}
}
protected override void OnStop()
{
#if DEBUG
if (!Debugger.IsAttached)
Debugger.Launch();
Debugger.Break();
#endif
WriteLog("服务停止,时间:" + DateTime.Now.ToString("HH:mm:ss") + "\r\n");
}
/// <summary>
/// 日志记录
/// </summary>
/// <param name="logInfo"></param>
private void WriteLog(string logInfo)
{
try
{
string logDirectory = AppDomain.CurrentDomain.BaseDirectory + "\\Logs";
if (!Directory.Exists(logDirectory))
{
Directory.CreateDirectory(logDirectory);
}
string filePath = logDirectory + "\\" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
File.AppendAllText(filePath, logInfo);
}
catch
{
}
}
}
0
投稿
猜你喜欢
- 近期工作内容需要涉及到相机开发,其中一个功能点就是实现一个相机预览页底部的滑动指示器,现在整理出来供大家讨论参考。先上一张图看下效果:主要实
- 前面聊的最近在调试项目的亮屏速度,我们希望在按下power键后到亮屏这个时间能达到500MS以内,在Rockchip 3399和3288上面
- 简介Springboot Admin是一个管理和监控Springboot项目的组件,分为服务端和客户端,两端通过http进行通信。由于其轻量
- 看前欣赏美图!上才艺!目录 一.顺序结构二.分支结构1.if语句2.switch 语句 三. 循环结构1 .while 循环2. break
- 正则: “.”和"\""."点儿,在正则表达式中表示任意一个字符。"\"在正则表
- 本文实例讲述了C#编程简单实现生成PDF文档的方法。分享给大家供大家参考,具体如下:using System;using System.IO
- Android 完全退出的实例详解首先,在基类BaseActivity里,注册RxBus监听:public class BaseActivi
- 用java实现简易外卖订餐系统,供大家参考,具体内容如下一、使用技术javaSE二、实现功能外卖订餐系统具体要求如下:使用选择结构,循环结构
- 开篇本例是在springboot整合H2内存数据库,实现单元测试与数据库无关性和使用RestTemplate消费spring boot的Re
- 前言Java 8 提供了一组称为 stream 的 API,用于处理可遍历的流式数据。stream API 的设计,充分融合了函数式编程的理
- 译者注:个人觉得用定时任务来跑垃圾回收不是很好的例子,从译者接触到的项目来看,比较常见的是用定时任务来进行非实时计算,清除临时数据、文件等。
- SpringMVC注解@RequestMapping在之前的 hello world 示例中,用到了 @RequestMapping 注解,
- 1.通过UnityHub安装unityUnityHub下载路径,UnityHub安装按照步骤来就可以了。打开UnityHub,点安装。3.选
- Java程序有的时候在主线程中会创建多个线程去执行任务,然后在主线程执行完毕之前,把所有线程的任务进行汇总,以前可以用线程的join方法,但
- 大家可能在做app的时候,或多或少需要使用联系人,而根据google提供的api,你需要编写大量的代码,例如首先需要查询数据库,涉及到数据库
- java多线程的同步方法实例代码先看一个段有关银行存钱的代码:class Bank { private int su
- 什么是Handler Handler是Android消息机制的上层接口,它为我们封装了许多底层的细节,让
- 大家好,欢迎来到老胡的博客,今天我们继续了解设计模式中的职责链模式,这是一个比较简单的模式。跟往常一样,我们还是从一个真实世界的例子入手,这
- final——final用于类表示该类不能派生子类。final用于方法表示该方法不能被子类重写。final用于变量时表示常量,类似于C/C+
- 多态是同一个行为具有多个不同表现形式或形态的能力。多态性意味着有多重形式。在面向对象编程范式中,多态性往往表现为"一个接口,多个功