2009-11-06 8 views
0

동일한 프로세스에서 여러 서비스를 시작하는 단일 Windows 서비스를 구현하려고합니다. 코드에 따르면 다음과 같은 것을 보았습니다.동일한 프로세스에서 여러 Windows 서비스가 시작되지 않음

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

그러나이 코드는 Service1 만 실행하고 Service2는 실행하지 않습니다. Service1과 Service2는 모두 자체적으로 실행됩니다. 어떤 아이디어?

+0

두 개의 별도의 물리적 Windows 서비스 또는 두 가지 기능을 수행하는 Windows 서비스를 찾고 있습니까? – zac

+0

귀하의 Service1/Service2는 어떻게 생겼습니까? 그들은 OnStart 메소드와 ServiceName을 모두 가지고 있습니까? –

+0

@ 앤더스 예. 사실 둘 다 시작하고 개별적으로 잘 실행됩니다. –

답변

3

기본 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() { ... } 
} 

그러면 당신은 단순히, 실제 서비스 코드에서 마지막으로 당신이 당신의 하위 서비스를 위해 필요하지만 많은 threadedservice를 기반으로 클래스를 생성하고, 같은의 app.config에 ... 무언가를 그들 모두를 던져 ..

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

및 ServiceConfiguration.Current.Start()를 시작하고 Exit의 Service.Configuration.Current.Stop()을 수행하면됩니다.

희망 하시겠습니까?

+0

이것은 흥미로운 아이디어입니다. 이렇게하면 새 라이브러리를 독립적으로 컴파일하고 app.config에 추가하여 서비스를 다시 컴파일하지 않고도 서비스에 추가 할 수 있습니까? 이것은 매우 확장 가능한 해결책이 될 것입니다! –

+0

그래, 그게이 솔루션의 의도 였어. 우리는 지속적으로 미니 서비스를 추가하고 있었고 Windows 서비스 핸들러를 건드리지 않아도되었습니다. app config에 새 줄을 놓으십시오. – Sean

관련 문제