Hosting WCF REST Service in IIS with HTTPS


In my earlier article I discussed Exposing WCF REST Service over HTTPS. A major limitation of the service created in that article was it's self-hosted nature. In this article, I will continue with that service and host it in IIS. So at the end of this article, you will be able to host a WCF REST Service in IIS with HTTPS.

Configure IIS

To start with let us first configure a Web Site in IIS.

Open IIS

  1. Open the command prompt in administrator mode. To do that click on the Start button then type RUN in search and then in Run window type inetmgr to open IIS.

    WCFHttps1.gif

    Now you will have IIS open like below:

    WCFHttps2.gif
     
  2. Right-click on Sites and then click Add Web Site

    WCFHttps3.gif
     
  3. Now a window will open.

    WCFHttps4.gif

Give any name of your choice as the site name. I am using the name HostedWcfService
WCFHttps5.gif

Now click on select and choose ASP.Net v4.0 from the drop down.
 

WCFHttps6.gif

Now in the physical path section, we need to give the physical path of the service. So to get the physical path of the service, right click on the WCF Service project in Visual Studio and click open project in the Windows Explorer. Open the project in Windows Explorer and from the address bar copy the path and paste that below. Or you can browse to it. Just click on the browse button and navigate to the project folder of the WCF Service.


WCFHttps7.gif

Now at the Binding section select HTTPS. Give any port number. I am leaving default port.


WCFHttps8.gif

Again I am selecting DEBUGMODE certificate.

Note: the steps to create a certificate is in my previous article Exposing WCF REST Service over HTTPS

Configure WEB.Config

In the last article we created a self-hosted WCF REST Service. Now we need to host the service in IIS. So we need to configure EndPoint in Web.Config.

Open Web.Config of WCF Service
  1. Add service End Point

    WCFHttps9.gif

    IService1 is Service Contract name and svcBehavior is service behavior. And of course for REST service we are using webHttpBinding.
     
  2. Create Service Behavior

    WCFHttps10.gif
     
  3. Add Transport level security to webHttpBinding

    WCFHttps11.gif

WEB.Config will look like:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="svcBehavior">        
          <serviceMetadata httpGetEnabled="true"/>         
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding>
          <security mode="Transport"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="WcfService13.IService1" behaviorConfiguration="svcBehavior">
        <endpoint name="" binding="webHttpBinding" address="" contract="WcfService13.IService1" />        
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  
</configuration>


Publish Service


Right-click on WCF Service and click on Publish

  1. Choose Web Deploy

  2. At Site URL give any URL of your choice

  3. In Site Application give the name of the site you created in the beginning of this article.

    WCFHttps12.gif

In this article we saw how to host WCF REST Service in IIS with HTTPS. Thanks for reading. I hope it was useful.
 

Up Next
    Ebook Download
    View all
    Learn
    View all