流程1、更改项目 应用程序——输出类型——windows应用程序 改为 控制台应用程序2、Program启动类中添加调用代码3、服务类里面添加启动方法去启动OnStart和 Console.ReadLine();停止OnStop方法。 操作1、更改项...
流程
1、更改项目 应用程序——输出类型——windows应用程序 改为 控制台应用程序
2、Program启动类中添加调用代码
3、服务类里面添加启动方法去启动OnStart和 Console.ReadLine();停止OnStop方法。
操作
1、更改项目
2、Program添加代码
/// <summary> /// 应用程序的主入口点。 /// </summary> static void Main() { #if DEBUG //调试的时候记得把 应用程序——输出类型——windows应用程序 改为 控制台应用程序 if (Environment.UserInteractive) { Service1 service1 = new Service1(); service1.TestStartupAndStop(); } #endif #if !DEBUG
//下面这块是调用服务的,创建服务自动生成的
ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(ServicesToRun); #endif }
3、服务类里面添加方法调用start和stop
/// <summary> /// 添加个内部方法,用于调试 /// </summary> /// <param name="args"></param> internal void TestStartupAndStop() { #if DEBUG this.OnStart(new string[0]); Console.ReadLine(); this.OnStop(); #endif }
沃梦达教程
本文标题为:随笔——写windows服务的时候如何调试 c# .net
猜你喜欢
- SQL查询到LINQ C#[加入多个表] 2023-11-13
- Unity实现3D射箭小游戏 2023-04-15
- P/Invoke之C#调用动态链接库DLL示例详解 2023-07-18
- C# 中属性PropertyInfo的setvalue用法说明 2023-03-29
- C#运行程序时阻止关闭显示器和系统待机 2023-06-15
- C#基于HttpWebRequest实现发送HTTP请求的方法分析 2023-01-11
- C#-UWP Windows Store应用程序上的TLS客户端证书认证 2023-11-16
- C#实现Windows服务创建、安装 2023-11-14
- ajaxFileUpload插件,C#返回Json数据报错问题的解决方案 2022-12-02
- .Net Core 5 MVC 接受 Post 参数 2023-09-27