WebHttpBinding with Http and Https

2019-04-05 02:41发布

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>

5条回答
戒情不戒烟
2楼-- · 2019-04-05 03:10

If you update your endpoint configs like such:

        <endpoint address="http://myserver/services/Lookups.svc" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="httpWebBinding" contract="MyService.Lookups" >         
        </endpoint>
        <endpoint address="https://myserver/services/Lookups.svc" behaviorConfiguration="AjaxBehavior"
          binding="webHttpBinding" bindingConfiguration="httpsWebBinding" contract="MyService.Lookups" >          
        </endpoint>

I have done a similar dance with a prod system, and this was my solution. Hope it works for you as well.

查看更多
该账号已被封号
3楼-- · 2019-04-05 03:13

As @Lesmian pointed out in his answer, the issue is in your IIS configuration. More specifically in:

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

The reason is that IIS can not handle endpoints on a schema which it does not support.

You have two sites, and one of them has HTTP binding but does not has HTTPS, and the other has HTTPS but not HTTP.

So when you browse to http:// URL, IIS directs you to the (surprise!) http-enabled site, reads web.config, sees that it registers https endpoint (which is not supported by the site) and throws the exception telling that there is no https scheme support on the http-enabled-only site.

When you browse to the https:// URL the situation is similar - IIS does not allow you to use http endpoint on the https-enabled-only site.

To handle the issue you better use a single site with two bindings.

Another (and more complex) option would be using different web.configs for sites: set up separate sites (pointed to separate folders) and use the publishing and web.config transforming tools of the Visual Studio

查看更多
霸刀☆藐视天下
4楼-- · 2019-04-05 03:18

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.

查看更多
5楼-- · 2019-04-05 03:18

Add This Code.

<protocolMapping>
  <add scheme="http"  binding="webHttpBinding" bindingConfiguration="httpWebBinding"/>
  <add scheme="https" binding="webHttpBinding" bindingConfiguration="httpsWebBinding"/>
</protocolMapping>
查看更多
闹够了就滚
6楼-- · 2019-04-05 03:27

I dont't know this query is still active or not but as i checked Please

Add binding="mexHttpsBinding" also with binding="mexHttpBinding" with different endpoint

This helps me.

查看更多
登录 后发表回答