IIS hosting is much reliable to other types of hosting. When we host our service on IIS, it automatically takes all the features of IIS like process recycling, Idle shutdown, message based activation and many others. There is also one disadvantage of IIS hosting, it only supports HTTP protocol.
Create WCF Project
Open Visual Studio, Go to File Menu, Choose New and then click on Project. It will open the following window where you can choose your WCF service. You can create your WCF service using “WCF Service Library” or “WCF Service Application”. You need to choose WCF Service Application and provide the suitable name for the service as “WCFServiceIISHostingDemo” and click on OK.
It will take some time to create a new WCF Service and create a sample template as in the following. We are going to create fresh step by step WCF service. So, first delete these two components: “IService1.cs” and “Service.svc”.
After deleting both files, right click on Project and choose Add and then choose New Item. It will open a popup window where you can choose different types of file to be added. You need to select WCF Service and provide the suitable name for it and click OK.
It will add an Interface for the service and also a service file. The following will be the structure of the WCF service.
Now make some changes in the code as in the following;
MathService.svc.cs
- namespace WCFServiceIISHostingDemo
- {
- public class MathService: IMathService
- {
- public int AddTwoNumber(int firstNumber, int secondNumber)
- {
- return firstNumber + secondNumber;
- }
- }
- }
IMathService.cs - using System.ServiceModel;
- namespace WCFServiceIISHostingDemo
- {
-
- [ServiceContract]
- public interface IMathService
- {
- [OperationContract]
- int AddTwoNumber(int firstNumber, int secondNumber);
- }
- }
Web.Config - <?xml version="1.0"?>
- <configuration>
-
- <appSettings>
- <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
- </appSettings>
- <system.web>
- <compilation debug="true" targetFramework="4.5" />
- <httpRuntime targetFramework="4.5"/>
- </system.web>
- <system.serviceModel>
- <services>
- <service name="WCFServiceIISHostingDemo.MathService">
- <endpoint address="http://localhost/WCFServiceIISHostingDemo/MathService.svc" binding="wsHttpBinding" contract="WCFServiceIISHostingDemo.IMathService">
- <identity>
- <dns value="localhost"/>
- </identity>
- </endpoint>
- <endpoint
- address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
- </service>
- </services>
- <behaviors>
- <serviceBehaviors>
- <behavior>
- <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
- <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
- <!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
- <serviceDebug includeExceptionDetailInFaults="false"/>
- </behavior>
- </serviceBehaviors>
- </behaviors>
-
-
- <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
- </system.serviceModel>
- <system.webServer>
- <modules runAllManagedModulesForAllRequests="true"/>
- <!--
- To browse web app root directory during debugging, set the value below to true.
- Set to false before deployment to avoid disclosing web app folder information.
- -->
- <directoryBrowse enabled="true"/>
- </system.webServer>
-
- </configuration>
Now, it’s time to run the project. So, press F5. It will open a
WCF Test Client. Here you will find your method, you can just pass your parameter and click on execute. It will show you the output.
Publish Your Service
It is time to publish your service that can be hosted on IIS. So, to publish your service right click on the project and choose publish. It will open a window where you can choose profile for publishing. You need to choose
Custom and provide the name of profile. And from the
Connection tab, you can choose
File System and provide the hosting path in wwwroot. And finally
click on Publish. It will take some time and publish your WCF service inside the wwwroot.
So, open
integer from search program and you will find your publish directory here.
Right Click on publish directory “WCFServiceIISHostingDemo” and choose Convert Application. Provide default pool for this.
You will take care that default pool should be 4.0
Now run again from browser using the = following url.
http://localhost/WCFServiceIISHostingDemo/MathService.svc So, finally service is hosted on IIS. The WCF service is running fine.
Thanks for reading this article, hope you enjoyed it.