Topshelf - starting threads based on custom parameters

允我心安 提交于 2019-12-08 02:48:11

问题


I've made a topshelf webservice that uses custom parameter:

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");      
            }
        }
    }
}

Where Helpers.logfile simply writes to text file. Aa you can see from the code above the parameter department is passed to the MyService(string d). It all works fine when I'm debugging using i.e. the "-department:workshop" as debugging parameter. But when I'm trying to install the program as a service using callforward.exe install -department:logistics I do create service callforwardservice_logistics bu when I check the log the parameter hasn't been passed to the MyService.

What am I doing wrong?


回答1:


It seems that by default Topshelf does not support adding custom parameters to the service start configuration and after installation the ImagePath value under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService does not contain an additional parameter -department:.... You could inherit the default WindowsHostEnvironment and overload the Install method, but I think it would be easier (maybe less nicer) to just add the following code to your host configuration code:

// *********************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));
        }
    });
    ...
}


来源:https://stackoverflow.com/questions/29837596/topshelf-starting-threads-based-on-custom-parameters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!