简体   繁体   中英

Enabling http and https on the same service

I am writing a WCF service which contains a singe contract. I would like web clients to call the service endpoint using either http or https.

My web.config is as follows:

(Some parts have been removed for brevity)

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IDataService" />
        <binding name="BasicHttpsBinding_IDataService" >
          <security mode="Transport" />
        </binding>
      </basicHttpBinding>
    </bindings>

    <services>
      <service behaviorConfiguration="DataServiceMetadataBehavior" name="DummyService.DataService">

        <endpoint address="" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IDataService"
          contract="DummyService.IDataService"/>

        <endpoint address=""
                 binding="basicHttpBinding" bindingConfiguration="BasicHttpsBinding_IDataService"
                 contract="DummyService.IDataService" name="BasicHttpsBinding_IDataService" 
                  />

      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="DataServiceMetadataBehavior">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add scheme="http" binding="basicHttpBinding" />
      <add scheme="https" binding="basicHttpBinding" />

    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>

When I try to test the service using the VisualStudio test client, it gives the following error:

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

Everything works fine with only a single endpoint.

An endpoint consists of address, binding and contract. http://YourBaseAddress/YourApp/YourService.svc and httpS://YourBaseAddress/YourApp/YourService.svc are different address with different schema name or protocol as well as different ports: 80 and 443 by default, so you can have both endpoints for the service, with the same basicHttpBinding, provided that the https one has a bindingConfiguration for SSL transportation as you had done. The error message is quite informative, so you need to go to IIS (or IIS Express) to make sure there's a http listener, say https binding defined after checking the "Edit Bindings" function of the Website. After you had done so, you should be able to get WSDL through httpS://YourBaseAddress/YourApp/YourService.svc?wsdl in a Web browser.

In fact, many Web services/applications like those from Microsoft and Google support both http and https through the same host name and path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM