Hello everyone !
Today, I will explain the process of hosting of WCF (Windows Communication Foundation) Service on IIS (Internet Information Services). In my first article, I explained about creating a simple WCF Service and hosting it on a console application.
For WCF, several types of hosting are available.
- Self Hosting in console application
- Windows Form application hosting
- Windows Service hosting
- WAS hosting
- IIS hosting
Here, I will explain you about IIS hosting. We have already developed WCF Service for addition in the previous article. So now, we have to host it on IIS.
- Open “MathService” WCF Service project with administrator rights (Run as Administrator) and then, add new web site (MathService_IIS) to its solution, as shown below.
- Now, add reference of the WCF Service to IIS Host project.
- Make the following changes in ‘Service.svc’ file.
Remove the ‘CodeBehind="~/App_Code/Service.cs" and also change the Service parameter value to ‘Service="MathService.MathService".
- Now, open the Web.config file and paste the below code (attached in zip file too).
- <?xml version="1.0"?>
- <configuration>
- <appSettings>
- <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
- </appSettings>
- <system.web>
- <compilation debug="false" targetFramework="4.5.1" />
- <httpRuntime targetFramework="4.5.1"/>
- </system.web>
- <system.serviceModel>
- <services>
- <service behaviorConfiguration="Default" name="MathService.MathService">
- <endpoint address="" binding="basicHttpBinding" contract="MathService.IMathService" />
- <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
- <host>
- <baseAddresses>
- <add baseAddress="http://localhost:7777/MathService" />
- </baseAddresses>
- </host>
- </service>
- </services>
- <behaviors>
- <endpointBehaviors>
- <behavior name="webBehavior">
- <webHttp />
- </behavior>
- </endpointBehaviors>
- <serviceBehaviors>
- <behavior name="Default">
- <serviceMetadata httpGetEnabled="true"/>
- <serviceDebug includeExceptionDetailInFaults="true"/>
- </behavior>
- </serviceBehaviors>
- </behaviors>
- </system.serviceModel>
- </configuration>
- Now, IIS host project is ready. So, we have to open IIS and host this project on it. So, for opening IIS, type ‘inetmgr’ on run window and hit Enter. It will open the IIS.
- If IIS does not open and gives any error, then you have to first enable IIS from Windows features. For this, follow the below steps.
- Open Control Panel =>> Programs and Features =>> Turn Windows features ON or OFF =>> Internet Information Services.
And enable the checkbox, as shown in the below image.
- Now, click on OK button. It will take some time and prompt you to restart the system. Then, Restart the system and open IIS by typing ’inetmgr’ command.
- After opening the IIS window, go to Server name, expand it, and go to ‘site’.
- Now, right click on ‘Site’ and go to ‘Add Website’.
- Now, give site name, physical path, and port. then, click on OK button.
- It will add new website on IIS. Now, select this site and click on ‘Content View’.
- Now, right click on ‘Service.scv’ file and go to ‘Browse’. This will open Service URL ( http://localhost:7777/Service.svc ) on browser.
- Now, our Service is successfully hosted on IIS. You can use it by using this URL http://localhost:7777/Service.svc.