I'm not sure what you mean by pulling the associated addresses and contracts from config - this is done automatically. The service section in the configuration file is automatically connected to the type of service hosted by ServiceHost:
Service hosting:
using (var host = new ServiceHost(typeof(MyNamespace.Service)) { // no endpoint setting needed if configuration is correctly paired by the type name host.Open() }
Service Configuration:
<services> <service name="MyNamespace.Service"> ... </service> </service>
Now you only need to automatically process the creation of the ServiceHost. Here is my sample code:
class Program { static void Main(string[] args) { List<ServiceHost> hosts = new List<ServiceHost>(); try { var section = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection; if (section != null) { foreach (ServiceElement element in section.Services) { var serviceType = Type.GetType(element.Name); var host = new ServiceHost(serviceType); hosts.Add(host); host.Open(); } } Console.ReadLine(); } catch (Exception e) { Console.WriteLine(e.Message); Console.ReadLine(); } finally { foreach (ServiceHost host in hosts) { if (host.State == CommunicationState.Opened) { host.Close(); } else { host.Abort(); } } } } }
Ladislav Mrnka Sep 18 '10 at 9:20 2010-09-18 09:20
source share