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?