WebHttpBinding with Http and Https

Billa picture Billa · Apr 12, 2016 · Viewed 16.2k times · Source

I am trying to use https & http for the website. The website has .svc files which act as REST service and called from JavaScript.

My Config:

<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AjaxBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="MyServiceBehaviour">
          <serviceDebug includeExceptionDetailInFaults="true" />
          <serviceMetadata httpGetEnabled="True" httpsGetEnabled="true"/>         
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
    <services>
      <service behaviorConfiguration="MyServiceBehaviour" name="MyService.Lookups">
        <endpoint address="" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="httpWebBinding" contract="MyService.Lookups" >         
        </endpoint>
        <endpoint address="" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="httpsWebBinding" contract="MyService.Lookups" >          
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>      
      <webHttpBinding>
        <binding name="httpsWebBinding">
          <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None" />
          </security>
        </binding>
        <binding name="httpWebBinding">
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None" />
          </security>
        </binding>
      </webHttpBinding>
    </bindings>   
  </system.serviceModel>

Browsing https://myserver/services/Lookups.svc/Hello gives

Could not find a base address that matches scheme http for the endpoint with binding WebHttpBinding. Registered base address schemes are [https]

Browsing http://myserver/services/Lookups.svc/Hello gives

Could not find a base address that matches scheme https for the endpoint with binding WebHttpBinding. Registered base address schemes are [http]

If I remove any one endpoint it works. Example removing endpoint configured with bindingConfiguration="httpWebBinding" works for HTTPS ,

How can I make it work with HTTP and HTTPS? As of now, I can able to use either http or https by removing one endpoint.

Referred How can I combine the WCF services config for both http and https in one web.config? and How do you setup HTTP and HTTPS WCF 4 RESTful services?

Note: In IIS, it is two web sites one listen on http and another on https. Both sharing same code in physical folder

UPDATE: As of now, I removed endpoints and it works. But my concern is removing endpoing configured with behaviourConfiguration doesnt look great solution to me.

This works for both http & https

  <services>
      <service behaviorConfiguration="MyServiceBehaviour" name="MyService.Lookups">

      </service>
    </services>

Answer

Lesmian picture Lesmian · Apr 15, 2016

I've recreated your scenario and used your web.config to configure endpoints for my test service. Your configuration is ok and works correctly. The part that don't works for you is probably your https configuration in IIS. Make sure you have enabled https access to your service. If you test it with IISExpress from Visual Studio then left click on your project and in the properties window (View -> Properties Window ) select for SSL Enabled = True.