ServiceBase OnStart不从serviceController.Start接收args

时间:2015-10-22 18:16:17

标签: c# .net service

据我了解,serviceBase.Onstart(args)应该接收serviceController.start(args)中存在的参数。

这是我的服务控制器实现

    if (serviceController.Status == ServiceControllerStatus.Stopped)
            {
                try
                {
                    string[] args = {"execute-service"};
                    serviceController.Start(args);
                    ServiceManager.WaitForStatusChange(......);
                }
                catch (InvalidOperationException ex)
                {
                    EventLog.WriteEntry(.....);
                    return 1;
                }
            }

这是我的service.cs

     protected override void OnStart(string[] args)
    {
        this.OnStart(args);
        this.scheduler.StartScheduler();
    }

当我尝试启动我的服务时,参数" execute-service"没有传递给主program.cs。我有一个正在创建的logFile,可以看到args不存在。

正如我在线阅读的那样,寻找关于如何传递args的一些想法,我正在使用servicebase.onstart()正确地完成它。

关于如何调试或修复的想法?

1 个答案:

答案 0 :(得分:0)

我不认为此处显示的信息足以调试此问题。为了进行比较,这是我在一个旨在作为Windows服务运行的控制台应用程序中所做的:

public sealed partial class ServiceMain : ServiceBase
{
    // Service startup modes.
    private const string DEBUG = @"/d";         
    private const string INSTALL = @"/i";       
    private const string UNINSTALL = @"/u";     

然后将ServiceMain.Main()设置为启动方法:

// This is the entry point for this service. 
// This method runs on an SCM thread.
public static void Main(string[] args)
{
    if (Environment.UserInteractive && args.Length > 0)
    {
        switch (args[0])
        {
            // Debug the service as a normal app, presumably within Visual Studio.
            case DEBUG:
                ServiceMain DebugService = new ServiceMain();
                DebugService.OnStart(null);
                break;

调用ServiceMain.OnStart()方法:

// SCM requests service start using its own thread.
// This method must complete within 10 seconds of it
// starting. Otherwise the SCM diagnoses a hang.
protected override void OnStart(string[] args)
{
    this.AppLog = new Log();
    AppLog.Information("SCM requested service start.", "ServiceMain.OnStart");

您可以看到whole service in context here