Listing All the WCF Services Available in .config File at Run Time in C#

This article demonstrates how to list the services we have taken references of at run time.

Introduction

This can be done using some classes like: XDocument and Assembly. These classes take a major role in getting the service information, hence I specified here these classes. The XDocument class helps to read the config file and Assembly gives the details about services.
 
Getting Started
 
We need to wrap up some major steps for what I have said in this article. The procedure is:
  1. Read the App.config and get all the declared services and create the type of the service to help the console to host the service.

  2. Load the config file using the XDocument class.

  3. Query all the services declared in the config file using LINQ or loop from the service tag.

  4. Load the assembly that holds all the services that you have taken a reference of.
Read the App.config
 
We first need to read the App.config and get all the declared services and create the type of the service to help the console to host the service.

The XDocument class helps to load the application's config file. The load function of this class takes the path of the config file as parameter. This class is in the System.Xml.Linq namespace. Hence you need to get the reference of this namespace. 
  1. XDocument XMLConfigFile = XDocument.Load(configFile);   
Query all the services
 
Next we need to query all the services declared in the config file using LINQ or loop from the service tag.
 
We can query the name of the services from the config file in one of two ways,  one is using an traditional loop or and the other is LINQ. Though LINQ is advanced in the current market. Here I have used LINQ to get the name of the service. 

  1. List<string> ServiceNames = XMLConfigFile.Descendants("service").Attributes("name").Select(AT => AT.Value).ToList();
  2.  
Load the assembly
 
Then we need to load the assembly that holds all the services that your have taken a refrence of.
 
We can load the assembly of our application using the Assembly class in the System.Reflection namespace. The loadfrom function of this class helps to load an application's assembly. This function takes the path of the assembly as a parameter, then the path should be the base path of the application. We can get the base path of our application using the BaseDirectory class in the System.AppDomain.CurrentDomain namespace.  See the following code.
  1. Assembly assembly;    
  2.    string BasePath;  
  3.    BasePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + ConfigurationManager.AppSettings["ServiceAssembly"].ToString();    
  4.    assembly = Assembly.LoadFrom(BasePath);  
List All the services 
  1. foreach (string sericename in ServiceNames)  
  2. {  
  3.      Type type = assembly.GetType(sericename.ToString());  
  4.      Console.WriteLine(type.ToString());  
  5. }  
 Full Code 
  1. XDocument XMLConfigFile = XDocument.Load(configFile);  
  2.   
  3. List<string> ServiceNames = XMLConfigFile.Descendants("service").Attributes("name").Select(AT => AT.Value).ToList();  
  4.   
  5. foreach (string sericename in ServiceNames)    
  6. {    
  7.      Type type = assembly.GetType(sericename.ToString());    
  8.      Console.WriteLine(type.ToString());    
  9. }   
 Summery
 
This article described how the services, that we have taken references of, can be listed at run time.