English 中文(简体)
如何使窗口服务应用成为一种单独的方案?
原标题:How to make windows service application so it can run as a standalone program as well?

我首先举一个例子: 阿帕奇网络服务器(视窗)有一个冰盖特征:它既可以作为独立应用(既有用户特权),也可以作为窗口直接安装和运行(作为地方系统账户),同时使用同样的可执行手段。

申请要作为单独申请,它所需要的一切都符合某些公共部门的静态主(主)的要求。

为使申请能够安装和运行,必须采用某种方式实施“服务基础”和“安装”。 但是,如果像这种申请一样作为单独申请,它将显示电文箱。

如何实现这种阿帕奇类行动模式? 我认为,解决办法是简单的,但我确实没有想法可以开始。

以下守则用于援引服务。 是否可以修改该术语,允许单独使用?

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#。

<><>Edit>: 目前,我已将共同守则单独编成大会(请其图书馆查阅),我有两个可执行之处:Console.exe 和 Service.exe, 这两者分别是独立和窗口服务应用程序,两者都只是援引图书馆。

我的目标是将这些两名可起诉者合并成一个,这仍需要图书馆。

最佳回答

经过一些挖掘,我终于看了。 NET hood (System.serviceProcess). ServiceBase.Run方法,仅发现它检查 环境。 用户互动 bool,以确保可执行的是NOT。

为我工作的简化解决办法:

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();
        // ...
    }
}
问题回答

在C#中,需要一个指挥线理由作为服务加以管理,这是容易做到的。 如果该论点没有,那么就掌握了你的形式/内容。 然后,你安装了安装器,就在安装服务时,就把这一论点列入可执行的道路上,这样它就认为:

C:MyAppMyApp.exe -service

它想看一下:

static void Main(string[] args)
{
    foreach (string arg in args)
    {
        //Run as a service if our argument is there
        if (arg.ToLower() == "-service")
        {
            ServiceBase[] servicesToRun = new ServiceBase[] { new Service1() };
            ServiceBase.Run(servicesToRun);
            return;
        }
    }

    //Run the main form if the argument isn t present, like when a user opens the app from Explorer.
    Application.Run(new Form1());
}

这只是给你一个想法的一个例子,也许有更清洁的方式来撰写这一法典。

确实,你的所有功能都应从图书馆中抽取。 采用视窗服务的做法不应有。 事实上,如果有一个称为“服务公司”的班级,“服务公司”和“服务网”——“Windows Service”应用程序可以要求,这样,可以指一条指挥线、窗户或任何东西。

你在这里所描述的只是需要更加抽象。 “服务”的功能不一定需要紧密结合视窗服务如何运作。 希望

举例来说,我很相信阿帕奇特人在C或C++书写。 为此,你需要一个服务行业。 如果你像一个正常方案一样执行,则主人会叫。 如果你在该处看到服务控制主管,则Main服务公司被叫去。

关于C#,我要说的是我知道这一点。 如果我不得不在剪辑中写一份服务,我会在这里开始——





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...