一个基于 .NET 中的命令行参数作为控制台或 win32 服务启动的可执行文件?
是否可以在.net中编写可执行应用程序,该应用程序可以注册为Win32服务并作为服务运行,但出于调试目的可以作为带有控制台输出的控制台应用程序启动(作为性能计数器,文本日志文件对于监视不是很有用)
或者我是否应该将应用程序逻辑(Application 类)放在单独的程序集中,并编写两个完全不同的可执行包装器(win32 服务和控制台),使用 Application
类引用此库?
控制台
main()
{
Application.Start();
Console.Readkey();
Application.Stop();
}
Win32 服务
OnStart()
{
Application.Start();
}
OnStop()
{
Application.Stop();
}
Is it possible to write executable application in .net that can be registered as Win32 Service and operate as service but for debug purposes can be launched as console application with console output (as performance counters, text log files are not very useful for monitoring)
Or shall I put application logic (Application class) in a separate assembly and write two completely different executable wrappers (win32 service and console) that reference this library with Application
class?
Console
main()
{
Application.Start();
Console.Readkey();
Application.Stop();
}
Win32 Service
OnStart()
{
Application.Start();
}
OnStop()
{
Application.Stop();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,我以前做过这个。我创建了一个windoes服务项目,将项目属性(来自Windows应用程序)的类型更改为控制台应用程序,并添加了基于命令行参数的条件:
UserInteractive
也可以做到这一点。Yes I have done this before. I created a windoes service project, changed the type to Console Application on project properties (from windows application) and added a condition based on command line parameter:
UserInteractive
can also do the trick.在您的
Main()
方法中检测您是否想作为控制台或服务运行。如果作为控制台运行,只需直接调用OnStart()
即可。如果将 a 作为服务运行,请调用Run()
。In your
Main()
method detect if you want to run as a console or service. If running as a console, just callOnStart()
directly. If running a as a service, callRun()
.