Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Support both HTTP and HTTPS Web.Config WCF Service

I'm running into a configuration problem with my WCF service when trying to support both https and https. Ideally what I'd like is to run http on my dev machine and then publish to azure running https.

I followed these posts to try and run the configuration: http://jayakrishnagudla.blogspot.com/2009/12/configuring-wcf-services-to-work-with.html

How to configure a single WCF Service to have multiple HTTP and HTTPS endpoints?

My Web.Config is as follows:

<system.serviceModel>
    <services>
      <service name="Backend.Services.UserService.UserService" behaviorConfiguration="">

        <endpoint address=""
           binding="webHttpBinding"
           contract="Backend.Services.UserService.IUserService"
           bindingConfiguration="HttpsBinding"
           behaviorConfiguration="Web">

        </endpoint>

        <endpoint address=""
                  binding="webHttpBinding"
                  contract="Backend.Services.UserService.IUserService"
                  bindingConfiguration="HttpBinding"
                  behaviorConfiguration="Web"
                  >

        </endpoint>
      </service>
    </services>
      <bindings>
        <webHttpBinding>
          <binding name ="HttpBinding">
            <security mode="None">
              <transport clientCredentialType="None"/>
            </security>
          </binding>

          <binding name="HttpsBinding">
            <security mode="Transport"/>
          </binding>
        </webHttpBinding>

    </bindings>
    <behaviors>

      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="Web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <protocolMapping>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
  </system.serviceModel>

As far as I can tell, this configuration should be correct according to the above links. However, when I build and run this service locally via http I get the following error:

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

I'm not quite sure where the problem is and assume its a misconfiguration. Any help would be appreciated.

like image 599
zic10 Avatar asked Jun 28 '15 23:06

zic10


People also ask

How can I combine the WCF services config for both http and https in one web config?

You need to enable both HTTP & HTTPS in IIS. In IIS 7.5, go to your site and click on Bindings under Edit Site Action. Ensure that both http & https have been added. Then you need to create a binding for HTTP under <basicHttpBinding> , with security mode set to none.

What are 3 basic WCF configurations required for hosting a WCF service?

There are three types of hosting environments for WCF services: IIS, WAS, and self-hosting. The term “self-hosting” refers to any application that provides its own code to initialize the hosting environment. This includes console, Windows Forms, WPF, and managed Windows services.


1 Answers

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!

like image 107
Rich Moss Avatar answered Nov 03 '22 23:11

Rich Moss