一个基于 .NET 中的命令行参数作为控制台或 win32 服务启动的可执行文件?

发布于 2024-09-29 17:51:15 字数 486 浏览 0 评论 0原文

是否可以在.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云之铃。 2024-10-06 17:51:15

是的,我以前做过这个。我创建了一个windoes服务项目,将项目属性(来自Windows应用程序)的类型更改为控制台应用程序,并添加了基于命令行参数的条件:

/// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main(string[] args)
    {

        // setup DI 
        BootStrapper.Setup();

        if (!Environment.UserInteractive)
        {
            ServiceManager serviceManager = new ServiceManager();
            serviceManager.Start();
        }
        else if(args.Length==1 && args[0]==HarnessGuid)
        {
            RunInHarnessMode();
        }
        else
        {
            ServiceBase[] servicesToRun = new ServiceBase[] { new ServiceManager() };
            ServiceBase.Run(servicesToRun);
        }       
    }

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:

/// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main(string[] args)
    {

        // setup DI 
        BootStrapper.Setup();

        if (!Environment.UserInteractive)
        {
            ServiceManager serviceManager = new ServiceManager();
            serviceManager.Start();
        }
        else if(args.Length==1 && args[0]==HarnessGuid)
        {
            RunInHarnessMode();
        }
        else
        {
            ServiceBase[] servicesToRun = new ServiceBase[] { new ServiceManager() };
            ServiceBase.Run(servicesToRun);
        }       
    }

UserInteractive can also do the trick.

一笑百媚生 2024-10-06 17:51:15

在您的 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 call OnStart() directly. If running a as a service, call Run().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文