Hosting the WCF Service in IIS

In this exercise we will host the WCF service created in the previous exercise with wsHttpBinding in IIS. There are many advantages of hosting a WCF service in IIS. In this exercise IIS 7.0 is used as a host.

Task 1

Open the WCF Service solution created in Exercise 1. Since we will be hosting the WCF service in IIS, we do not need to use the "WCF_ConsoleHost" project. Copy the App.Config from this project and paste it into the "WCF_Service" project, rename the "App.Config" file to "Web.Config" because we are using IIS as the host process. Change the binding from "basicHttpBinding" to "wsHttpBinding". From the "Web.Config" file remove the following tag:

<host>

  <baseAddresses>

    <add

baseAddress="http://localhost:9012/MyServ"/>

  </baseAddresses>

</host>

We do not require the preceding address, because the WCF Service will be automatically published on the Web Server and Virtual Durectory.

Task 2


We need to have a web extension for the WCF service hosted in IIS like a we have ".asmx" for a web service and ".aspx" for an ASP.Net page. So right-click on the "WCF_Service" project, select "Add New Item", select "TextFile" and rename it as "Service.svc" as below:

Hosting-the-WCF-Service in-IIS-1.jpg

Task 3

Write the following Hosting in the "Service.svc" file:
 

<%@ ServiceHost Service="WCF_Service.CService" Language="C#" Debug="true"%>


The directive "@ServiceHost" indicates to IIS that it should start the WCF runtime executing.

WCF Service

Task 4

 

Since IIS understands the path until the "\bin" directory, change the output path of the "WCF_Service" project to "\bin" as below:

Hosting-the-WCF-Service in-IIS-2.jpg

Task 5

 

Open IIS and create a virtual directory targeting to the "WCF_Service" project folder.

Hosting-the-WCF-Service in-IIS-3.jpg

You will have a virtual directory created.

Task 6

 

Form the Virtual Directory, right-click on "Service.svc" and select "Browse", you should get the following output:

Hosting-the-WCF-Service in-IIS-4.jpg

Instead of port number, you will get the IIS server name.

Now you can add the service reference of the preceding WCF service address to the Windows client application and test the application by running the client application directly.

Up Next
    Ebook Download
    View all
    Learn
    View all