C# 同一进程下的多个Windows服务未启动

C# 同一进程下的多个Windows服务未启动,c#,.net,windows-services,C#,.net,Windows Services,我们正在尝试实现一个Windows服务,在同一进程下启动多个服务。根据代码,我看到您执行以下操作: static void Main() { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1(), new Service2() }; Servic

我们正在尝试实现一个Windows服务,在同一进程下启动多个服务。根据代码,我看到您执行以下操作:

    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new Service1(),
            new Service2()
        };
        ServiceBase.Run(ServicesToRun);
    }

但是,此代码仅执行Service1,而不执行Service2。Service1和Service2都是自己执行的。有什么想法吗?

我想您应该创建一个子服务模型,在这个模型中,可以从主windows服务启动任意数量的子服务

public interface ISubService
{
   void Initialize( XmlElement xmlSection );
   bool Start( );
   void RequestStop( );
   void Stop( TimeSpan timeout );
}
然后可能是一个基本的线程服务类

public abstract class ThreadedService : ISubService
{
     private Thread m_thread;

     private ThreadedService( )
     {
        m_thread = new Thread( new ThreadStart( StartThread ) );
     }

     // implement the interface
}
通过app.config和IConfigurationSectionHandler配置您的服务

public class ServiceConfigurationHandler : IConfigurationSectionHandler
{
   public ServiceConfigurationHandler() { }

   public object Create(object parent, object configContext, XmlNode section)
   {
       return new ServiceConfiguration((XmlElement)section);
   }
}
要处理配置部分的内容

public class ServiceConfiguration
{
   public static readonly ServiceConfiguration Current = (ServiceConfiguration)ConfigurationManager.GetSection("me/services");

   private List<ISubService> m_services;
   private string m_serviceName;

   internal ServiceConfiguration(XmlElement xmlSection)
   {
       // loop through the config and initialize the services
       // service = createinstance(type)..kind of deal
       // m_services.Add( service );
   }

   public void Start( )
   {
       foreach( ISubService service in m_services ) { service.Start( ); }           
   }
   public void Stop( ) { ... }
}
公共类服务配置
{
公共静态只读ServiceConfiguration当前=(ServiceConfiguration)ConfigurationManager.GetSection(“me/services”);
私人列表m_服务;
私有字符串m_serviceName;
内部服务配置(XmlElement xmlSection)
{
//循环配置并初始化服务
//服务=createinstance(类型)…交易类型
//m_services.Add(服务);
}
公共无效开始()
{
foreach(m_服务中的ISubService){service.Start();}
}
公共无效停止(){…}
}
然后,您只需创建子服务所需的基于threadedservice的多个类,并将它们全部放入app.config…类似

<me>
  <services>
     <service type="my.library.service1,my.library" />
     <service type="my.library.service2,my.library" />
  </services>
</me>

最后,在实际的服务代码中,只需在开始时执行ServiceConfiguration.Current.Start(),在退出时执行service.Configuration.Current.Stop()


希望有帮助

我认为您应该创建一个子服务模型,在该模型中,可以从主windows服务启动任意数量的子服务

public interface ISubService
{
   void Initialize( XmlElement xmlSection );
   bool Start( );
   void RequestStop( );
   void Stop( TimeSpan timeout );
}
然后可能是一个基本的线程服务类

public abstract class ThreadedService : ISubService
{
     private Thread m_thread;

     private ThreadedService( )
     {
        m_thread = new Thread( new ThreadStart( StartThread ) );
     }

     // implement the interface
}
通过app.config和IConfigurationSectionHandler配置您的服务

public class ServiceConfigurationHandler : IConfigurationSectionHandler
{
   public ServiceConfigurationHandler() { }

   public object Create(object parent, object configContext, XmlNode section)
   {
       return new ServiceConfiguration((XmlElement)section);
   }
}
要处理配置部分的内容

public class ServiceConfiguration
{
   public static readonly ServiceConfiguration Current = (ServiceConfiguration)ConfigurationManager.GetSection("me/services");

   private List<ISubService> m_services;
   private string m_serviceName;

   internal ServiceConfiguration(XmlElement xmlSection)
   {
       // loop through the config and initialize the services
       // service = createinstance(type)..kind of deal
       // m_services.Add( service );
   }

   public void Start( )
   {
       foreach( ISubService service in m_services ) { service.Start( ); }           
   }
   public void Stop( ) { ... }
}
公共类服务配置
{
公共静态只读ServiceConfiguration当前=(ServiceConfiguration)ConfigurationManager.GetSection(“me/services”);
私人列表m_服务;
私有字符串m_serviceName;
内部服务配置(XmlElement xmlSection)
{
//循环配置并初始化服务
//服务=createinstance(类型)…交易类型
//m_services.Add(服务);
}
公共无效开始()
{
foreach(m_服务中的ISubService){service.Start();}
}
公共无效停止(){…}
}
然后,您只需创建子服务所需的基于threadedservice的多个类,并将它们全部放入app.config…类似

<me>
  <services>
     <service type="my.library.service1,my.library" />
     <service type="my.library.service2,my.library" />
  </services>
</me>

最后,在实际的服务代码中,只需在开始时执行ServiceConfiguration.Current.Start(),在退出时执行service.Configuration.Current.Stop()


希望有帮助

您正在寻找两个独立的物理Windows服务还是一个执行两种功能的Windows服务?您的Service1/Service2看起来如何?他们有两种OnStart方法和不同的ServiceName?@Anders Yep。事实上,两者都可以单独启动和运行。我在这个问题上发现了类似的线程。因此,我们最终解决了我们的问题,取而代之的是使用一个托管了多个服务的项目,每个服务安装程序分别创建了三个不同的Windows服务。无论如何,这是一个更好的解决方案,因为我们可以在出现某种故障时独立启动和停止各个服务。您是在寻找两个独立的物理Windows服务还是一个执行两种功能的Windows服务?您的Service1/Service2看起来如何?他们有两种OnStart方法和不同的ServiceName?@Anders Yep。事实上,两者都可以单独启动和运行。我在这个问题上发现了类似的线程。因此,我们最终解决了我们的问题,取而代之的是使用一个托管了多个服务的项目,每个服务安装程序分别创建了三个不同的Windows服务。无论如何,这是一个更好的解决方案,因为我们可以在出现某种故障时独立启动和停止各个服务。这是一个有趣的想法。这是否允许我们独立编译新库并将它们添加到服务中,而无需通过将它们添加到app.config重新编译服务?这将是一个非常可扩展的解决方案!是的,这就是这个解决方案的初衷。我们一直在添加迷你服务,不想接触windows服务处理程序。在应用程序配置中添加一行新行,您的应用程序就很好了。这是一个有趣的想法。这是否允许我们独立编译新库并将它们添加到服务中,而无需通过将它们添加到app.config重新编译服务?这将是一个非常可扩展的解决方案!是的,这就是这个解决方案的初衷。我们一直在添加迷你服务,不想接触windows服务处理程序。在应用程序配置中添加一行新行,您的应用程序就正常了。