.net 如何为TopShelf中的服务指定命令行选项
我使用命令行参数将一些配置传递给windows服务(只有少数实例具有不同的命令行) 我的代码如下所示:.net 如何为TopShelf中的服务指定命令行选项,.net,topshelf,.net,Topshelf,我使用命令行参数将一些配置传递给windows服务(只有少数实例具有不同的命令行) 我的代码如下所示: HostFactory.Run(x => { x.Service<MyHost>(s => { s.ConstructUsing(name => new MyHost()); s.WhenStarted(tc =
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);
});
不幸的是,sqlserver
命令行选项仅在安装阶段可用,并且不转到服务的参数。所以当我运行服务时,我并没有得到我需要的参数值
有什么方法可以修改由TopShelf启动的服务的命令行吗?此时您无法轻松地完成此操作。我会查看邮件列表上的这个帖子
您不能修改传递给服务的命令行。我通过将这些参数保存到exe旁边的配置来解决这个问题。因此,用户可以这样做:
service.exe run /sqlserver:connectionstring
应用程序将连接字符串保存到一个文件中(使用System.IO.file或使用ConfigurationManager)
然后,如果用户执行此操作:
service.exe run
或者,如果服务作为windows服务运行,那么应用程序只是从配置加载。我有类似的要求,不幸的是,将命令行参数存储在文件中不是一个选项 免责声明:此方法仅适用于Windows 首先,我添加了一个 在
AddCommanLineParameterToStartupOptions
I中,更新服务的ImagePathWindows注册表项,以包含命令行参数
TopShelf在此步骤之后添加了它的参数,以避免重复servicename
和instance
I过滤掉这些参数。你可能想过滤掉的不仅仅是那些,但在我的情况下,这就足够了
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);
}
private static void AddCommandLineParametersToStartupOptions(InstallHostSettings安装设置)
{
var serviceKey=Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
$“SYSTEM\\CurrentControlSet\\Services\\{installSettings.ServiceName}”,
正确的);
if(serviceKey==null)
{
引发新异常($”无法找到服务“{installSettings.ServiceName}”的注册表项);
}
var arguments=Environment.GetCommandLineArgs();
字符串programName=null;
StringBuilder argumentsList=新建StringBuilder();
for(int i=0;i
您可能不想将用户名/密码(用于在特殊帐户下运行服务)写入imagepath。以下是允许白名单参数的版本:
x.AfterInstall(
installSettings =>
{
AddCommandLineParametersToStartupOptions(installSettings);
});
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);
}