Click here to Skip to main content
15,886,037 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there
I would like to load services dynamically out of a main service.
When looking at the exe in processExplorer I would like to see something like this:
- MainService.exe
----- Dispatcher.exe
----- TCPServer.exe
----- Calculator.exe

I was thinking about creating a service in VS. Then add a service interface, that all other subservices must implement in another project (to specify behavior).
Any SubService that will be loaded by the main service implements that interface.
Finally, the main service checks a config file, giving asseblyName and namespace of the subservices to load.

Some pseudo code, first the main service initializes all subservices (subsystems) and start the services

C#
public partial class MainService : ServiceBase
{
    List<INotifier> _Subsystems = null;
    public RemoCo()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
           foreach(SrvConfig a in ServiceConfig)
           {
                INotifier asm = Assembly.Load("mySubSystem");
                asm.OutgoingResponseSignaled += MessageSentSignal;
                asm.Start();
                _Subsystems.Add(asm);
           }
    }

    private void MessageSentSignal(object sender, INotifierEventArgs e)
    {
        ...
    }

    protected override void OnStop()
    {
        ...
    }
}


The interface that each subservice must implement would look something like
C#
public interface INotifier
{
    string Name { get; set; }
    void Start();
    void Stop(TimeSpan pTimeout);
    void RequestStopAsync(TimeSpan pTimeToWait);
    void IncomingTask(ITask pTask);
    void IncomingTaskAsync(ITask pTask);
    void OutgoingResponseAsync(IResult pResult);
    void ReadConfig(string pConfigFilePath);
    void HealthPing();

    event ServiceStartedEventHandler ServiceStarted;
    event ServiceStoppedEventHandler ServiceStopped;
    event ServiceStopRequestedEventHandler ServiceStopRequested;
    event IncomingTaskSignaledEventHandler IncomingTaskSignaled;
    event OutgoingResponseSignaledEventHandler OutgoingResponseSignaledSignaled;
}



After all sub-services are started I would like to do things like this:
C#
public void SendTCPPacket(string pMessage)
{
    _Subsystems.First(x => x.Name.Equals("TCPServer", System.StringComparison.InvariantCultureIgnoreCase)).IncomingTask(pSendMessageTask);
}


It needs to be an exe so the hierarchy is properly displayed, Sub-services exe underneath the mainservice. Is such a thing possible? Could you help me with a small example if possible?

Thanks very much for your help

What I have tried:

tried sample code as above but failed to load the exe or to execute methods and bind to events
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900