Support both HTTP and HTTPS Web.Config WCF Service Support both HTTP and HTTPS Web.Config WCF Service azure azure

Support both HTTP and HTTPS Web.Config WCF Service


I've been trying to do this for days (years actually, but just restarted a few days ago), and the post above pointed me in the right direction with protocolMapping, but you need to specify the bindingConfiguration in the protocolMapping section to make it choose the <security mode=None> or <security mode=Transport>:

<system.serviceModel><serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>  <protocolMapping>    <add scheme="http"  binding="basicHttpBinding" bindingConfiguration="ServiceBinding"/>    <add scheme="https" binding="basicHttpBinding" bindingConfiguration="ServiceBindingSSL"/>        </protocolMapping>

Leave the bindingConfiguration attribute off of the endpoint - it will be set automatically based on the protocol. Then set up the second binding configuration in your bindings section:

        <basicHttpBinding>            <binding name="ServiceBinding"  ...>                <security mode="None">                </security>            </binding>            <binding name="ServiceBindingSSL" ... >                <security mode="Transport">                </security>            </binding>        </basicHttpBinding>

This technique worked for me. Hope it helps you!


Have you looked at the protocol mapping wcf configuration section?

<protocolMapping>    <add scheme="http" binding="wsHttpBinding"/>    <add scheme="https" binding="wsHttpBinding"/>      </protocolMapping>

Edit : I am not sure why you have configured an "https" binding under the webHttpBinding type. Shouldn't you have both http webHttpBinding and wsHttpBinding defined and assigned to your endpoint?

<webHttpBinding>    <binding name ="HttpBinding">        <security mode="None">            <transport clientCredentialType="None"/>        </security>    </binding></webHttpBinding><wsHttpBinding>    <binding name="wsHttpEndpointBinding">        <security mode="TransportWithMessageCredential">            <transport clientCredentialType="None" />            <message clientCredentialType="UserName" />        </security>    </binding></wsHttpBinding>


i finally got it working for HTTP & HTTPS, based on Rick Moss' answer. except that i did not leave away the bindingConfiguration off the endpoint, because it caused a no endpoint listening error:

<configuration>  ...  <system.serviceModel>    <services>      <service name="WcfService1.Service1">        <endpoint address="" name="ep1" binding="webHttpBinding" bindingConfiguration="ServiceBinding" contract="WcfService1.IService1" behaviorConfiguration="WebBehavior" />        <endpoint address="" name="ep1" binding="webHttpBinding" bindingConfiguration="ServiceBindingSSL" contract="WcfService1.IService1" behaviorConfiguration="WebBehavior" />      </service>    </services>    <bindings>      <webHttpBinding>        <binding name="ServiceBinding" ...>          <security mode="None">          </security>        </binding>        <binding name="ServiceBindingSSL" ...>          <security mode="Transport">            <transport clientCredentialType="None" />          </security>        </binding>      </webHttpBinding>    </bindings>    <behaviors>      <endpointBehaviors>        <behavior name="WebBehavior">          <webHttp />        </behavior>      </endpointBehaviors>      ...    </behaviors>    <protocolMapping>        <add scheme="http"  binding="basicHttpBinding" bindingConfiguration="ServiceBinding"/>        <add scheme="https" binding="basicHttpBinding" bindingConfiguration="ServiceBindingSSL"/>          </protocolMapping>    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />  </system.serviceModel>  ...</configuration>

EDIT: this only seems to work with Anonymous authentication in IIS. Enabling Windows Authentication in a different web service under the same website in IIS caused:

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

i hate Web.config files. they're too tightly coupled to IIS