【问题标题】:Is there any way to specify a service name in the app.config file?有没有办法在 app.config 文件中指定服务名称?
【发布时间】:2010-11-09 07:59:40
【问题描述】:

我想在 app.config 中指定我的服务名称,而无需重复重新编译和安装/卸载我的服务。 但只是从 app.config 中检索服务名称,服务似乎忽略了它。有什么特殊的技巧如何获得这个吗? 提前致谢。

我的意思是经典的 Windows 服务。我认为这里不需要任何代码。我只想从 app.config 动态检索服务名称。

【问题讨论】:

  • 您必须向我们展示一些代码,我们才能提供帮助
  • 您是指windows服务、wcf服务还是经典的web服务?

标签: windows-services installation app-config


【解决方案1】:

在网上搜索了一段时间并阅读了文章后,我更清楚了,不能以如此动态的方式在 app.config 中指定服务名称,而是可以使用 sc 命令执行类似的解决方案。可以在app.config中指定其他配置变量,使用sc重命名

sc.exe create "servicename" binPath="myservicepath.exe"

【讨论】:

    【解决方案2】:

    我不确定您的想法是什么。您希望更改 Windows 服务的名称。很公平。什么时候改变?

    想象一下,您已经找到了解决方案并创建了这样的 Windows 服务。我认为在您的场景中,您至少会在第一次安装它。然后你不想卸载/安装它。但大概你想开始/停止并用它做其他事情。其中一项操作会导致服务名称更改吗?

    如果是这样,我想您可以启动一个进程,根据某种命名逻辑以透明的方式为您卸载和安装它。

    我看不出你还能怎么做。

    或者只是想出一个真正通用的名称来涵盖所有可能性(这可能非常简单或非常困难)。

    【讨论】:

      【解决方案3】:

      http://www.codeproject.com/Articles/21320/Multiple-Instance-NET-Windows-Service

      <add key="ServiceName" value="I"/>
      
       [RunInstaller(true)]
          public class ServiceInstaller1 : Installer
          {
              internal static string ServiceNameDefault = "My Service";
      
              internal static string ServiceName = GetConfigurationValue("ServiceName");
      
              /// <summary>
              /// Public Constructor for WindowsServiceInstaller.
              /// - Put all of your Initialization code here.
              /// </summary>
              public ServiceInstaller1()
              {
                  var serviceProcessInstaller =   new ServiceProcessInstaller();
                  var serviceInstaller = new ServiceInstaller();
      
                  //# Service Account Information
                  serviceProcessInstaller.Account = ServiceAccount.LocalSystem;
                  //serviceProcessInstaller.Username = null;
                  //serviceProcessInstaller.Password = null;
      
                  //# Service Information
                  serviceInstaller.DisplayName = ServiceName;
                  serviceInstaller.StartType = ServiceStartMode.Manual;
      
                  //# This must be identical to the WindowsService.ServiceBase name
                  //# set in the constructor of WindowsService.cs
                  serviceInstaller.ServiceName = ServiceName;
      
                  Installers.Add(serviceProcessInstaller);
                  Installers.Add(serviceInstaller);
              }
      
              private static string GetConfigurationValue(string key)
              {
                  Assembly service = Assembly.GetAssembly(typeof(Service));
      
                  Configuration config = ConfigurationManager.OpenExeConfiguration(service.Location);
      
                  if (config.AppSettings.Settings[key] != null)
                      return ServiceNameDefault + " " + config.AppSettings.Settings[key].Value;
                  else
                      return ServiceNameDefault;
              }
          }
      

      【讨论】:

      • 工作愉快。两个安装程序有问题(忘记在设计中删除安装程序),但一旦解决所有工作正常。我需要在配置中设置它,因为我必须为我们拥有的每个客户端编译此安装程序的不同实例。非常感谢,为我节省了一些时间!
      【解决方案4】:

      假设您指的是 Windows 服务,答案是否定的。该服务必须安装在注册表中,并且名称是注册表项之一。

      【讨论】:

        【解决方案5】:

        恐怕您尝试做的事情是不可能的。它实际上似乎违背了 Windows 服务目的和当前行为的本质。

        安装 Windows 服务后,如果不重新安装,则无法更改名称。实际命名该服务的是一个名为服务安装程序的元素。到目前为止,我假设您知道它是什么以及它的位置。

        但是,有一些方法可以使用Windows Management Instrumentation (WMI) 来操作已安装的服务。也许这与 Izabela 的建议相结合成为您解决方案的正确途径。

        我建议您阅读以下教程,您可能会找到另一种方法来实现您想要做的事情。

        http://www.serverwatch.com/tutorials/article.php/1576131/Windows-Services-Management-With-WMI-Part-1.htm

        【讨论】:

          猜你喜欢
          • 2013-01-08
          • 1970-01-01
          • 1970-01-01
          • 2023-04-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-09-21
          相关资源
          最近更新 更多