如何从Visual Studio运行(F5)Windows服务

niknxzdl  于 2023-11-21  发布在  Windows
关注(0)|答案(9)|浏览(223)

如何从visual studio运行windows服务项目。
我在visual studio 2008中构建了一个windows服务,我必须总是从控制面板运行服务,然后将调试器附加到服务的运行示例上。这有点烦人,因为我清理了很多代码,并且在开发过程中需要多次重新启动我的服务。
我想设置我的项目,以便能够按下F5并运行服务,并直接进入调试模式。关于如何实现这一点的一些提示将是伟大的。
提前感谢!

knsnq2tg

knsnq2tg1#

转载自here

  1. static void Main(string[] args)
  2. {
  3. DemoService service = new DemoService();
  4. if (Environment.UserInteractive)
  5. {
  6. service.OnStart(args);
  7. Console.WriteLine("Press any key to stop program");
  8. Console.Read();
  9. service.OnStop();
  10. }
  11. else
  12. {
  13. ServiceBase.Run(service);
  14. }
  15. }

字符串
这应该允许您从Visual Studio中运行。
另一种方法是通过调用System.Diagnostics.Debugger.Break()在代码中嵌入编程断点。当您将其放入服务的OnStart()回调并从服务控制台启动服务时,编程断点将触发一个对话框,允许您附加到Visual Studio的现有示例或启动新示例。这实际上是我用来调试服务的机制。

展开查看全部
dddzy1tm

dddzy1tm2#

在你的Main()例程中检查Debugger.IsAttached,如果它是真的,就像一个控制台一样启动你的应用程序,如果不是,就调用ServiceBase.Run()

pkln4tw6

pkln4tw63#

可以为Windows Service设置一个配套项目,该项目作为控制台应用程序运行,但使用反射访问服务方法。有关详细信息和示例,请参阅此处:http://ryan.kohn.ca/articles/how-to-debug-a-windows-service-in-csharp-using-reflection/
下面是控制台应用程序中需要的相关代码:

  1. using System;
  2. using System.Reflection;
  3. namespace TestableWindowsService
  4. {
  5. class TestProgram
  6. {
  7. static void Main()
  8. {
  9. Service1 service = new Service1();
  10. Type service1Type = typeof (Service1);
  11. MethodInfo onStart = service1Type.GetMethod("OnStart", BindingFlags.NonPublic | BindingFlags.Instance); //retrieve the OnStart method so it can be called from here
  12. onStart.Invoke(service, new object[] {null}); //call the OnStart method
  13. }
  14. }
  15. }

字符串

展开查看全部
v2g6jxz6

v2g6jxz64#

你也可以这样做:(见注解的解释)

  1. public class Program : ServiceBase
  2. {
  3. private ServiceHost _serviceHost = null;
  4. public Program()
  5. {
  6. ServiceName = "";
  7. }
  8. /// <summary>
  9. /// The main entry point for the application.
  10. /// </summary>
  11. static void Main()
  12. {
  13. #if(!DEBUG)
  14. // when deployed(built on release Configuration) to machine as windows service use this
  15. ServiceBase[] ServicesToRun;
  16. ServicesToRun = new ServiceBase[] { new Program() };
  17. ServiceBase.Run(ServicesToRun);
  18. #else
  19. // when debugging use this (When debugging after building in Debug Configuration)
  20. //If you want the DEBUG preprocessor constant in Release you will have to check it on in the project configuration
  21. Program progsvc = new Program();
  22. progsvc.OnStart(new string[] { });
  23. System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
  24. #endif
  25. }
  26. protected override void OnStart(string[] args)
  27. {
  28. // Start Web Service
  29. if (_serviceHost != null)
  30. {
  31. _serviceHost.Close();
  32. }
  33. _serviceHost = new ServiceHost(typeof(Namespace.Service));
  34. _serviceHost.Open();
  35. }
  36. }

字符串

展开查看全部
svmlkihl

svmlkihl5#

只需从服务构造函数调用OnStart()事件
我是这样做的

  1. public XXX()
  2. {
  3. InitializeComponent();
  4. OnStart(new string[] { "shafi", "moshy" });
  5. }

字符串

q5lcpyga

