Hosting WCF Service at Runtime in C#

Introduction

This article introduces how to host your WCF service using your own C# code. I am writing this article because I think sometimes we may need a hot service at run time only, for this kind of situation this article may help you.

For example, suppose you're in the situation that you don't want to host your service in your server, you want to host it in the client's machine, where the application is running. Then you need to write code to host your service.

Getting Started

This article shows how to host a service using C# code. Hosting a WCF Service is very easy, you just need to use the following simple procedure.

Points

1. Retrieve service details from the assembly

2. List all the services

3. Host the retrieved service

Points 1 and 2: Retrieve and List all the services

Before hosting a WCF service you need to naintain the information of that service, such as endpoint, endpoint address, bindings and so on. That information is required for hosting and are available in your application config file, when you reference the service. Hence we need to retrieve all the information of the service available in the config file. Previously I wrote the article “Listing All the WCF Services Available in .config File at Run Time in C#” that explains how to list services.

The following is the code for listing services available in the config file.

 
  1. XDocument XMLConfigFile = XDocument.Load(configFile);  
  2. List<string> ServiceNames = XMLConfigFile.Descendants("service").Attributes("name").Select(AT => AT.Value).ToList();  
  3. Assembly assembly;      
  4.    string BasePath;    
  5.    BasePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + onfigurationManager.AppSettings["ServiceAssembly"].ToString();      
  6.    assembly = Assembly.LoadFrom(BasePath);  
  7. List<Type> ServiceTypes= new List<Type>();           
  8.             foreach (string servicename in ServiceNames)     
  9.             {  
  10.                 Type type = assembly.GetType(servicename);  
  11.                 assmTypes.Add(type);  
  12.             }  
 Point 3: Host the retrieved service

Hosting a WCF Service at runtime is very easy, the ServiceHost class helps to host WCF services at runtime. This class is in the System.ServiceModel namespace.

The following is the code for hosting services:

  1. List<Type> serviceTypes = new List<Type>();  
  2.             List<ServiceHost> hostingbroker = new List<ServiceHost>();  
  3.             foreach (Type servicetype in serviceTypes)  
  4.             {  
  5.                 ServiceHost SH = new ServiceHost(servicetype);  
  6.                 SH.Open();  
  7.                 hostingbroker.Add(SH);  
  8.             }  
In the preceding code line 1 is the list of services retrieved from the config file, code line 2 contains something for listing the hosted services.
 
Full Code
 
  1. XDocument XMLConfigFile = XDocument.Load(configFile);  
  2. List<string> ServiceNames = XMLConfigFile.Descendants("service").Attributes("name").Select(AT => AT.Value).ToList();  
  3. Assembly assembly;      
  4.    string BasePath;    
  5.    BasePath = System.AppDomain.CurrentDomain.BaseDirectory + "\\" + onfigurationManager.AppSettings["ServiceAssembly"].ToString();      
  6.    assembly = Assembly.LoadFrom(BasePath);  
  7. List<Type> ServiceTypes= new List<Type>();           
  8.             foreach (string servicename in ServiceNames)     
  9.             {  
  10.                 Type type = assembly.GetType(servicename);  
  11.                 assmTypes.Add(type);  
  12.             }  
  13.   
  14.             List<ServiceHost> hostingbroker = new List<ServiceHost>();  
  15.             foreach (Type servicetype in serviceTypes)  
  16.             {  
  17.                 ServiceHost SH = new ServiceHost(servicetype);  
  18.                 SH.Open();  
  19.                 hostingbroker.Add(SH);  
  20.             }  
Summary
 
In this article we learned how to host a WCF service using C# at runtime.
 

Up Next
    Ebook Download
    View all
    Learn
    View all