简体   繁体   English

如何在不扩展安装程序的情况下安装Windows服务?

[英]How to install Windows service without extending Installer?

I am building a reusable framework for building scheduling services using the .NET Framework. 我正在建立一个可重用的框架,以使用.NET Framework 构建调度服务 My code is distributed as a library, which is then referenced by the user in a console application. 我的代码以库的形式分发,然后由用户在控制台应用程序中引用。 In the main entry point of the application, the user is asked to transfer control to a static method in the library, which will commence job scheduling if running in non-interactive mode, alternatively read command arguments and perform maintenance tasks such as installation. 在应用程序的主要入口点,要求用户将控制权转移到库中的静态方法,如果以非交互方式运行,它将开始作业调度,或者读取命令参数并执行维护任务(如安装)。

In order to install the application as a Windows service using the System.Configuration.Install namespace, the user will have to add a class descendant of Installer to the application assembly, telling the framework how to install the service. 为了使用System.Configuration.Install命名空间将应用程序安装为Windows服务,用户必须向应用程序程序集添加Installer的类后代,以告知框架如何安装服务。 I really would prefer to do this once and for all in the library, but unfortunately the installer class has to be in the application assembly. 我真的宁愿做这个一劳永逸库,但不幸的是安装程序类必须在应用程序组件。

The solution that I am looking at right now, involves defining the an installer class in the library, and requiring the user to inherit this in a public class, without adding any additional code. 我现在正在寻找的解决方案包括在库中定义一个安装程序类,并要求用户在公共类中继承此安装程序,而无需添加任何其他代码。 While this works, it requires the user to write annoying boiler plate code. 在这种情况下,需要用户编写烦人的样板代码。

What are the alternatives to extending Installer ? 扩展Installer的替代方法是什么? Can I access the managed installation framework in more direct way? 我可以以更直接的方式访问托管安装框架吗?

I use SC.exe to register the service binary. 我使用SC.exe注册服务二进制文件。

Doing so leaves only this stub in the service installer class: 这样做仅将这个存根保留在服务安装程序类中:

[RunInstaller(true)]
public partial class SoapSenderInstaller : Installer
{
    public SoapSenderInstaller()
    {
        InitializeComponent();
    }
}

Calling SC.exe is a simple as this: 调用SC.exe很简单:

sc create MyService binpath= "C:\Path\to\my\MyService.exe" DisplayName= "My Service Display Name" depend= MSMQ start= auto

The only pitfall is the way sc.exe expects its commmand line arguments: 唯一的陷阱是sc.exe期望其命令行参数的方式:

binpath=[BLANK]"MyService.exe"

EDIT 编辑

Of course this solution falls short to the requirement that user should not have to create any code that makes his binary a windows service because if he just references your library he would still need to inherit from ServiceBase and implement OnStart() and OnStop(). 当然,此解决方案无法满足用户不必创建任何使其二进制文件成为Windows服务的代码的要求,因为如果他只是引用您的库,则仍需要从ServiceBase继承并实现OnStart()和OnStop()。

I know that you can add some custom fields to installer and require user's input during installation if you compile it using Installer.CodeEffects.com. 我知道您可以向安装程序添加一些自定义字段,并且如果您使用Installer.CodeEffects.com进行编译,则在安装过程中需要用户输入。 May be, by requiring a namespace or type name as a string input from the end user, you could load or invoke things that you need on the fly at run time? 可能是,通过要求名称空间或类型名称作为最终用户的字符串输入,您可以在运行时动态加载或调用所需的内容?

I can't think of anything else in your situation. 在您的情况下,我想不起其他任何事情。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM