English 中文(简体)
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. 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. Both Service1 and Service2 execute by themselves. 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.

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...

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..

<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.

Hope that helps!





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签