Self Hosting in WCF

In this article, we will explain self-hosting a service using a Windows console application. This is referred to as a self hosting WCF service, the exact meaning of Self Hosted is that it hosts the service in an application that could be a Console Application or Windows Forms and so on. Earlier we saw what a WCF Service is in the .Net environment. We can host a WCF service in IIS and a Windows service also.
 
A service can also be in-process, in other words, the client and service are in the same process. Now let's create the WCF Service that is hosted in a console application. We will also look into creating a proxy using the "Client Base" class.
 
Step 1
 
First, let's start to create the Service Contract and its implementation. Create a console application and name it "WCF_NewsService". This is a simple service that returns News.
 
Hosting1.jpg 
 
Step 2
 
Add the System.ServiceModel and System.runtime.serialization reference to the project.
 
Hosting2.jpg 
 
Step 3
 
Now right-click the project name in Solution Explorer and select "Add new item" and select "WCF Service" in the window and specify "News_Service" and click "Add".
 
Hosting3.jpg 
 
Once you add this WCF Service, a function and class files are added to the solution.
 
Hosting4.jpg 
 
Step 4
 
In the "INews_Service" interface, add "ServiceContract" and "OperationContract" attributes to the class and function as in the following. You will learn more about these contracts in a later session. These contracts will expose methods to the outside world for using this service.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7.   
  8. namespace WCF_NewsService  
  9. {  
  10.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "INews_Service" in both code and config file together.  
  11.     [ServiceContract]  
  12.     public interface INews_Service  
  13.     {  
  14.         [OperationContract]  
  15.         TOInews Getnews(int a);  
  16.     }  
  17.     [DataContract]  
  18.     public class TOInews  
  19.     {  
  20.         private int _id;  
  21.         private string _header;  
  22.         private string _body;  
  23.         [DataMember]  
  24.         public int ID  
  25.         {  
  26.             get { return _id; }  
  27.             set { _id = value; }  
  28.         }  
  29.         [DataMember]  
  30.         public string Header  
  31.         {  
  32.             get { return _header; }  
  33.             set { _header = value; }  
  34.         }  
  35.         [DataMember]  
  36.         public string Body  
  37.         {  
  38.             get { return _body; }  
  39.             set { _body = value; }  
  40.         }  
  41.     }  
  42. }  
Step 5
 
Open the "New_Service" file and right-click on "INews_Service" then go to "implement Interface" and delete the DoWork() method that is not needed by us. And provide the following code:
 
Hosting5.jpg 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.Text;  
  7.   
  8. namespace WCF_NewsService  
  9. {  
  10.     // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "News_Service" in both code and config file together.  
  11.     public class News_Service : INews_Service  
  12.     {  
  13.         #region INews_Service Members  
  14.         public TOInews Getnews(int a)  
  15.         {  
  16.             TOInews objtoinews = new TOInews();  
  17.             switch (a)  
  18.             {  
  19.                 case 1:  
  20.                     {  
  21.                         objtoinews.ID = 1;  
  22.                         objtoinews.Header = "Mumbai News";  
  23.                         objtoinews.Body = "2013 Code contest quiz orgnize by TOI";  
  24.                         break;  
  25.                     }  
  26.                 case 2:  
  27.                     {  
  28.                         objtoinews.ID = 2;  
  29.                         objtoinews.Header = "Pune News";  
  30.                         objtoinews.Body = "commonwealth fraud in pune ";  
  31.                         break;  
  32.                     }  
  33.                 case 3:  
  34.                     {  
  35.                         objtoinews.ID = 3;  
  36.                         objtoinews.Header = "Solapur News";  
  37.                         objtoinews.Body = "New IT hub in Solapur";  
  38.                         break;  
  39.                     }  
  40.                 default:  
  41.                     {  
  42.                         break;  
  43.                     }  
  44.             }  
  45.             return objtoinews;  
  46.         }  
  47.         #endregion  
  48.     }  
  49. }
Step 6
 
