Click here to Skip to main content
Click here to Skip to main content

WCF - Automatically create ServiceHost for multiple services

, 18 Jan 2013
Rate this:
Please Sign up or sign in to vote.
Automatically create ServiceHost for multiple services.

WCF - Automatically create ServiceHost for multiple services

Welcome back readers! This blog post is about a small tip that may make working with WCF servicehost a bit easier, if you have lots of services and you need to quickly host them for testing.
Recently I was encountered a situation where we were faced to create multiple service host quickly for testing. Here is the code snippet which is pretty self explanatory. You can put this code in your service host which in this case is a console application.

class Program
{
   static void Main(string[] args)
   {                        
       // Stores all hosts
       List<ServiceHost> hosts = new List<ServiceHost>();

       try
       {                                   
           // Get the services element from the serviceModel element in the config file
           var section = ConfigurationManager.GetSection("system.serviceModel/services") as ServicesSection;
           if (section != null)
           {
               foreach (ServiceElement element in section.Services)
               {                                                
                   // NOTE : If the assembly is in another namespace, provide a fully qualified name here in the form
                   // <typename, namespace>                        
                   // For e.g. Business.Services.CustomerService, Business.Services

                   var serviceType = Type.GetType(element.Name);  // Get the typeName 
                   var host = new ServiceHost(serviceType);

                   hosts.Add(host);  // Add to the host collection
                   host.Open();      // Open the host
               }
           }

           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();
               }
           }
       }
   }
} 

I hope you find this useful. You can make this as a Windows service if required.

License

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

About the Author

Rajesh Pillai
Architect
India India
Co Founder at TekAcademy Labs and Principal Consultant and Chief Architect as Megasoft Informations Systems Pvt. Ltd.
 
http://tekacademy.com/
Follow on   Twitter

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 18 Jan 2013
Article Copyright 2013 by Rajesh Pillai
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid