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. namespace WCF_NewsService
  8. {
  9. // 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.
  10. [ServiceContract]
  11. public interface INews_Service
  12. {
  13. [OperationContract]
  14. TOInews Getnews(int a);
  15. }
  16. [DataContract]
  17. public class TOInews
  18. {
  19. private int _id;
  20. private string _header;
  21. private string _body;
  22. [DataMember]
  23. public int ID
  24. {
  25. get { return _id; }
  26. set { _id = value; }
  27. }
  28. [DataMember]
  29. public string Header
  30. {
  31. get { return _header; }
  32. set { _header = value; }
  33. }
  34. [DataMember]
  35. public string Body
  36. {
  37. get { return _body; }
  38. set { _body = value; }
  39. }
  40. }
  41. }
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. namespace WCF_NewsService
  8. {
  9. // 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.
  10. public class News_Service : INews_Service
  11. {
  12. #region INews_Service Members
  13. public TOInews Getnews(int a)
  14. {
  15. TOInews objtoinews = new TOInews();
  16. switch (a)
  17. {
  18. case 1:
  19. {
  20. objtoinews.ID = 1;
  21. objtoinews.Header = "Mumbai News";
  22. objtoinews.Body = "2013 Code contest quiz orgnize by TOI";
  23. break;
  24. }
  25. case 2:
  26. {
  27. objtoinews.ID = 2;
  28. objtoinews.Header = "Pune News";
  29. objtoinews.Body = "commonwealth fraud in pune ";
  30. break;
  31. }
  32. case 3:
  33. {
  34. objtoinews.ID = 3;
  35. objtoinews.Header = "Solapur News";
  36. objtoinews.Body = "New IT hub in Solapur";
  37. break;
  38. }
  39. default:
  40. {
  41. break;
  42. }
  43. }
  44. return objtoinews;
  45. }
  46. #endregion
  47. }
  48. }
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. namespace WCF_NewsService
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. ServiceHost host = new ServiceHost(typeof(News_Service));
  13. host.Open();
  14. Console.WriteLine("Host Open Sucessfully ...");
  15. Console.ReadLine();
  16. }
  17. }
  18. }
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. namespace WCF_NewsConsumer
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Proxy_TOInews.News_ServiceClient proxy = new News_ServiceClient("BasicHttpBinding_INews_Service");
  13. TOInews Tnews = new TOInews();
  14. Console.WriteLine("Enetr News Id :");
  15. string newsid = Console.ReadLine();
  16. Tnews = proxy.Getnews(Convert.ToInt32(newsid));
  17. News nws = new News();
  18. nws.cID = Tnews.ID;
  19. nws.cHeader = Tnews.Header;
  20. nws.cBody = Tnews.Body;
  21. Console.WriteLine(" News from:" + nws.cID + "\r\r \n " + nws.cHeader + "\r\r \n " + nws.cBody + "");
  22. Console.ReadLine();
  23. }
  24. public class News
  25. {
  26. private int _id;
  27. private string _header;
  28. private string _body;
  29. public int cID
  30. {
  31. get { return _id; }
  32. set { _id = value; }
  33. }
  34. public string cHeader
  35. {
  36. get { return _header; }
  37. set { _header = value; }
  38. }
  39. public string cBody
  40. {
  41. get { return _body; }
  42. set { _body = value; }
  43. }
  44. }
  45. }
  46. }
Now run both applications and enter a new id and hit the Enter key.
Hosting9.jpg