Hosting WCF Service: Part 3

Introduction

For a WCF service to be available for the clients to consume, we need to host the WCF service. I highly recommend reading the previous parts:

  1. Hosting WCF Service: Part 1
  2. Hosting WCF Service: Part 2
Internet Information Services (IIS)

No code is required to host a WCF service, there is no need to write code to instantiate and start the service host, as is the case with self-hosting. Automatic message based activation of IIS provides automatic message-based activation. This means that the service can be activated on demand. When a message arrives at the service, it then launches itself and fulfils the request. In case of self-hosting, the service should be always be running. 

Step 1

Creating WCF Service
  1. Open Visual Studio.

  2. Go To File -> New Project.

  3. Select WCF Service Application.

  4. Provide it a name like "SquareService".

    SquareService

    Figure 1: SquareService

  5. Delete the default created Service1.cs and Service1.svc file.

  6. Add a new svc file as ini the following and name it SquareService, then right-click on the application and select Add -> New Item and Select WCF Service.

    WCF Service

    Figure 2: WCF Service
      
    WCF Service1

    Figure 3: WCF Service1

  7. Replace the following code in the ISquareService interface.
    1. [ServiceContract]  
    2. public interface ISquareService   
    3. {  
    4.     [OperationContract]  
    5.     void DoWork();  
    6. }  
    With:
    1. [ServiceContract]  
    2. public interface ISquareService   
    3. {  
    4.     [OperationContract]  
    5.     int GetSqaure(int num);  
    6. }  
  8. Replace the following code in the SquareService.svc file:
    1. public void DoWork()   
    2. {  
    3.       
    4. }  
    5. With: public int GetSqaure(int num)   
    6. {  
    7.     return num * num;  
    8. }  
  9. Build the solution.

Step 2

Now to set up to publish the service to be hosted in IIS.

  1. Right-click on Application, select Publish.

    Publish

    Figure 4: Publish

  2. Create profile to publish the application.

    IISHostService

    Figure 5: IISHostService

    When you click on Custom, it will show like the preceding entered profile name IISHostService.

  3. After clicking Next it will go to the next step. 

    File System

    Figure 6: File System

    Select the Publish method then select File System, it will ask for the default path to publish the service.

    Publish Method

    Figure 7: Publish Method

  4. Click on the Next Button, it will show the profile created previously with the option select Configuration to Build 1. Debug 2. Release. The publish options are like the following screen:

    Configuration

    Figure 8: Configuration

  5. Click on the Publish button, it will show the following screen:

    Output Window

    Figure 9: Output window

    In the preceding screen you can see the Output window and the folder created in the Solution Explorer and that we created the profile name IISHostService.

  6. You can see the folder path also, like in the following screen:

    folder path

         Figure 10: Folder Path

Step 3

Hosting Created service on IIS.

  1. Click on the Start Menu then search for inetmgr then press Enter, it will open like the following:

    inetmgr

    Figure 10: Inetmgr 

  2. Right-click on Sites then select Add Web Site.

    Add Web Site

    Figure 11: Add Web Site

  3. It will open a new window as in the following screen.

    Path of Publish folder

    Figure 12:Path of Publish folder

    Enter the site Name and the path of the Publish folder and assigned the port number then click OK.

  4. Now you can see that the web site is added in IIS.

    Added in the IIS

    Figure 13: Added in the IIS

  5. Right-click on SquareService.svc then select Browse.

    Browse

    Figure 14: Browse

  6. Now you see in the output that we created the service is hosted on IIS.

    Hosted on IIS

    Figure 15:Hosted on IIS

Step 4

Consume the IIS hosted service in Console Application.

  1. Open Visual Studio.

  2. Go to File-> New Project.

  3. Select Console Application then name it IISConsume then click OK.

    Console Application

    Figure 16: Console Application

  4. Open Solution Explorer then right-click on References then select Add Service References.

    Add Service References

    Figure 17: Add Service References

  5. It will show a window like the following screen.

    Popup Window

    Figure 18: Popup Window

  6. Copy the browse service URL then paste it into the Address: box then click the Go Button then it will show the following screen.

    Services box

    Figure 19: Services box

    In the preceding screen, you can see that the Services box shows the interface name and inside the Operations box it showa the method name GetSquare and provides the namespace name IISHostedService then click OK.

  7. Now you can see in Solution Explorer there is one folder added like the following screen.

    Solution Explorer

    Figure 20:Solution Explorer

  8. Now to test the IIS hosted service. Use the following code in the Program.cs file.

    Code to consume the service:
    1. static void Main(string[] args)   
    2. {  
    3.     IISHostedService.SquareServiceClient service = new IISHostedService.SquareServiceClient();  
    4.     var res = service.GetSqaure(5);  
    5.     Console.WriteLine("Sqaure of 5 is {0}", res);  
    6.     Console.ReadKey();  
    7. }  
  9. Build the application and Run. It will show output like following:

    Build the application

    Figure 21:Build the application

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

Summary

I hope you understand the concepts of Hosting a WCF Service on Internet Information Services (IIS) and it is useful for all readers. If you have any suggestion regarding this article then please contact me.

 


Similar Articles