Thursday, May 24, 2012

Configure a WCF service for https and http protocols

Configuring a service to serve http and https is become a common requirement for all  the services. I have spent googling on this. After spending about 6 hours I fincdout below one would configure your service for both http and https.


<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="httpsConfiguration" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">         
          <security mode="Transport">
            <transport clientCredentialType ="None"/>
          </security>
        </binding>
        <binding name="httpConfiguration" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">         
        </binding>
      </basicHttpBinding>

    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpsGetEnabled="true" httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="ServiceBehavior" name="YourServiceName">
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="httpsConfiguration" contract="YourServiceContract">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="httpConfiguration" contract="YourServiceContract">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>

1 comment: