Hosting WCF Service: Part 1

Introduction

For a WCF service to be available for the clients to consume, we need to host the WCF service. The following are the various options available for hosting a WCF Service.

  1. Self-Hosting
  2. Windows Service
  3. Internet Information Services (IIS)
  4. Windows Activation Services (WAS)

Now we can learn one by one about WCF service hosting options as follows.

Self-Hosting

A WCF service can be self-hosting using a console application or Windows Forms applications. Hosting a WCF service in any managed .Net application is called self hosting. Now the following is the procedure for creating a WCF service and hosting it in a console application.

Step 1: Creating WCF Service

  1. Open Visual Studio.

  2. File -> New Project.

  3. Select Class Library.

  4. Provide it a name like "SquareService".

    SquareService

  5. Delete the default class1.cs file.

  6. Right-click on the project then select Add -> New Item.

    New Item

  7. Select WCF Service name as like "SquareService"

    WCF Service

  8. Add one method in interface and implement it "GetSquare()".

    Declaration
    1. [ServiceContract]  
    2. public interface ISquareService  
    3. {  
    4.    [OperationContract]  
    5.    int GetSqaure(int number);  
    6. }  
    Implementation
    1. /// <summary>  
    2. /// GetSqaure  
    3. /// </summary>  
    4. /// <param name="number"></param>  
    5. /// <returns></returns>  
    6. public int GetSqaure(int number)  
    7. {  
    8.     var result = number * number;  
    9.     return result;  
    10. }  
  9. We didn't need here app.config file because we not configure anything.

  10. Finally we created the WCF service for Self-Hosting and Build it.
Step 2: Create a Console application to host WCF Service
  1. Right-click on the solution "SqaureService".

  2. Add -> New Project.

  3. Select Console Application.

  4. Name it something like "ServiceHost".

    Console Application

  5. Click OK.

  6. Now add two references, a SquareService reference and a System.ServiceModel assembly.

    ServiceHost

    This is for adding the System.ServiceModel assembly.

    ServiceModel

    This is for the created WCF service reference.

  7. Now we need to change the App.config file to host. To expose the endpoint, generate proxy classes for the client and enable metadata exchange.

  8. For that right-click on the App.config file then select option "Edit WCF Configuration". (You do this also by adding these one by one).

  9. It will show a pop-up like the following.

    popup

  10. Select the "Services" folder then click on the right side link Create a New Service like in the following image.

    Create a New Service

  11. Click on the Browse Button and open the bin folder of SqaureService.

    SqaureService

    service name

    service type

  12. Click on the Next Button.

  13. It will ask you to provide the Service Contract.

    Service Contract

  14. Click the Next Button.

  15. It will ask you to select the binding TCP binding we used here and click next.

    http

  16. Enter the endpoint address; we enter it as in the following image.

    endpoint address

  17. Enter a relative address to determine the service and SquareService.

  18. Here you can see all the features we selected previously.

    select features

  19. Click Finish.

  20. Hare we need to select the base address because we select the relative address then click on the "Host" tab of the left side.

    Host

  21. Here we added two base addresses, one for the service endpoint and the other to enable metadata exchange.

    metadata

    service endpoint

  22. We need to enable service behaviours to enable metadata exchange, clients can generate proxy classes using that. Expand the "Advanced" folder then select the "Service Behaviour" option.

    Service Behaviour

  23. We need to create a new service behaviour configuration, because currently there is no service behaviours.

    ServiceMetadata

  24. We need to add a new service behaviour for metadata exchange. Select NewBehavior then select the Add button then select from the list ServiceMetadata.

    select service metadata

  25. Then click on serviceMetatdata to set HttpGetEnable to True.

    HttpGetEnable

  26. Now click on the newly created NewBehavior and change its name to mexBehavior.

    mexBehavior

  27. We need to set this new behavior to our service. For that click on our SquareService and set the Behaviour Configuration to mexBehavior.

    Behaviour Configuration

    created behavior

  28. Now close this window. Then it will ask for yu to save the changes; click Yes.

    Yes

  29. Now check your App.config file, we created all the features automatically. Provide the following in the App.config file.
    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <configuration>  
    3.     <startup>   
    4.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />  
    5.     </startup>  
    6.     <system.serviceModel>  
    7.         <behaviors>  
    8.             <serviceBehaviors>  
    9.                 <behavior name="mexBehavior">  
    10.                     <serviceMetadata httpGetEnabled="true" />  
    11.                 </behavior>  
    12.             </serviceBehaviors>  
    13.         </behaviors>  
    14.         <services>  
    15.             <service behaviorConfiguration="mexBehavior" name="SqaureService.SquareService">  
    16.                 <endpoint address="SquareService" binding="netTcpBinding" bindingConfiguration=""  
    17.                     contract="SqaureService.ISquareService" />  
    18.                 <host>  
    19.                     <baseAddresses>  
    20.                         <add baseAddress="http://localhost:8080" />  
    21.                         <add baseAddress="net.tcp://localhost:8090" />  
    22.                     </baseAddresses>  
    23.                 </host>  
    24.             </service>  
    25.         </services>  
    26.     </system.serviceModel>  
    27. </configuration>  
  30. Now test the self-hosted service. Open the Program.cs file and make the following changes.
    1. class Program  
    2. {  
    3.     static void Main(string[] args)  
    4.     {  
    5.         SqaureService.SquareService src = new SqaureService.SquareService();  
    6.         var result = src.GetSqaure(5);  
    7.         Console.WriteLine("Self-Hosting a WCF Service in Console Application\n");  
    8.         Console.WriteLine("Square of 5 is " + result +"\n");  
    9.         Console.WriteLine("Finish");  
    10.         Console.ReadKey();  
    11.     }  
    12. }  
    Output: Run the Console Application. It will show output like in the following image.

    Output
Note: For detailed code please download the Zip file attached above.

Summary

I hope you understand the concepts of hosting a WCF Service and it is useful for you. If you have any suggestion regarding this article then please contact me.

Up Next
    Ebook Download
    View all
    Learn
    View all