Have you got tired of attaching the Visual Studio debugger to the service application? I got the solution just for you! It’s a small helper class containing a static method which you need to invoke.

public static void Main(string[] argv)
{
// just include this check, "Service1" is the name of your service class.
if (WindowsServiceHelper.RunAsConsoleIfRequested<Service1>())
return; // all other code
}

Then go to project properties, the “Debug” tab and add “-console” as Command Arguments.

How to configure Visual Studio

That’s it. What I do is simply allocate a console using the winapi and then invoke (through reflection) the properprotected methods in your service class.

Source code for the helper class:

public static class WindowsServiceHelper
{
[DllImport("kernel32")]
static extern bool AllocConsole(); public static bool RunAsConsoleIfRequested<t>() where T : ServiceBase, new()
{
if (!Environment.CommandLine.Contains("-console"))
return false; var args = Environment.GetCommandLineArgs().Where
(name => name != "-console").ToArray(); AllocConsole(); var service = new T();
var onstart = service.GetType().GetMethod("OnStart",
BindingFlags.Instance | BindingFlags.NonPublic);
onstart.Invoke(service, new object[] {args}); Console.WriteLine("Your service named '" + service.GetType().FullName +
"' is up and running.\r\nPress 'ENTER' to stop it.");
Console.ReadLine(); var onstop = service.GetType().GetMethod("OnStop",
BindingFlags.Instance | BindingFlags.NonPublic);
onstop.Invoke(service, null);
return true;
}
}

An easier way to debug windows services的更多相关文章

  1. 用C#创建Windows服务(Windows Services)

    用C#创建Windows服务(Windows Services) 学习:  第一步:创建服务框架 创建一个新的 Windows 服务项目,可以从Visual C# 工程中选取 Windows 服务(W ...

  2. Professional C# 6 and .NET Core 1.0 - Chapter 39 Windows Services

    本文内容为转载,供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - Chapter 39 Windows Servi ...

  3. 当程序以Windows Services形式启动时当前路径不对

    当程序以Windows Services形式启动时当前路径不对 @(操作系统)[博客|dotNet] 很多时候我们需要将我们的程序写成利用Windows服务的形式来让它能够自启动.今天遇到一个问题,当 ...

  4. Windows Services Windows Services的操作

    Windows Services的操作 一.服务的创建: 1.新建项目——Windows服务 2.这是每个人都会犯的错误,新建一个项目后,都会按F5(运行),就会出现如下错误: 3.安装服务有很多种方 ...

  5. the service mysql56 was not found in the Windows services的解决办法

    mysql无法启动,无法改变状态-CSDN论坛-CSDN.NET-中国最大的IT技术社区 http://bbs.csdn.net/topics/390943788   具体描述: 关闭,重启mysql ...

  6. Windows Services的1053错误的解决办法之一:修改注册表允许的响应时间

    Error: 'The service did not respond in a timely fashion' (ServicesPipeTimeout) when attempting when ...

  7. Windows Services(NT)

    本文主要记录什么是Windows Service,及其主要组成?并通过一个列子来创建一个Windows Services,同时,记录几个在查资料碰到的问题. Windows Services全文简称N ...

  8. C# windows服务:创建Windows服务(Windows Services)的一般步骤

    C#创建Windows服务(Windows Services) Windows服务在Visual Studio 以前的版本中叫NT服务,在VS.net启用了新的名称.用Visual C# 创建Wind ...

  9. Topshelf + QuartzNet 实现挂载在 WIndows Services 中的定时任务

    直接贴代码了: 首先我们可以把所有的 Job 放到一个单独的 DLL 中,好处是可以共享这些业务 Job.比如我们新建一个 QuartzNetDemo.WinService.Jobs 的类库. 然后, ...

随机推荐

  1. 华三IRF的配置

    https://blog.csdn.net/VictoryKingLIU/article/details/79255901 拓扑结构 1 配置成员编号(重启) 2 配置成员优先级(大的主设备) 3 配 ...

  2. Oracle登录

    一.安装Oracle 二.打开Database Configuration Assistant 三.打开SQL Plus 1.打开SQL Plus 2.输入用户名sys 3.输入口令1 as sysd ...

  3. linux/mac下命令行rm回收站--rmtrash

    Linux.mac的命令行下没有回收站功能,很多时候手一抖就把重要文件给 rm -fr * 了,虽然linux下有可能通过lost +found/debugfs找回,但难度也比较大,不能保证一定能够找 ...

  4. 使用GIT进行源码管理——GUI客户端

    很多人对GIT GUI客户端是非常不屑一顾的,但我非常喜欢GUI的方便快捷,也不用记忆冗杂的命令,本文简单的介绍了几种免费的Windows下的GIT客户端,方便大家使用. Git for Window ...

  5. mysql store procedure 存储过程

    参考资料: 1.http://blog.sina.com.cn/s/blog_52d20fbf0100ofd5.html 2.https://dev.mysql.com/doc/refman/5.7/ ...

  6. BZOJ 4003: [JLOI2015]城池攻占 左偏树 可并堆

    https://www.lydsy.com/JudgeOnline/problem.php?id=4003 感觉就是……普通的堆啊(暴论),因为这个堆是通过递归往右堆里加一个新堆或者新节点的,所以要始 ...

  7. 【漏洞预警】Apache ActiveMQ Fileserver远程代码执行漏洞(CVE-2016-3088)

    漏洞编码:CVE-2016-3088 实验环境:Linux Apache ActiveMQ版本号:Apache ActiveMQ 5.7.0 ----------------------------- ...

  8. Java基础学习——多线程之线程池

    1.线程池介绍     线程池是一种线程使用模式.线程由于具有空闲(eg:等待返回值)和繁忙这种不同状态,当数量过多时其创建.销毁.调度等都会带来开销.线程池维护了多个线程,当分配可并发执行的任务时, ...

  9. HDU 5715 XOR 游戏 二分+字典树

    XOR 游戏 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5715 Description 众所周知,度度熊喜欢XOR运算(XOR百科). 今天,它 ...

  10. 自动打怪 c#

    其中思路很简单,单线程的一个乱七八糟的游戏 预计会更新背包,背包这个估计会用一个vector来存 图形的话,我得催催我的美工大人,她会帮我弄吧,哇哈哈 界面: namespace auttompk { ...