Tuesday, December 29, 2009

Configuring WCF services to work with both HTTP and HTTPS

1) Write two endpoints for the service, one is for http and another for https.
<services>
    <service behaviorConfiguration="MyServiceBehavior" name="JK.MyService">
      
      <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="JK.IMyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>

      <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webBindingHTTPS" contract="JK.IMyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>     
      
    </service>
  </services>
2) Enable both httpGetEnabled="True" httpsGetEnabled="true" in serviceBehaviors.
<behaviors>
    
    <serviceBehaviors>      
      <behavior name="MyServiceBehavior">
        <serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>      
    </serviceBehaviors>
    
    <endpointBehaviors>
      <behavior name="WebBehavior">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
    
  </behaviors>
3) Write two bindings configurations for http and https. For http give security mode="None" and for https give mode="Transport".
<bindings>
    <webHttpBinding>

      <binding name="webBinding">
        <security mode="None">
          <transport clientCredentialType="None" />
        </security>
      </binding>

      <binding name="webBindingHTTPS">
        <security mode="Transport">
          <transport clientCredentialType="None" />
        </security>
      </binding>

    </webHttpBinding>
  </bindings>
Below are the complete configuration settings in web.config:
<system.serviceModel>
  
  <services>
    <service behaviorConfiguration="MyServiceBehavior" name="JK.MyService">
      
      <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="JK.IMyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>

      <endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" bindingConfiguration="webBindingHTTPS" contract="JK.IMyService">
        <identity>
          <dns value="localhost" />
        </identity>
      </endpoint>

      <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      
    </service>
  </services>
    
  <behaviors>
    
    <serviceBehaviors>      
      <behavior name="MyServiceBehavior">
        <serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true" />
      </behavior>      
    </serviceBehaviors>
    
    <endpointBehaviors>
      <behavior name="WebBehavior">
        <webHttp/>
      </behavior>
    </endpointBehaviors>
    
  </behaviors>
  
  <bindings>
    <webHttpBinding>

      <binding name="webBinding">
        <security mode="None">
          <transport clientCredentialType="None" />
        </security>
      </binding>

      <binding name="webBindingHTTPS">
        <security mode="Transport">
          <transport clientCredentialType="None" />
        </security>
      </binding>

    </webHttpBinding>
  </bindings>
  
</system.serviceModel>

37 comments:

Anonymous said...

This was a great help. Thank you!

Anonymous said...

Thanks. This helped!

Anonymous said...

Thanks brother.Thanks a lot.

maxson said...

Hi,
This is so good and easy to understand.
However, I tried doing the same way to make it work on one of our site, it is giving 404 not found error on Json postback.
The problem is, http has always been working, but gives error when we are on https.
Just FYI, we can get to the website with https and browse. Just that when this service is called it errors out.
Could you please address on this issue.

Anonymous said...

Thank you!

Anonymous said...

Thank you so much! You save my day.

Anonymous said...

Good stuff!

Anonymous said...

Thank you for this. When I try this out, I get the following error:
Could not find a base address that matches scheme https for the endpoint with binding . Registered base address schemes are [http]
If I comment out the https endpoint, the error disappears (understandably).
Will you be able to help. Thanks heaps! Sam.

Anonymous said...

GR8 Post..

Anonymous said...

worked really gud stuff ,thx

Unknown said...

I have been looking for something like this for a day now. Great work!

Kiran said...

Excellent post. It is really a time saver to me. Thanks a lot.

Kanagal Raj said...

Good blog.

Anonymous said...

Good Post!!!

Anonymous said...

Great!!!

siv said...

Hi jayakrishna,

I got the error below when I followed this approach.

Configuration binding extension 'system.serviceModel/bindings/true' could not be found. Verify that this binding extension is properly registered in system.serviceModel/extensions/bindingExtensions and that it is spelled correctly.

Here's my config file:






















































govind singh bisht said...

Done the same ...still showing the error:
Could not find a base address that matches scheme https for the endpoint with binding WebHttpBinding. Registered base address schemes are [http]

Any help would be appreciated...Thanks in advance

Harpreet Singh said...

Great post!!!!!!!!!!
/Harpreet

Jerry said...

Wonderful ! Thanks a lot !!!

Oliver Atom said...

Hi, thanks for the data but i do this and the http service is working but the https is getting SSL connection error "ERR_SSL_PROTOCOL_ERROR" any idea why?

Anonymous said...

Thank you!

Anonymous said...

u r awesome

Gaurav Chaudhary said...

Thankyou - this post helped me

Anonymous said...

Very nice... just works

Anonymous said...

Wonderful ! Thanks a lot !!!

Guru said...

Wow, very good, clean and clear thanks you so much, big helps.

Guru said...

It's worked on production, but don't work on debug mode. i can't run on visual studio.
Error:
Could not find a base address that matches scheme https for the endpoint with binding WebHttpBinding. Registered base address schemes are [http].

Unknown said...

Thank you man, I tried many things but failed. I implement this according to your guideline, my problem resolved in couple of minutes. Thanx again

Anonymous said...

Thank you for a working solution!

Anonymous said...

Thanks you save my time alomst 2 days I was looking for that..


thanks Again

Anonymous said...

Thanks for Great Post

Shiva said...

Great Post... Helped me a lot

Anonymous said...

yes, its helpful and working with http and https both

Anonymous said...

Thanks á´ ery nice blog!

Anonymous said...

Pretty! This has been an extremely wonderful post. Thanks for supplying this information.

Anonymous said...

Appreciate this post. Will try it out.

Unknown said...

Extremely helpful! Thank you so much Jayakrishna!!

Post a Comment