An easier way to debug windows services
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.

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的更多相关文章
- 用C#创建Windows服务(Windows Services)
用C#创建Windows服务(Windows Services) 学习: 第一步:创建服务框架 创建一个新的 Windows 服务项目,可以从Visual C# 工程中选取 Windows 服务(W ...
- Professional C# 6 and .NET Core 1.0 - Chapter 39 Windows Services
本文内容为转载,供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - Chapter 39 Windows Servi ...
- 当程序以Windows Services形式启动时当前路径不对
当程序以Windows Services形式启动时当前路径不对 @(操作系统)[博客|dotNet] 很多时候我们需要将我们的程序写成利用Windows服务的形式来让它能够自启动.今天遇到一个问题,当 ...
- Windows Services Windows Services的操作
Windows Services的操作 一.服务的创建: 1.新建项目——Windows服务 2.这是每个人都会犯的错误,新建一个项目后,都会按F5(运行),就会出现如下错误: 3.安装服务有很多种方 ...
- the service mysql56 was not found in the Windows services的解决办法
mysql无法启动,无法改变状态-CSDN论坛-CSDN.NET-中国最大的IT技术社区 http://bbs.csdn.net/topics/390943788 具体描述: 关闭,重启mysql ...
- Windows Services的1053错误的解决办法之一:修改注册表允许的响应时间
Error: 'The service did not respond in a timely fashion' (ServicesPipeTimeout) when attempting when ...
- Windows Services(NT)
本文主要记录什么是Windows Service,及其主要组成?并通过一个列子来创建一个Windows Services,同时,记录几个在查资料碰到的问题. Windows Services全文简称N ...
- C# windows服务:创建Windows服务(Windows Services)的一般步骤
C#创建Windows服务(Windows Services) Windows服务在Visual Studio 以前的版本中叫NT服务,在VS.net启用了新的名称.用Visual C# 创建Wind ...
- Topshelf + QuartzNet 实现挂载在 WIndows Services 中的定时任务
直接贴代码了: 首先我们可以把所有的 Job 放到一个单独的 DLL 中,好处是可以共享这些业务 Job.比如我们新建一个 QuartzNetDemo.WinService.Jobs 的类库. 然后, ...
随机推荐
- vue 组件使用中的细节点
1.is属性 有些 HTML 元素,诸如 <ul>.<ol>.<table> 和 <select>,对于哪些元素可以出现在其内部是有严格限制的.而有些元 ...
- [leetcode sort]147. Insertion Sort List
Sort a linked list using insertion sort. 利用插入排序对一个链表进行排序 思路和数组中的插入排序一样,不过每次都要从链表头部找一个合适的位置,而不是像数组一样可 ...
- iOS Sprite Kit教程之xcode安装以及苹果帐号绑定
iOS Sprite Kit教程之xcode安装以及苹果帐号绑定 其它的网站上下载安装Xcode 有时候,应用商店下载较慢,所以用户也可以选择从其他网站下载Xcode安装文件.下面讲解这种Xcode的 ...
- MySql 模糊查询、范围查询
实例: SQL模糊查询,使用like比较关键字,加上SQL里的通配符,请参考以下: 1.LIKE'Mc%' 将搜索以字母 Mc 开头的所有字符串(如 McBadden). 2.LIKE'%inger' ...
- [AGC016E]Poor Turkeys
[AGC016E]Poor Turkeys 题目大意: 有\(n(n\le400)\)只火鸡,编号为\(1\)到\(n\),有\(m(m\le10^5)\)个人,每人指定了两只火鸡\(x\)和\(y\ ...
- MySQL数据库基准压力测试工具之MySQLSlap使用实例
一.Mysqlslap介绍 mysqlslap是MySQL5.1之后自带的benchmark基准测试工具,类似Apache Bench负载产生工具,生成schema,装载数据,执行benckmark和 ...
- C++反汇编-虚函数
学无止尽,积土成山,积水成渊-<C++反汇编与逆向分析技术揭秘> 读书笔记 在C++中,使用关键字virtual声明为虚函数. 虚函数地址表(虚表) 定义:当类中定义有虚函数时,编译器会把 ...
- 移动平台的WebApp之Meta标签
对于桌面平台web布局中大家对meta标签再熟悉不过了,它永远位于 head 元素内部,对做SEO的朋友一定对meta有种特殊的感情吧,今天我们就来说说移动平台的meta标签,在移动平台meta标签究 ...
- Linux内核hlist数据结构分析
在内核编程中哈希链表hlist使用非常多,比方在openvswitch中流表的存储中就使用了(见[1]).hlist的表头仅有一个指向首节点的指针.而没有指向尾节点的指针,这样在有非常多个b ...
- C/C++ 函数指针 总结
什么是函数指针 就像某一变量的地址可以存储在相应的指针变量中一样,指向函数的指针中保存着函数代码起始处的地址 函数指针的声明 当声明一个函数指针时,必须声明它指向的函数类型.要指定函数类型,就要指出函 ...