WCF – IIS Hosting

Step 1 : Start Visual Studio 2012 and click File -> New -> Web site. Select “WCF Service” and Location as http. This will host the service in IIS. Click OK.

Output3 of IIS hosting

Step 2 : The code behind the interface in service cs file is given below

namespace IISDemoHostService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IISDemoHostService
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

 

Step 3 : The code behind the class file is given below.

namespace IISDemoHostService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
    public class Service1 : IISDemoHostService
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite == null)
            {
                throw new ArgumentNullException("composite");
            }
            if (composite.BoolValue)
            {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}

Step 4 : Service file (.svc) contains the name of the service and the code behind the file name. This file is used to know about the service. Right click the .svc file and click on View markup.

<%@ ServiceHost Language="C#" Debug="true" Service="IISDemoHostService.Service1" CodeBehind="IISDemoHostService.svc.cs" %>

Step 5 : Server-side configurations are mentioned in the config file. Here, there is a mention of only one end-point which is configured to ‘wsHttpBinding’; we can also have multiple endpoints with different bindings. Since we are going to host in IIS, we have to use only http binding.

<system.serviceModel>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="IISDemoHostService.Service1">
        <endpoint address="http://localhost/IISDemoHostService.svc"
        binding="wsHttpBinding" contract="IISDemoHostService.IISDemoHostService">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, 
  set the value below to false and remove the 
  metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="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>
  </system.serviceModel>

 

Step 6:You need to mention the service file name, along with the Address mentioned in the config file. The screenshot of IIS is given here. Click Start -> run -> inetmgr which will open the following window.

Output1 of IIS hosting

Step 7 : Run the application which will produce the following screen.

Output2 of IIS hosting