I've been tasked with moving over dozens of WCF Services to a single Windows Service. I've created a Windows Service using the Windows Service template and added the the following code to ServiceHostController:
public partial class ServiceHostController : ServiceBase
{
private List<ServiceHost> serviceHosts;
public ServiceHostController()
{
InitializeComponent();
this.ServiceName = "WCFServices";
this.CanStop = true;
this.AutoLog = true;
}
protected override void OnStart(string[] args)
{
if (serviceHosts != null)
{
foreach (var service in serviceHosts)
{
service.Close();
}
}
InitializeServices();
foreach (var service in serviceHosts)
{
service.Open();
}
}
protected override void OnStop()
{
if (serviceHosts != null)
{
foreach (var service in serviceHosts)
{
service.Close();
}
serviceHosts.Close(); = null;
}
foreach (var service in serviceHosts)
{
service.Close();
}
}
private void InitializeServices()
{
serviceHosts = new List<ServiceHost>()
{
new ServiceHost(typeof(WCFService1)),
new ServiceHost(typeof(WCFService2)),
// add dozens of services here
};
}
}
Besides not following the Do not repeat yourself rule here (actual code is different), is this how I should be hosting these WCF services in the Windows Service code?