2013-03-06 10 views
8

일부 명령 줄 인수를 사용하여 일부 구성을 Windows 서비스에 전달합니다 (명령 줄이 다른 인스턴스는 거의 없습니다).TopShelf에서 서비스의 명령 줄 옵션을 지정하는 방법

내 코드는 다음과 같습니다 만에 상을 설치

myhost.exe install -sqlserver:someinstance 

불행하게도, sqlserver 명령 줄 옵션을 사용할 수 있으며 이동하지 않는 : 나는 서비스를 설치하면

HostFactory.Run(x =>         
{ 
    x.Service<MyHost>(s => 
    {      
     s.ConstructUsing(name => new MyHost()); 
     s.WhenStarted(tc => tc.Start());    
     s.WhenStopped(tc => tc.Stop());    
    }); 
    x.AddCommandLineDefinition("sqlserver", v => sqlInstance = v); 
}); 

내가 사용 서비스의 매개 변수. 따라서 서비스를 실행할 때 필요한 매개 변수 값을 얻지 못합니다.

TopShelf에서 시작한 서비스의 명령 줄을 수정할 수있는 방법이 있습니까?

답변

2

에이 스레드를 검토 할 것입니다. 나는이 매개 변수를 exe 옆의 구성에 저장하여이 문제를 해결합니다. 따라서 사용자는이 작업을 수행 할 수 있습니다

:

그런 다음, (하나의 ConfigurationManager를 위해 System.IO.File을 사용하거나 사용)

service.exe run /sqlserver:connectionstring 

를 앱은 파일에 ConnectionString을 저장 사용자는이 작업을 수행하는 경우

service.exe run 

또는 서비스가 Windows 서비스로 실행되는 경우 앱은 config에서로드 만합니다.

3

필자는 유사한 요구 사항을 가지고 있으며 불행히도 파일에 명령 줄 매개 변수를 저장하는 것은 옵션이 아닙니다.

면책 조항 :이 방법은 윈도우

에만 유효 우선 내가 명령 줄을 포함하는 서비스에 대한 의 ImagePath Windows 레지스트리 항목을 업데이트 AddCommanLineParameterToStartupOptions에서, After Install Action

x.AfterInstall(
    installSettings => 
    { 
     AddCommandLineParametersToStartupOptions(installSettings); 
    }); 

추가 매개 변수.

TopShelf는 servicenameinstance 중복을 피하기 위해이 단계 이후에 매개 변수를 추가합니다. 제 경우보다 더 많은 것을 걸러 내고 싶을 수도 있습니다 만, 이것으로 충분합니다.

 private static void AddCommandLineParametersToStartupOptions(InstallHostSettings installSettings) 
    { 
      var serviceKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
       $"SYSTEM\\CurrentControlSet\\Services\\{installSettings.ServiceName}", 
       true); 

      if (serviceKey == null) 
      { 
       throw new Exception($"Could not locate Registry Key for service '{installSettings.ServiceName}'"); 
      } 

      var arguments = Environment.GetCommandLineArgs(); 

      string programName = null; 
      StringBuilder argumentsList = new StringBuilder(); 

      for (int i = 0; i < arguments.Length; i++) 
      { 
       if (i == 0) 
       { 
        // program name is the first argument 
        programName = arguments[i]; 
       } 
       else 
       { 
        // Remove these servicename and instance arguments as TopShelf adds them as well 
        // Remove install switch 
        if (arguments[i].StartsWith("-servicename", StringComparison.InvariantCultureIgnoreCase) | 
         arguments[i].StartsWith("-instance", StringComparison.InvariantCultureIgnoreCase) | 
         arguments[i].StartsWith("install", StringComparison.InvariantCultureIgnoreCase)) 
        { 
         continue; 
        } 
        argumentsList.Append(" "); 
        argumentsList.Append(arguments[i]); 
       } 
      } 

      // Apply the arguments to the ImagePath value under the service Registry key 
      var imageName = $"\"{Environment.CurrentDirectory}\\{programName}\" {argumentsList.ToString()}"; 
      serviceKey.SetValue("ImagePath", imageName, Microsoft.Win32.RegistryValueKind.String); 
} 
관련 문제