[英]Multiple Windows Services under the same process not starting
We are trying to implement a single Windows Services that starts multiple services under the same process. 我们正在尝试实现一个Windows服务,该服务可以在同一过程中启动多个服务。 According to code I've seen you do the following:
根据代码,我已经看到您执行以下操作:
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1(),
new Service2()
};
ServiceBase.Run(ServicesToRun);
}
However, this code only executes Service1 and not Service2. 但是,此代码仅执行Service1,而不执行Service2。 Both Service1 and Service2 execute by themselves.
Service1和Service2均自行执行。 Any ideas?
有任何想法吗?
I would think you would want to create a sub service model where one could start any number of sub services from the main windows service. 我想您会想要创建一种子服务模型,在该模型中,可以从主Windows服务启动任何数量的子服务。
public interface ISubService
{
void Initialize( XmlElement xmlSection );
bool Start( );
void RequestStop( );
void Stop( TimeSpan timeout );
}
Then maybe a base Threaded Service class.. 然后可能是基本的线程服务类。
public abstract class ThreadedService : ISubService
{
private Thread m_thread;
private ThreadedService( )
{
m_thread = new Thread( new ThreadStart( StartThread ) );
}
// implement the interface
}
Configure your services through the app.config and an IConfigurationSectionHandler... 通过app.config和IConfigurationSectionHandler配置服务...
public class ServiceConfigurationHandler : IConfigurationSectionHandler
{
public ServiceConfigurationHandler() { }
public object Create(object parent, object configContext, XmlNode section)
{
return new ServiceConfiguration((XmlElement)section);
}
}
Something to handle the config sections... 可以处理配置部分的内容...
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( ) { ... }
}
then you simply create however many threadedservice based classes you need for your sub services, and throw them all into the app.config...something like.. 然后,您只需创建子服务所需的许多基于线程服务的类,然后将它们全部放入app.config中即可。
<me>
<services>
<service type="my.library.service1,my.library" />
<service type="my.library.service2,my.library" />
</services>
</me>
and lastly, in your actual service code, just have to do ServiceConfiguration.Current.Start( ) on the start, and Service.Configuration.Current.Stop( ) in the exit. 最后,在您实际的服务代码中,只需在开始处执行ServiceConfiguration.Current.Start(),然后在出口处执行Service.Configuration.Current.Stop()。
Hope that helps! 希望有帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.