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

C# Topshelf-基于自定义参数启动线程,c#,topshelf,C#,Topshelf,我制作了一个使用自定义参数的topshelf Web服务: string department = null; // *********************Below is a TopShelf HostFactory.Run(hostConfigurator => { //Define new parameter hostConfigurator.ApplyCommandLine();

我制作了一个使用自定义参数的topshelf Web服务:

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");      
            }
        }
    }
}
字符串部门=null;
//****************************下面是一个TopShelf
运行(hostConfigurator=>
{
//定义新参数
hostConfigurator.ApplyCommandLine();
//应用它
hostConfigurator.AddCommandLineDefinition(“department”,f=>{department=f;});
日志文件(“xxx”,“Got department:”+department);
hostConfigurator.Service(serviceConfigurator=>
{
//我们正在使用什么服务
serviceConfigurator.ConstructUsing(()=>newmyservice(department));
//开始时运行什么
开始时(myService=>myService.Start());
//停下来
serviceConfigurator.WhenStopped(myService=>myService.Stop());
}
hostConfigurator.RunAsLocalService();
//****************将这些名称更改为其他名称
字符串d=“CallForwardService”+部门;
hostConfigurator.SetDisplayName(d);
hostConfigurator.SetDescription(“使用Topshelf的调用”);
hostConfigurator.SetServiceName(d);
});
公共类MyService
{
字符串删除任务;
公共MyService(字符串d)
{
//***********************针对三个不同目的地的三项任务
depTask=d;
_taskL=新任务(物流);
_taskP=新任务(计划员);
_taskW=新任务(车间);
LogFile(depTask,“开始为“+d”处理线程);
公开作废开始()
{
如果(depTask==“物流”)
{
_taskL.Start();
日志文件(depTask,“选择了正确的线程”);
}
}
}
}
其中
Helpers.logfile
只是写入文本文件。Aa您可以从参数
department
上方的代码中看到,它被传递到
MyService(字符串d)
。 当我使用“-department:workshop”作为调试参数进行调试时,一切正常。但是当我尝试使用
callforward.exe安装-部门:物流
我确实创建了服务
callforwardservice\u物流
bu当我检查日志时,参数还没有传递给MyService


我做错了什么?

默认情况下,Topshelf不支持向服务启动配置添加自定义参数,并且在安装后,
HKEY\U LOCAL\U MACHINE\SYSTEM\CurrentControlSet\Services\MyService
下的
ImagePath
值不包含其他参数
-部门:…
>。您可以继承默认的
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));
        }
    });
    ...
}

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

所以在我的情况下

callforward.exe install -department"logistics"
我用过

callforward.exe install -department"logistics" -instance:logistics
然后按实例名称启动实例:

net start CallForwardService_$logistics
这使我能够使用参数控制的不同名称创建同一服务的多个实例:

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

这是一项要求很高的功能,也是一个受欢迎的拉动请求。我相信我最终会实现它,目前它在我的雷达上还不算高。