Blogger :
Thinktecture Blog
All posts :
All posts by Thinktecture Blog
Category :
WSCF/WCF
Blogged date : 2006 Sep 05
As WCF has reached RC1 stage, I find myself cleaning up a few bits of older WCF code. While playing around with it, I always found myself having to start more and more ServiceHosts for different configurations. The following snippet iterates over all <service> entries in configuration/system.serviceModel and opens a ServiceHost for each of them.
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Configuration;
using System.ServiceModel.Configuration;
using System.ServiceModel;
public class ServiceHostGroup
{
static List<ServiceHost> _hosts = new List<ServiceHost>();
private static void OpenHost(Type t)
{
ServiceHost hst = new ServiceHost(t);
hst.Open();
_hosts.Add(hst);
}
public static void StartAllConfiguredServices()
{
Configuration conf =
ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
ServiceModelSectionGroup svcmod =
(ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel");
foreach (ServiceElement el in svcmod.Services.Services)
{
Type svcType = Type.GetType(el.Name);
if (svcType == null)
throw new Exception("Invalid Service Type " + el.Name + " in configuration file.");
OpenHost(svcType);
}
}
}