q5lcpyga6#

创建一个单独的项目,它只引用服务项目,并示例化和启动服务。它就像一个普通的应用程序一样运行,你可以进入它。

  1. YourService s = new YourService();
  2. s.Start();

字符串

kzmpq1sx

kzmpq1sx7#

如果你想让你的windows服务作为一个shell,那么里面应该有很少的代码,这样你就不必测试它了。
你应该在一个类中拥有你想让你的服务做的每一件事。
你可以单元测试你的类,如果它工作,然后引用它到你的服务。
这样,当你让你的类做你想做的每一件事,然后当它应用到你的服务时,每一件事都应该工作。
将一个事件日志,你可以看到你的服务正在做什么,而它正在运行,也是一个很好的方式来测试:D尝试。

  1. namespace WindowsService
  2. {
  3. public partial class MyService : ServiceBase
  4. {
  5. public MyEmailService()
  6. {
  7. InitializeComponent();
  8. if (!System.Diagnostics.EventLog.SourceExists("MySource")) // Log every event
  9. {
  10. System.Diagnostics.EventLog.CreateEventSource(
  11. "MySource", "MyNewLog"); // Create event source can view in Server explorer
  12. }
  13. eventLogEmail.Source = "MySource";
  14. eventLogEmail.Log = "MyNewLog";
  15. clsRetriveEmail Emails = new clsRetriveEmail();
  16. eventLogEmail.WriteEntry("Populateing database with mail"); // log event
  17. Emails.EmailGetList(); // Call class
  18. }
  19. protected override void OnStart(string[] args)
  20. {
  21. eventLogEmail.WriteEntry("Started");
  22. }
  23. protected override void OnStop()
  24. {
  25. eventLogEmail.WriteEntry("Stopped");
  26. }
  27. protected override void OnContinue()
  28. {
  29. eventLogEmail.WriteEntry("Continuing");
  30. }
  31. }
  32. }

字符串

展开查看全部
rseugnpd

rseugnpd8#

这些链接在使用服务时非常有用。
这是一个步行虽然创造他们http://msdn.microsoft.com/en-us/library/zt39148a.aspx
James Michael Hare在他的博客http://geekswithblogs.net/BlackRabbitCoder/上写了一个他制作的非常好的模板/框架,使开发(和调试)Windows服务变得更加容易:C#模板:一个可扩展的,自安装的Windows服务模板(1/2)http://geekswithblogs.net/BlackRabbitCoder/archive/2010/09/23/c-windows-services-1-of-2-creating-a-debuggable-windows.aspx
它为您提供了快速入门所需的所有基础知识。最重要的是,它给予您一种非常好的方式来调试您的服务,就像它是一个普通的控制台应用程序一样。我还可以提到,它提供了开箱即用的功能来安装(和卸载)您的服务。这篇文章的第二部分可以在这个链接中找到。
我自己用过几次,真的可以推荐它。

r3i60tvu

r3i60tvu9#

13年后,使用最新的C# .NET Core和所有。我尝试了Matt Davis(保护访问级别的问题)和Ryan Kohn(传递参数的问题,也不能通过反射调用OnStart)的解决方案,但没有成功。最后,我能够通过微小的调整完成它,我认为这可能是一个更好的方法。
在您的服务中添加Start功能:

  1. public partial class MyService : ServiceBase {
  2. public void Start(string[] args = null)
  3. {
  4. OnStart(args);
  5. }
  6. }

字符串
Main中,像这样使用它而不需要任何反射:

  1. static void Main(string[] args)
  2. {
  3. MyService service = new MyService ();
  4. if (Environment.UserInteractive)
  5. {
  6. service.Start(args);
  7. if (!Debugger.IsAttached)
  8. {
  9. Console.WriteLine("Press any key to stop program");
  10. Console.Read();
  11. service.Stop();
  12. Environment.Exit(0);
  13. }
  14. }
  15. else
  16. {
  17. ServiceBase.Run(service);
  18. }
  19. }


您可以在Visual Code模式下运行该服务,也可以作为常规CLI命令运行该服务。不同之处在于,当您在VScode中单击Stop模式时,不会执行OnStop代码。

展开查看全部

相关问题