如何使Windows服务应用程序,以便它可以作为一个独立的程序,以及运行? [英] How to make windows service application so it can run as a standalone program as well?

查看:148
本文介绍了如何使Windows服务应用程序,以便它可以作为一个独立的程序,以及运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将用一个例子开始:Apache Web服务器(Windows下)有一个很好的功能:它可以运行为独立的应用程序(当前用户的权限),而且它可以安装并运行为Windows服务直接(如本地系统帐户),使用相同的可执行文件。

I'll start with an example: Apache web server (under Windows) has a nice feature: it can be both run as a standalone application (with current users privileges), and that it can be installed and run as a windows service directly (as local system account), using same executable.

为了应用程序来运行作为一个独立的应用程序,它需要做的全部是沿行在一些公共类具有静态公共的Main()。

In order for application to be run as a standalone app, all it needs to do is along the lines of having static public Main() in some public class.

为了让应用程序能够安装和可运行的服务,它必须实现某种方式ServiceBase和安装程序类。但是,如果应用程序是这样运行作为独立的应用程序,它会显示消息框。

In order for application to be installable and runnable as service, it has to implement ServiceBase and Installer classes in certain way. But, if application like this is run as standalone app, it will show message box.

如何操作这种类似Apache的方式来实现?我相信,解决方法很简单,但我真的没有一个知道从哪里开始。

How can this Apache-like mode of operation be achieved? I believe solution is simple, but I don't really have an idea where to start.

件。 ?它可以被修改,以允许单机使用

Piece of code that follows is used to invoke service. Can it be modified to allow standalone usage?

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new Service() // defined elsewhere as Service : ServiceBase
        };
        ServiceBase.Run(ServicesToRun);
    }
}



我选择的语言是C#。

My language of choice is C#.

编辑:目前,我已经抽象通用代码到单独的组件(姑且称之为Library.dll),我有两个可执行文件:Console.exe和服务.EXE,它们是独立的和窗口服务应用程序,分别与双方都只是手段,调用Library.dll。

Currently, I have abstracted common code into separate assembly (let's call it Library.dll), and I have two executables: Console.exe and Service.exe, which are standalone and windows service applications, respectively, and both are just means to invoking Library.dll.

我的目标是这两个可执行文件合并成一个,这仍然会打电话给Library.dll。

My goal is to merge those two executables into one, that will still call to Library.dll.

推荐答案

一些挖后,我终于在.NET下引擎盖看了(System.ServiceProcess .ServiceBase.Run法),才发现它检查 Environment.UserInteractive 布尔确保可执行文件未交互运行。

After some digging, I have finally looked under .NET hood (System.ServiceProcess.ServiceBase.Run method), only to find that it checks Environment.UserInteractive bool to make sure that executable is NOT run interactively.

过于简单的解决方案,为我的作品:

Oversimplified solution that works for me:

class Program
{
    static void Main(string[] args)
    {
        if (!Environment.UserInteractive)
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                // Service.OnStart() creates instance of MainLib() 
                // and then calls its MainLib.Start() method
                new Service()
            };
            ServiceBase.Run(ServicesToRun);
            return;
        }

        // Run in a console window
        MainLib lib = new MainLib();
        lib.Start();
        // ...
    }
}

这篇关于如何使Windows服务应用程序,以便它可以作为一个独立的程序,以及运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