开发者

How to install Windows service without extending Installer?

I am building a reusable framework for building scheduling serv开发者_开发百科ices using the .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. 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? Can I access the managed installation framework in more direct way?


I use SC.exe to register the service binary.

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 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:

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().


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. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