Topshelf - 基于自定义参数启动线程

时间:2015-04-24 02:16:19

标签: c# topshelf

我已经制作了一个使用自定义参数的topshelf webservice:

string department = null;

// *********************Below is a TopShelf
HostFactory.Run(hostConfigurator =>
{
    //Define new parameter
    hostConfigurator.ApplyCommandLine();                                                
    //apply it  
    hostConfigurator.AddCommandLineDefinition("department", f => { department = f; });

    Helpers.LogFile("xxx", "Got department:"+department);  

    hostConfigurator.Service<MyService>(serviceConfigurator =>
    {
        //what service we are using
        serviceConfigurator.ConstructUsing(() => new MyService(department));
        //what to run on start
        serviceConfigurator.WhenStarted(myService => myService.Start());
        // and on stop             
        serviceConfigurator.WhenStopped(myService => myService.Stop());                 
    }

    hostConfigurator.RunAsLocalService();

    //****************Change those names for other
    string d = "CallForwardService_" + department;

    hostConfigurator.SetDisplayName(d);
    hostConfigurator.SetDescription("CallForward using Topshelf");
    hostConfigurator.SetServiceName(d);                
});



public class MyService
{
    string depTask;
    public MyService(string d)
    {
        //***********************Three tasks for three different destinations
        depTask = d;
        _taskL = new Task(Logistics);
        _taskP = new Task(Planners);
        _taskW = new Task(Workshop);
        Helpers.LogFile(depTask, "started working on threads for "+d);   

        public void Start()
        {
            if (depTask == "logistics")
            {
                _taskL.Start();
                Helpers.LogFile(depTask, "proper thread selected");      
            }
        }
    }
}

Helpers.logfile只是写入文本文件。您可以从上面的代码中看到参数department传递给MyService(string d)。 当我使用&#34; -department:workshop&#34;进行调试时,一切正常。作为调试参数。但是当我试图将程序作为服务安装时 callforward.exe install -department:logistics当我检查日志时,我确实创建了服务callforwardservice_logistics,但参数还没有传递给MyService。

我做错了什么?

2 个答案:

答案 0 :(得分:2)

默认情况下,Topshelf似乎不支持向服务启动配置添加自定义参数,安装后ImagePath下的HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService值不包含其他参数-department:...。您可以继承默认的WindowsHostEnvironment并重载Install方法,但我认为将以下代码添加到主机配置代码会更容易(可能更不好):

// *********************Below is a TopShelf code*****************************//
HostFactory.Run(hostConfigurator =>
{
    ...
    hc.AfterInstall(ihc =>
    {
        using (RegistryKey system = Registry.LocalMachine.OpenSubKey("System"))
        using (RegistryKey currentControlSet = system.OpenSubKey("CurrentControlSet"))
        using (RegistryKey services = currentControlSet.OpenSubKey("Services"))
        using (RegistryKey service = services.OpenSubKey(ihc.ServiceName, true))
        {
            const String v = "ImagePath";
            var imagePath = (String)service.GetValue(v);
            service.SetValue(v, imagePath + String.Format(" -department \"{0}\"", department));
        }
    });
    ...
}

答案 1 :(得分:0)

我最终解决了这个问题:设置参数还不够,您还必须创建一个命名实例。

所以我的情况是

callforward.exe install -department"logistics"

我用过

callforward.exe install -department"logistics" -instance:logistics

然后通过实例名称启动实例:

net start CallForwardService_$logistics

这使我可以通过参数控制使用不同名称创建同一服务的多个实例:

enter image description here