简体   繁体   English

如何创建一个独立的exe应用程序的Windows服务?

[英]How can I create a windows service that is a standalone exe application?

I would like to create an console exe application, that may be run as a standalone application as well as a windows service. 我想创建一个控制台exe应用程序,它可以作为独立的应用程序以及Windows服务运行。 Is it possible to do? 有可能吗? What are actually the benefits of using svchost? 使用svchost实际上有什么好处?

Rewrite your windows service's main method like this, and it will be console application if you run with parametr -c, and yes don't forgot to change project type to console from project's property window 重写你的windows服务的主要方法,如果你使用parametr -c运行它将是控制台应用程序,是的,不要忘记从项目的属性窗口将项目类型更改为控制台

 public static void Main(string[] args)
 {
      Service service = new Service();
      if (args.Contains("-c", StringComparer.InvariantCultureIgnoreCase) || args.Contains("-console", StringComparer.InvariantCultureIgnoreCase))
      {
           service.StartService(args);
           Console.ReadLine();
           service.StopService();
           return;
      }
      ServiceBase[] ServicesToRun = new ServiceBase[] 
                                    { 
                                        service 
                                    };
      ServiceBase.Run(ServicesToRun);           
  }

StartService and StopService just call service's overrided OnStart, and OnStop methodes StartService和StopService只调用服务的覆盖OnStart和OnStop方法

The best approach to this cases is to think about the notion of a component. 这种情况的最佳方法是考虑组件的概念。

Your strategy should be develop a DLL with all the logic you need. 您的策略应该是开发一个具有您需要的所有逻辑的DLL。

After that, you build a console application and windows service that use that DLL. 之后,您构建一个使用该DLL的控制台应用程序和Windows服务。

The notion of components and the possibility to reuse them is trivial and high advisable. 组件的概念和重用它们的可能性是微不足道的,并且是可取的。

When you need to change your logic, you only must change the DLL, and replace with the old one. 当您需要更改逻辑时,您只需更改DLL,并替换旧的DLL。 You don't need any kind of special deploy. 您不需要任何特殊部署。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows服务:如何将多个Windows服务创建为多个exe文件 - Windows Service: How can i create multiple windows service as a multiple exe file 如何部署使用SQL Server的Windows应用程序(独立EXE) - How To Deploy A Windows Application That Uses SQL Server (Standalone EXE) 如何在C#中创建可以与GUI *或*一起运行的Windows应用程序? - How can I create a Windows application that can run with a GUI *or* as a Windows service in C#? 是否可以创建部署为 EXE 或 Windows 服务的独立 C# Web 服务? - Is it possible to create a standalone, C# web service deployed as an EXE or Windows service? 如何从Windows服务文件生成exe - how can i generate exe from windows service file 如何制作Windows服务应用程序,以便它也可以作为独立程序运行? - How to make windows service application so it can run as a standalone program as well? 我可以创建 C# 独立应用程序而不是在 Windows 中安装吗? - Can I Create C# Standalone Application Than Runs Without Installing in Windows? 创建一个独立的 .exe 文件 - Create a standalone .exe file 我如何使用 heat.exe(或类似的)从 Windows 服务中获取 COM 注册? - How can I use heat.exe (or similar) to harvest COM registration from a Windows Service? 如何使用 C# 从 Windows 服务运行 EXE 程序? - How can I run an EXE program from a Windows Service using C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM