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. m3u8转mp4

    先进行一波操作 新建一个文件夹,里面床两个txt文件 如图 里面随意写一些内容 之后新建一个demo.bat文件.里面输入 copy /b 1.txt+2.txt new.txt 之后双击会有一个ne ...

  2. 【AI in 美团】深度学习在文本领域的应用

    背景 近几年以深度学习技术为核心的人工智能得到广泛的关注,无论是学术界还是工业界,它们都把深度学习作为研究应用的焦点.而深度学习技术突飞猛进的发展离不开海量数据的积累.计算能力的提升和算法模型的改进. ...

  3. 【SQL】184. Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  4. 机器学习之路: tensorflow 自定义 损失函数

    git: https://github.com/linyi0604/MachineLearning/tree/master/07_tensorflow/ import tensorflow as tf ...

  5. android studio 添加有趣的注释模板 佛祖保佑无bug等

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 汉化包 百度云盘 下载地址:https://pan.baidu.com/s/1pLjwy ...

  6. 直接插入排序(高级版)之C++实现

    直接插入排序(高级版)之C++实现 一.源代码:InsertSortHigh.cpp /*直接插入排序思想: 假设待排序的记录存放在数组R[1..n]中.初始时,R[1]自成1个有序区,无序区为R[2 ...

  7. BZOJ2111 ZJOI2010排列计数

    根据Pi>Pi/2可以看出来这是一个二叉树 所以我们可以用树形DP的思想 f[i]=f[i<<1]*f[i<<1|1]*C(s[i]-1,s[i<<1]),s ...

  8. 2017-2018-1 JAVA实验站 第八周作业

    2017-2018-1 JAVA实验站 第八周作业 详情请见团队博客

  9. HTML5 UI 控件Mobiscroll的使用(年月日三级联动)

    概述: 遇到制作一个html5界面,需要选择年月日,其实这个功能很常用.一般我们都是网上找,之前也没有收藏一个自己常用的,今天发现一个不错的库.特此记录一下使用过程,以便以后遇到了方面查阅. 1.官方 ...

  10. Codeforces Round #356 (Div. 2) A. Bear and Five Cards 水题

    A. Bear and Five Cards 题目连接: http://www.codeforces.com/contest/680/problem/A Description A little be ...