Now after providing the code in the service we need to start our service using the console application The following code shows the implementation of the host process.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.ServiceModel;  
  6.   
  7. namespace WCF_NewsService  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             ServiceHost host = new ServiceHost(typeof(News_Service));  
  14.             host.Open();  
  15.             Console.WriteLine("Host Open Sucessfully ...");  
  16.             Console.ReadLine();  
  17.         }  
  18.     }  
  19. }  
Step 7
 
Now open the app.config file and change some settings.
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <configuration>  
  3.     <system.serviceModel>  
  4.       <bindings>  
  5.         <basicHttpBinding>  
  6.           <binding name="Pramod"></binding>  
  7.         </basicHttpBinding>  
  8.       </bindings>  
  9.       <behaviors>  
  10.             <serviceBehaviors>  
  11.                 <behavior name="">  
  12.                     <serviceMetadata httpGetEnabled="true" />  
  13.                     <serviceDebug includeExceptionDetailInFaults="false" />  
  14.                 </behavior>  
  15.             </serviceBehaviors>  
  16.         </behaviors>  
  17.         <services>  
  18.             <service name="WCF_NewsService.News_Service">  
  19.                 <endpoint address="News_Service"  
  20.                           binding="basicHttpBinding"  
  21.                           contract="WCF_NewsService.INews_Service"  
  22.                           bindingConfiguration="Pramod">  
  23.                     <identity>  
  24.                         <dns value="localhost" />  
  25.                     </identity>  
  26.                 </endpoint>  
  27.                 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />  
  28.                 <host>  
  29.                     <baseAddresses>  
  30.                         <add baseAddress="http://localhost:8732/Design_Time_Addresses/WCF_NewsService/News_Service/" />  
  31.                     </baseAddresses>  
  32.                 </host>  
  33.             </service>  
  34.         </services>  
  35.     </system.serviceModel>  
  36. </configuration>  
Step 7
 
Run your application and see message in console screen.
 
Hosting6.jpg 
 
Now create a consumer to consume the service. Again you need to create a new application and write the code in the Program.cs file.
 
Hosting7.jpg 
 
Before writing the code for these files we need to add a service reference for the NewsService Project.
 
For adding the service reference, right-click "WCF_NewsConsumer" in the Solution Explorer and select "Add Service reference". Copy the URL from the Service Project and paste it into the "Address location" and provide an appropriate name for the Service.
 
Here I am giving the name "Proxy_TOInews" for the namespace. Then click "OK".
 
Hosting8.jpg 
 
After adding the reference, add a namespace as in "using WCF_NewsConsumer.Proxy_TOInews;".
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using WCF_NewsConsumer.Proxy_TOInews;  
  6.   
  7. namespace WCF_NewsConsumer  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Proxy_TOInews.News_ServiceClient proxy = new News_ServiceClient("BasicHttpBinding_INews_Service");  
  14.             TOInews Tnews = new TOInews();  
  15.             Console.WriteLine("Enetr News Id :");  
  16.             string newsid = Console.ReadLine();  
  17.             Tnews = proxy.Getnews(Convert.ToInt32(newsid));  
  18.             News nws = new News();  
  19.             nws.cID = Tnews.ID;  
  20.             nws.cHeader = Tnews.Header;  
  21.             nws.cBody = Tnews.Body;  
  22.             Console.WriteLine(" News from:" + nws.cID + "\r\r \n " + nws.cHeader + "\r\r \n " + nws.cBody + "");  
  23.             Console.ReadLine();  
  24.         }  
  25.         public class News  
  26.         {  
  27.             private int _id;  
  28.             private string _header;  
  29.             private string _body;  
  30.             public int cID  
  31.             {  
  32.                 get { return _id; }  
  33.                 set { _id = value; }  
  34.             }  
  35.             public string cHeader  
  36.             {  
  37.                 get { return _header; }  
  38.                 set { _header = value; }  
  39.             }  
  40.             public string cBody  
  41.             {  
  42.                 get { return _body; }  
  43.                 set { _body = value; }  
  44.             }  
  45.         }  
  46.     }  
  47. }  
Now run both applications and enter a new id and hit the Enter key.
 
Hosting9.jpg 


Similar Articles