2015-04-24 5 views
1

J'ai fait un webservice topshelf qui utilise le paramètre personnalisé:Topshelf - fils à partir en fonction des paramètres personnalisés

 string department = null; 



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

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

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


      //****************Change those names for other services*******************************************// 
      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 écrit simplement un fichier texte. Aa vous pouvez voir à partir du code ci-dessus le paramètre department est passé à MyService(string d). Tout fonctionne correctement lorsque je débogue en utilisant le "-partment: workshop" comme paramètre de débogage. Mais quand j'essaye d'installer le programme en tant que service utilisant callforward.exe install -department:logistics je crée le service callforwardservice_logistics bu quand je vérifie le journal le paramètre n'a pas été passé au MyService.

Qu'est-ce que je fais mal?

Répondre

2

Il semble que par défaut Topshelf ne supporte pas l'ajout de paramètres personnalisés à la configuration de démarrage du service et après l'installation, la valeur ImagePath sous HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\MyService ne contient pas de paramètre supplémentaire -department:.... Vous pouvez hériter de la valeur par défaut WindowsHostEnvironment et surcharger la méthode Install, mais je pense qu'il serait plus facile (peut-être moins agréable) pour ajouter simplement le code suivant à votre code de configuration hôte:

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

Ceci est une fonctionnalité très demandée si, et serait une demande de traction bienvenue. Je suis sûr que j'y arriverai finalement, c'est tout juste trop élevé sur mon radar à ce stade. –