WCF IIS Hosting With Test Client

19:09

Introduction

In this articales I want to show how to hosting a Wcf service to IIS and testing this service to the WCF Test Client.

Advantage of IIS hosting

There are mainly two advantages of IIS hosting:
i) Automatic activation: Automatic activation means the service is not necessary to run. It always running.
ii) Process recycling: Any browser when starts, it create a instance means a worker process. Sometimes browser disconnect the process and the information is loss. That time IIS finds this issue and restarts the worker process. IIS restarts the worker process around 120 minuties. By restart the worker process this issue is resolved.

Disadvantage of IIS hosting

The main disadvantage of using IIS hosting is that, it will support only HTTP protocol.

Create Project

Go to Visual studio > File > New > Project > Templates > WCF > WCF Service Application > Provide a name (Ex: "WcfIISHosting") of project > Ok Like below Images:


We create a simple calculator function service like Add, Subtract, Multiply and Divide
IService1.cs
Now replace "IService1.cs" code by the below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfIISHosting
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        double Add(double a, double b);

        [OperationContract]
        double Subtract(double a, double b);

        [OperationContract]
        double Multiply(double a, double b);

        [OperationContract]
        double Divide(double a, double b);
    }
}
Service1.svc
After that replace "Service1.svc" code by the below code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfIISHosting
{
    public class Service1 : IService1
    {
        public double Add(double a, double b)
        {
            return a + b;
        }
        public double Subtract(double a, double b)
        {
            return a - b;
        }
        public double Multiply(double a, double b)
        {
            return a * b;
        }
        public double Divide(double a, double b)
        {
            return a / b;
        }
    }
}
Now create the service Endpoint to the Web.config file like below:
<system.serviceModel>
    <services>
      <service name="WcfIISHosting.Service1">
        <endpoint address="" binding="basicHttpBinding" contract="WcfIISHosting.IService1"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="IISHostingBehaviour">
          <!-- 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>    
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Our service is ready to host on IIS.
Windows + R then type inetmgr


Click Ok. We see that open the IIS server.


Now right click on the Sites and click Add Web Site... like below:


We can see a new window. Fill Site name (Ex: WcfIISHosting), Application pool, physical path, Port number and click Ok. Now right click on "WcfIISHosting" and browse. We look that new web site is create like below:


After click on browse the browser is open. Click on "Service1.svc" and we see the service is open like below image:


Now copy the address "http://localhost:85/Service1.svc". Now the service is ready to test.
Go to visual studio command prompt


Then type "wcftestclient"


After enter open the wcf test client window. Click on File > Add service... like below.


After click on "Add service..." paste the service address to the textbox. Like below:


Click Ok. We see an error window like below:


To resolved the issue go to "Web.config" and add the property [behaviorConfiguration="IISHostingBehaviour"]
So, after resolved the web.config file is below:
<system.serviceModel>
    <services>
      <service behaviorConfiguration="IISHostingBehaviour" name="WcfIISHosting.Service1">
        <endpoint address="" binding="basicHttpBinding" contract="WcfIISHosting.IService1"></endpoint>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="IISHostingBehaviour">
          <!-- 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>    
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https" />
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Now build the application and go to again the below step:


Click Ok. We see that error is resolved and our service function i.e. Add(), Subtract(), Multiply(), Divide() is show like below:


Now double click on "Add()" function and enter the value [a=7, b=8]


Click "Invoke" button and we see the total value [15]


Download

You can download application zip file here - 1.5 MB

Conclusion

Guys I try to explain IIS hosting with the Wcf service client my best. I think it will helpfull.

You Might Also Like

0 comments